5 ms·
Small improvement, but this can help: if(someCrap == null) { return; } I like that Swift makes this more explicit with the guard statement, bu
by programmarchy 10y ago
Small improvement, but this can help:
if(someCrap == null) {
return;
}
I like that Swift makes this more explicit with the guard statement, but I've found it useful to reduce the "mental stack" in other languages, too.
- adrianN 10y agoNot everybody is allowed to do that. MISRA for example requires a single exit point.
- rjzzleep 10y agoThat's 14.7, but if for some reason 14.7 doesn't apply 16.8 requires that all exit points return a value. So I'd argue MISRA lets you do that.
- programmarchy 10y agoReading 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.
- deleted 10y ago[deleted]
- deleted 10y ago[deleted]
- rootlocus 10y agoMISRA wasn't written with java in mind.
- adrianN 10y agoAnd in C(++) you never have to check preconditions that maybe should lead to an early return? I don't understand your point.
- rootlocus 10y agoFrom the first paragraph on wikipedia: "Its aims are to facilitate code safety, security, portability and reliability in the context of embedded systems, specifically those systems programmed in ISO C / C90 / C99." 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.
- adrianN 10y agoIf you read this thread again, you'll see that someone recommended early returns to reduce nesting. I replied that some people are not allowed to do that (eg because they have to follow MISRA). There is no need to quote Wikipedia. None of the points raised in this thread pertains only to one programming language. I'm sure you'll find at least one style guide forbidding multiple returns for every language that has return statements.
- rootlocus 10y agoThis is getting ridiculous. The reason I quoted wikipedia was to explain why "MISRA wasn't written with java in mind". Will you argue otherwise? > 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.