Linked Questions
18 questions linked to/from Should I return from a function early or use an if statement?
16
votes
10
answers
13k
views
Best practices concerning exit in Delphi [duplicate]
A co-worker and myself are having a debate on whats best. Both concepts work and work well but is there a general consensus on not making a call to exit?
Whats better?
To call exit within a ...
7
votes
2
answers
10k
views
"Proceed if true" vs "stop if false" in if statements [duplicate]
While I was writing a private helper method in Java, I needed to add a check for a precondition that would cause the method to do nothing if not met. The last few lines of the method were just off the ...
0
votes
3
answers
1k
views
When not to use early return? [duplicate]
Nesting is unavoidable, however in most cases returning early is a more viable option.
Consider the following snippet:
MyRoutine(argument)
{
if (0 == argument) {
SubRoutine(argument);
...
0
votes
3
answers
1k
views
Which is more readable: early returns, or conditionals? [duplicate]
I’m writing an asynchronous, Promise-returning function. In the processing I test some conditions, and if the conditions pass, the promise should be fulfilled (resolved), but if one fails, then the ...
2
votes
4
answers
228
views
function exit condition on parameter consistency check [duplicate]
When checking for parameter consistency a the top of a function body, what is the best strategy?
This one:
protected void function(Object parameter)
if (parameter == null)
return;
...
-3
votes
1
answer
2k
views
Best practices: Number of return statements [duplicate]
I'd like to ask about best practices on return statements.
So, what would be more preferable, something like...
...
String r = null;
if(var != null) {
r = "NOT NULL!";
}
return r;
Or something ...
-2
votes
1
answer
80
views
Are These Both The Same Or Different If Statements [duplicate]
This is for unity
I tried another way of stopping player after he dies and it worked fine but now it does not work with sounds
That was my way the only thing that I changed is the state
Ben's was
...
1294
votes
13
answers
251k
views
Where did the notion of "one return only" come from?
I often talk to programmers who say "Don't put multiple return statements in the same method." When I ask them to tell me the reasons why, all I get is "The coding standard says so." or "It's ...
72
votes
7
answers
10k
views
Should I follow the normal path or fail early?
From the Code Complete book comes the following quote:
"Put the normal case after the if rather than after the else"
Which means that exceptions/deviations from the standard path should be put in ...
39
votes
8
answers
11k
views
Inverting an IF statement
So I've been programming for a few years now and recently have started using ReSharper more. One thing that ReSharper always suggests to me is to "invert 'if' statement to reduce nesting".
Let's say ...
12
votes
6
answers
1k
views
Best Practice - Wrapping if around function call vs Adding early exit if guard in function
I know this can be very use-case specific, but I find myself wondering this far too often. Is there a generally preferred syntax.
I'm not asking what is the best approach when in a function, I am ...
7
votes
2
answers
37k
views
if and else or if and return?
I have a Java method with a void type of return that checks a condition. It do something in case of true and other thing in case of false, typical if / else structure, but is possible use only the if ...
5
votes
4
answers
7k
views
What are the benefits of closing every if-statement with an else in Python?
I am reading Learn Python the Hard Way by Zed Shaw. In this lesson he writes: "Every if-statement must have an else."
What are the benefits of ending every if-statement with an else? Are there any ...
3
votes
3
answers
275
views
Structuring an if-else clause inside a for loop [closed]
Suppose I am doing an if-else clause inside a for loop. I like to structure it as
for (...)
if (...)
// (part A)
continue;
//(part B)
rather than
for (...)
if (...)
...
0
votes
3
answers
171
views
Eliminating the bad cases in if to get a nicer code
I have read this link Should I return from a function early or use an if statement? and it triggered a conflict in my head. I agree that it looks nicer and cleaner and I guess that would be the way I ...