Excellent question that is difficult to answer. I'll give you a short response here and then write a blog post with example code when I have time. Put your email in your profile and I'll make sure to let you know when that's ready.
A little background: I have gotten many calls when legacy code has a bug or needs a critical enhancement and no one in-house is willing or able to figure it out. I'm no smarter than anyone else, but because I'm stupid and fearless, I often do something that few others attempt: I rewrite or refactor the code first, then work on the bug/enhancement. And the first thing I always do is look for early exits. This has always given me the most bang for my buck in making unintelligible code understandable. The only entry to any function or subroutine should be on the first line and the only exit should be on the last line.
This is never about what runs fastest or produces less lines of code. It's strictly about making life easier for the next programmer.
Early exits can make things really easy (I'll just get out now.) 20 lines of clean code. No problem. Until years later, when that function is 300 lines long, and the next programmer can't figure out what you're doing. Much of the maintenance had been done in emergency mode, each programmer just trying to get in and out as fast as they could.
Early exits make it much easier for bad things to evolve:
- 200 line multiple nested if statements
- 200 line multiple nested recursions
- unidentified but critical "modes" (add/change) (found/notFound)...
Removing early exits forces you to understand what really should be happening and enables you to structure code into smaller, more intelligible pieces.
Short, hand-wavy response, but I hope that helps clarify some. Stay tuned for a better answer...
check if we have already done this or so -> exit
check if this is possible to do. if not -> exit
do some first thing. on error -> exit
do some other thing. on error -> exit
...
> And the first thing I always do is look for early exits. This has always given me the most bang for my buck in making unintelligible code understandable.
Ouch. It will be kind of amusing if we ever work on the same code - I frequently start a bug fix by refactoring to introduce as many early exits as possible. I find guard clauses so much easier to understand than nested conditionals that sometimes refactoring it like this is the only way I can understand the code at all. I would love to see a blog post where you compare different styles.
OTOH, If you're talking about things other than guard clauses then I think we might have a much more similar viewpoint.
What if you fail on line 20? Carrying on may cause cascading errors, what alternatives are there to return an error code directly apart from storing a return value in a variable, use a goto to the end and return once? (assuming this is C)
But, a computer scientist once argued, multiple returns are structured: they return to the same point. Knuth wrote an article on structured programming with GOTOs, and pointed out that this can save memory.
I disagree because I find that it makes my code more elegant. A lot of smart developers I respect have also embraced early exits. But then again I haven't yet worked with large legacy codebases.
I'm still learning so I'd be grateful for a heads up if you make that blog post. I've put my email in my hn profile.
I think the biggest problem is that early exits make you forget you have to clean things up before returning (a bigger problem in C and sometimes Java, than it is in modern dynamic languages). There are certainly other reasons, but I'll leave them to others that are more experienced!
njharman|13 years ago
pestaa|13 years ago
eranation|13 years ago
edw519|13 years ago
A little background: I have gotten many calls when legacy code has a bug or needs a critical enhancement and no one in-house is willing or able to figure it out. I'm no smarter than anyone else, but because I'm stupid and fearless, I often do something that few others attempt: I rewrite or refactor the code first, then work on the bug/enhancement. And the first thing I always do is look for early exits. This has always given me the most bang for my buck in making unintelligible code understandable. The only entry to any function or subroutine should be on the first line and the only exit should be on the last line.
This is never about what runs fastest or produces less lines of code. It's strictly about making life easier for the next programmer.
Early exits can make things really easy (I'll just get out now.) 20 lines of clean code. No problem. Until years later, when that function is 300 lines long, and the next programmer can't figure out what you're doing. Much of the maintenance had been done in emergency mode, each programmer just trying to get in and out as fast as they could.
Early exits make it much easier for bad things to evolve:
Removing early exits forces you to understand what really should be happening and enables you to structure code into smaller, more intelligible pieces.Short, hand-wavy response, but I hope that helps clarify some. Stay tuned for a better answer...
albertzeyer|13 years ago
It often goes like this:
zmmmmm|13 years ago
Ouch. It will be kind of amusing if we ever work on the same code - I frequently start a bug fix by refactoring to introduce as many early exits as possible. I find guard clauses so much easier to understand than nested conditionals that sometimes refactoring it like this is the only way I can understand the code at all. I would love to see a blog post where you compare different styles.
OTOH, If you're talking about things other than guard clauses then I think we might have a much more similar viewpoint.
rlpb|13 years ago
This is what I would focus on avoiding instead.
adsr|13 years ago
ChristianMarks|13 years ago
godbolev|13 years ago
I'm still learning so I'd be grateful for a heads up if you make that blog post. I've put my email in my hn profile.
FredBrach|13 years ago
pooriaazimi|13 years ago
pooriaazimi|13 years ago
http://www.osdata.com/programming/loops/earlyexit.html
http://programmers.stackexchange.com/questions/118703/where-...
eranation|13 years ago