The real crime here is not putting opening braces on a new line.
Real truth.The real crime here is not putting opening braces on a new line.
A is preferred by clang-tidy and as such is all you'll see in our codebase. We can't merge code that conforms to B.Seen a lot of the former practice at my workplace lately and tbh it's totally bothering me, I'd put human readability above most things. Are you all about saving characters? Is the former actually somehow "safer" in your opinion?
function doThing() {
return (condition ? x = 1 : 2);
}
bool MyFunc()
{
if (situation)
{
return true;
}
else
{
return false;
}
return false;
}
No, C# would handle B fine.Bit of a sidetrack, but in C#, wouldn't case B fail because there's no default Return? Like, I'd have to do this if I wanted to add the explicit else case becuse without the second Return False, it wouldn't compile:
C#:bool MyFunc() { if (situation) { return true; } else { return false; } return false; }
Otherwise, I'd be forced to use A, right? I realise B is clearer to human readability.
The real crime here is not putting opening braces on a new line.
Which language are you thinking? Because at least in JS that would be overly complex (simply condition ? 1 : 2 would work) and as a side effect, you'd have a global variable x.I'm an A guy.
But a ternary-like thing would be another option
Code:function doThing() { return (condition ? x = 1 : 2); }
Yeah, because really, it should flag your final return as being unreachable.Yeah, okay, just a weird compiler issue in my case then, I guess? Just wondered as this thread brought that specific quirk to mind.
Funny I was just writing this logic yesterday. I used the else logic but Resharper tool wanted to simplify it to the first. The end of the day IMO it's not a huge deal either way so long as the logic is consise and clear.
B is more intuitive. A looks like it will return 2 even if the condition is true.
var = 1;
function doThing() {
if (condition) var = 2;
}
- readability is much more important than shorter codeThe argument B is more readable is weird. It's good to explicit and verbose at times, but that hardly matters in this case. Also as pointed out, most linters by default would accept B as the correct solution.
Both are as readable but one has extra clutter in it (but a negligible amount).
Which language are you thinking? Because at least in JS that would be overly complex (simply condition ? 1 : 2 would work) and as a side effect, you'd have a global variable x.
const doThing = () => condition ? 1 : 2;
function doThing(input) {
if input != NULL {
return ERROR1;
}
else if (valid(input)) {
return ERROR2;
}
else if (someGlobalState == READY) {
return ERROR3;
}
else {
// do thing to input
return result;
}
}
function doThing(input) {
if input != NULL {
return ERROR1;
}
if (valid(input)) {
return ERROR2;
}
if (someGlobalState == READY) {
return ERROR3;
}
// do thing to input
return result;
}
Which language are you thinking? Because at least in JS that would be overly complex (simply condition ? 1 : 2 would work) and as a side effect, you'd have a global variable x.
Obviously the context would matter, but I'd argue the former is more readable, primarily because it's making it explicit that each of the conditions are intended to be mutually exclusive, whereas simply using a number of if statements may not necessarily mean they would all be exclusive.So in your example, I'd argue it's a wash, go for legibility (i.e. B). Where I might differ is the point at which you have multiple paths to return the same(ish) thing (for example multiple ways of validating an input prior to processing it leading to multiple error codes).
So for example:
Code:function doThing(input) { if input != NULL { return ERROR1; } else if (valid(input)) { return ERROR2; } else if (someGlobalState == READY) { return ERROR3; } else { // do thing to input return result; } }
I'd argue in this instance we've started to obfuscate where the major processing will be performed.
Code:function doThing(input) { if input != NULL { return ERROR1; } if (valid(input)) { return ERROR2; } if (someGlobalState == READY) { return ERROR3; } // do thing to input return result; }
I've seen alot of code that nests the processing within an unseemly number of if statements where a flatter structure and multiple return points would be more readable (assuming your language allows this that is). But of course ultimately, alot of this is down to what you'll find legible. Assuming you're running through some sort of intelligent compiler/interpreter, it'll normally produce similar executable code anyway.
This is the one. A lead of mine once told me they preferred functions with a single entry and exit point (i.e., only one return statement), and that's become completely ingrained in my coding style ever since.
A lead of mine once told me they preferred functions with a single entry and exit point (i.e., only one return statement), and that's become completely ingrained in my coding style ever since.
Those are functionally two very different code blocks even if the output is the same in this scenario. The first one actually obfuscates the logic more because the blocks are all connected in a single logical chain. Only one of those statements will ever be executed. In the second block, you're guaranteed that each check will occur sequentially and in order if for some reason one of the statements was not actually a return statement. Or if multiple of the logic branches were true (like A < 2, followed by A < 3).So in your example, I'd argue it's a wash, go for legibility (i.e. B). Where I might differ is the point at which you have multiple paths to return the same(ish) thing (for example multiple ways of validating an input prior to processing it leading to multiple error codes).
So for example:
Code:function doThing(input) { if input != NULL { return ERROR1; } else if (valid(input)) { return ERROR2; } else if (someGlobalState == READY) { return ERROR3; } else { // do thing to input return result; } }
I'd argue in this instance we've started to obfuscate where the major processing will be performed.
Code:function doThing(input) { if input != NULL { return ERROR1; } if (valid(input)) { return ERROR2; } if (someGlobalState == READY) { return ERROR3; } // do thing to input return result; }
I've seen alot of code that nests the processing within an unseemly number of if statements where a flatter structure and multiple return points would be more readable (assuming your language allows this that is). But of course ultimately, alot of this is down to what you'll find legible. Assuming you're running through some sort of intelligent compiler/interpreter, it'll normally produce similar executable code anyway.
A will return 2 no matter what and then only return 1 if the statement is true.
B is the proper form if you are trying to do an either/or thing.
EDIT: Ignore me. I haven't had my coffee yet.
The real real crime is not just doing whatever is most conventional for your project or language and moving on to things that actually matter. :)