3 ms·
Reading up on MISRA, I found an interesting defense of a single return statement [1]. In the embedded world, I imagine it would be a good standard to force peo
by programmarchy 10y ago
Reading up on MISRA, I found an interesting defense of a single return statement [1].
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/ https://spin.atomicobject.com/2011/07/26/in-defence-of-misra...
- lmm 10y ago> That’s a lot simpler isn’t it? 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)
- roblabla 10y agoIn OCaml : 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.
- notalaser 10y ago> In the embedded world, I imagine it would be a good standard to force people to consider refactoring functions to their truth table. 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.
- gjm11 10y agoEquivalent to the "multiRet" function there and, I think, easier to follow: 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.