Skip to main content
53 events
when toggle format what by license comment
Jul 15, 2022 at 22:39 answer added Astrid timeline score: 0
Mar 20, 2021 at 18:58 answer added August Karlstrom timeline score: 2
Jun 14, 2020 at 22:23 comment added Peter Mortensen Related: The Forgotten Art of Structured Programming - Kevlin Henney. C++ on Sea 2019
Jun 13, 2020 at 16:31 answer added Pekka timeline score: 2
Jun 12, 2020 at 18:16 comment added cambunctious @Caltor my bad!
Jun 12, 2020 at 15:44 comment added Caltor @cambunctious so was mine! :D Quoting from the wikipedia article you referenced - "In Life, the Universe and Everything, a character named Prak who knows all that is true, confirms that 42 is indeed The Answer, and that it is impossible for both The Answer and The Question to be known in the same universe, as they will cancel each other out and take the Universe with them—to be replaced by something even more bizarre (as described in the first theory) and that it may have already happened (as described in the second)"
Jun 11, 2020 at 17:47 comment added cambunctious @Caltor It was an allusion. See en.wikipedia.org/wiki/…
Jun 11, 2020 at 14:11 comment added Caltor As another alternative to the first one you could remove the else and unindent the second return. Some might prefer that style.
Jun 11, 2020 at 14:05 comment added Caltor @cambunctious But what is the question? ;)
Jun 10, 2020 at 19:55 comment added Toby Surprised no-one has mentioned MISRA-C yet,.I think it used to be rule 17.4 (unsure if that's correct in newer editions). MISRA-C is intended for use in specific environments and often specific industries. The implication being that if you are not in those envs or industries, and do not have to conform to a specific standard and are not coding with particular languages... Then follow common sense.
Jun 10, 2020 at 16:48 answer added gnasher729 timeline score: -1
Jun 10, 2020 at 16:04 comment added Dan Jones Not really an answer to your question, but personally, I would write that as: if (cond) { return 42; } return 97; No need for the else. Since there was a return in the if block, it's implied that anything after that block is already an else. This actually slightly decreases complexity.
Jun 10, 2020 at 15:50 comment added Jeff Learman On the other hand, I've seen cases where optimizers work better on code with no returns and no break statements in loops.
Jun 10, 2020 at 15:49 comment added Jeff Learman I often have to fight this bias too. I'm an advocate of early returns, either for guard clauses, or in cases where the responsibility has been handled (but more cases remain.) I find it leads to much cleaner, simpler, more linear code with fewer bugs.
Jun 10, 2020 at 14:52 answer added Murph timeline score: 4
Jan 28, 2020 at 18:25 comment added Emile Cormier I'm finding myself fighting against the single return habit in some performance-critical code, where I'm noticing it makes a speed difference in certain hot spots. In some cases, bailing out of the function early is cheaper than having to test a bunch of "ok to proceed" logic.
Nov 19, 2019 at 18:17 history edited Sazzad Hissain Khan
Added appropriate tags
May 8, 2019 at 13:49 comment added Mythics Early returns to simplify a function and readability are usually good, but one responsibility for a function usually leads to more modular and readable code. If you have a condition that is not purely for an early return, chances are higher your function is breaching single responsibility principals (if you care about that concept).
Mar 15, 2019 at 16:03 comment added cambunctious You should remove the condition altogether. The answer is 42.
Jan 2, 2019 at 22:23 comment added DeadMG That is one of the worst articles I have ever read. It seems like the author spends more time fantasising about the purity of his OOP than actually figuring out how to achieve anything. Expression and evaluation trees have value but not when you can just write a normal function instead.
Jan 2, 2019 at 18:27 comment added yegor256 Check this out: Why Many Return Statements Are a Bad Idea in OOP
Oct 8, 2018 at 11:33 answer added Sorter timeline score: 8
Mar 12, 2018 at 15:01 comment added visc John Carmack did it all the time in Doom. It's easier to get work done faster. Your tests should prove your work works. not mantras that make more work. Like Goto is evil. github.com/id-Software/DOOM/blob/…
Oct 16, 2017 at 19:03 answer added oopexpert timeline score: 14
Jul 7, 2017 at 1:26 comment added NMrt A similar question has also been answered at Stack Overflow: stackoverflow.com/questions/36707/…
Jul 6, 2017 at 21:54 history edited Deduplicator CC BY-SA 3.0
added syntax-highlighting; slight touchups
Aug 18, 2016 at 19:15 comment added mendota i think the example is a simple enough case where i wouldn't have a strong opinion one way or the other. the single-entry-single-exit ideal is more to guide us away from crazy situations like 15 return statements and two more branches that don't return at all!
May 14, 2016 at 11:39 comment added martinkunev It came from the notion of structured programming. Some may argue that having just one return allows you to easily modify the code to do something just before returning or to easily debug.
Feb 18, 2016 at 15:55 history edited user22815 CC BY-SA 3.0
Minor cleanup to focus the question better.
Feb 17, 2016 at 7:13 comment added gnat this question is discussed at meta
Nov 18, 2015 at 10:52 vote accept fredoverflow
S Nov 5, 2015 at 8:14 history suggested Victor Yarema
Added coding-style tag.
Nov 5, 2015 at 0:30 review Suggested edits
S Nov 5, 2015 at 8:14
Oct 29, 2014 at 3:47 history protected CommunityBot
Dec 14, 2013 at 9:14 comment added Piotr Perak This is somewhat connected to Guard Clause refactoring. stackoverflow.com/a/8493256/679340 Guard Clause will add returns to the beginning of your methods. And it makes code a lot cleaner in my opinion.
Oct 4, 2013 at 12:06 review Close votes
Oct 8, 2013 at 11:44
Oct 4, 2013 at 6:58 answer added Pieter B timeline score: 56
Mar 22, 2012 at 23:04 history edited Dynamic
edited tags
Nov 11, 2011 at 16:17 history made wiki Post Made Community Wiki by Eugene - AmberPixels
Nov 10, 2011 at 14:50 answer added Anthony timeline score: 22
Nov 10, 2011 at 1:52 history edited Adam Lear
edited tags
Nov 10, 2011 at 1:47 history edited user8
edited tags
Nov 9, 2011 at 23:19 history edited Steven Jeuris CC BY-SA 3.0
Changed title to reflect the part of the question which isn't a duplicate.
Nov 9, 2011 at 17:35 answer added user606723 timeline score: 7
Nov 9, 2011 at 16:32 answer added kevin cline timeline score: 1453
Nov 9, 2011 at 15:47 history edited Eric Wilson CC BY-SA 3.0
Removed sensasional language, and unecessary diversion to exception handling
Nov 9, 2011 at 15:20 answer added perreal timeline score: 99
S Nov 9, 2011 at 14:05 history suggested Mike Partridge CC BY-SA 3.0
rewrote title as a question
Nov 9, 2011 at 13:46 answer added John R. Strohm timeline score: 63
Nov 9, 2011 at 13:44 review Suggested edits
S Nov 9, 2011 at 14:05
Nov 9, 2011 at 8:45 answer added sbi timeline score: 1051
Nov 9, 2011 at 8:20 history tweeted twitter.com/#!/StackProgrammer/status/134183679059693569
Nov 9, 2011 at 7:10 history asked fredoverflow CC BY-SA 3.0