In the embedded world, I imagine it would be a good standard to force people to consider refactoring functions to their truth table.
Generally, I strongly favor coding standards to combat bad coding habits. Curious how MISRA works out in practice.
[1] https://spin.atomicobject.com/2011/07/26/in-defence-of-misra...
int multiRet() {
if (!a) return 0;
if (!b) return 1;
return c ? 2 : 0;
}
The singleRet function, as someone else already pointed out, actually has different behaviour (as well as being, in my view, just slightly harder to read and reason about): int singleRet() {
if (!a) return 0;
return (b&&c) ? 2 : 1;
}
Both of these seem to me much much clearer than the multiply-nested versions there, and much clearer than introducing a new variable for the sake of a single return point.(The other way to get a single return point here is to turn the code into a nest of ?: operators. I claim that will be less readable and more error-prone for most C programmers.)
I can't recall any instance I've ever seen where code became simpler or easier to follow as a result of turning multiple return points into a single return point.
WTF?? No it isn't. You have this magic initial value and have to figure out which code paths have mutated it and which haven't. Mutable variables make code far far more complex to read.
I'm all for single return but through the use of expressions, not variables; I would probably express the given function as:
expression() {
return
a ?
(b && c) ?
2
:
1
:
0
;
}
(Ideally I'd use a language that allowed a sensible conditional expression rather than one that relies on bizarre symbols, but we work with what's available) let expression a b c = match (a, b, c) with
| (true, true, true) => 2,
| (true, true, _) => 1,
| (false, _, _) => 0
I like how pattern matching allows you to create something very close to a truth table. It allows you to visually see what's going on.EDIT: Just realized that the two code examples in the parent's link don't even do the same... Look at the truth table, and look at the case of 1 1 0. The first code would indeed return 0, but not the second one. Derp.
It's not only a "good standard". It helps with verification: you can verify that the truth table is correct, and then that the function correctly implements that truth table. At least the former can sometimes be done formally.
> Curious how MISRA works out in practice.
Like every other standard, it depends on who's implementing it and with what tools, but it generally works out pretty well.
Java has better much better code safety than C (no pointers, gc, etc.) is portable via a VM, and is not used in embedded systems. It's also not specifically ISO C. MISRA was built do address certain issues with a certain language, in a certain environment. You shouldn't apply everything it recommends blindly to another language just because they both have conditions and return statements.
If you can explain to me why java benefits from having a single return statement, I would consider it. Otherwise, I refuse to blindly follow guidelines that were designed for different problems.
> None of the points raised in this thread pertains only to one programming language.
Except you replied to a java problem with a C style guide.
> I'm sure you'll find at least one style guide forbidding multiple returns for every language that has return statements.
And if that guide can explain why java benefits from having a single return statement, I would consider it. Otherwise, I refuse to follow guidelines that were designed for different problems or cosmetic reasons.