Linked Questions

73 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 ...
jao's user avatar
  • 1,123
12 votes
6 answers
2k 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 ...
Matthew Mullin's user avatar
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 ...
Brian Dant's user avatar
2 votes
4 answers
239 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; ...
Seraphim's host's user avatar
1 vote
4 answers
638 views

How would I add up enumerator values such that any combination provides a unique number?

Backstory (You can skip) I am writing a pronunciation library for irregular words. Take something like the following: T1E1s // tee one E one es | tee one E ones 1994-1995// 1994 (minus|dash|to|) ...
Anon's user avatar
  • 3,593
3 votes
3 answers
331 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 (...) ...
Eric Auld's user avatar
  • 141
0 votes
3 answers
712 views

Code Design: Repeatedly doing the same Boolean check with Stream Writer

I have several areas in a program where I am doing the following check on the same two booleans, but each spot has different text being written to a file via Stream Writer based on the value of the ...
Godwin's user avatar
  • 11
0 votes
3 answers
3k 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 ...
chharvey's user avatar
  • 264
0 votes
3 answers
172 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 ...
acy's user avatar
  • 3
0 votes
3 answers
2k 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); ...
Misiur's user avatar
  • 109
10 votes
2 answers
50k 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 ...
Orici's user avatar
  • 217
7 votes
2 answers
11k 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 ...
ThisIsNoZaku's user avatar
-2 votes
2 answers
191 views

Leave method (return) based on pass/fail outcome of another method (C#)

I have a WPF application used to execute logic that tests hardware (i.e. reads sensors, amps, voltages, etc.) Each individual test runs when a "Test Device" method is called. I need to be ...
Dink McDinkleman's user avatar
-2 votes
1 answer
84 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 ...
FullMetalAlchemistModz_YT's user avatar
-3 votes
1 answer
3k 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 ...
Vers's user avatar
  • 113

15 30 50 per page