What's the difference?
There is no functional difference.
thank you, never stop believing
It's a matter of aesthetics..
Which you're going to choose largely depends on the sizes of code1, code2 and code3.
>>338435
Functionally, there's no difference.
Stylistically, some would argue that including the else makes it clear that the programmer thought of what should happen in that situation. And some would argue that's a dumb rationale and people would catch it in code review if it was really a problem. Of course I have absolutely no opinion on this at all. ;)
>>338545
Personally, I'm inclined to prefer the former as a matter of style. If all branches of the if lead to a return, but the programmer still provides an else anyway, it might lead me to believe they think there's a chance it could fall through. making it "clear that the programmer thought of what should happen in that situation" doesn't really make sense to me here as a style argument, since if they write more code under it... that's what they intend to happen in that situation
>>338560
>since if they write more code under it... that's what they intend to happen in that situation
Yeah, but how do you know they specifically decided the "else" block is unnecessary. As opposed to forgetting it.
I can't say I outright hate this reasoning, but it's problematic because it's targeted towards coders who aren't thorough. The ones who can't/won't independently test their own stuff before a formal review. Otherwise they will almost certainly spot an issue like this and correct it.
>>338581
Sorry, I fail to see how this is "targeted towards coders who aren't thorough". Maybe I'm misunderstanding you. Is one less likely to test their code for writing it like this?
I see what you mean about the possibility of forgetting the else, but to me this is just dual to the problem I mentioned.
- Did they leave out the else? Maybe they forgot to write it.
- Did they include an else, when I just read an if block whose every branch exits the function? Maybe they forgot to write the case that would fall below this else.
In this light, I'm pretty sure whatever you're saying about thoroughness applies to both cases equally.
I think I'm liable to say at this point that neither presents a clear advantage over the other, which is why it isn't important which you choose
namaste