Linked Questions
23 questions linked to/from Elegant ways to handle if(if else) else
50
votes
6
answers
101k
views
Clarification of "avoid if-else" advice [duplicate]
The experts in clean code advise not to use if/else since it's creating an unreadable code. They suggest rather using IF and not to wait till the end of a method without real need.
Now, this if/else ...
16
votes
5
answers
8k
views
Keep indentation level low [duplicate]
I hear a lot that you should not write functions larger than one screen size, that you should extract things into functions if you call things very often and all these coding guidelines. One of them ...
4
votes
5
answers
18k
views
What is the Best Style for Functions with Multiple Returns and If/Else Statements? [duplicate]
Possible Duplicate:
Elegant ways to handle if(if else) else
In a function where there are multiple returns, what is the best style to use?
This style, where there is no 'else' because it is ...
4
votes
1
answer
2k
views
Redundant ElseIf-Else Blocks [duplicate]
These types of if-elseif-else blocks appear all over the place, and in no small number (so the less the better). Every time I have to think and decide: Do I want the simpler or the more thorough of ...
-2
votes
3
answers
3k
views
Nested if statements or multiple if statements [duplicate]
Which way is better?
-- Option 1-------------
if ( condition1 )
{
statement1
}
else
{
exit program
}
if ( condition2 )
{
statement2
}
else
{
exit program
}
----...
46
votes
13
answers
19k
views
How do I edit a chain of if-else if statements to adhere to Uncle Bob's Clean Code principles?
I'm trying to follow Uncle Bob's clean code suggestions and specifically to keep methods short.
I find myself unable to shorten this logic though:
if (checkCondition()) {addAlert(1);}
else if (...
56
votes
11
answers
154k
views
How would you refactor nested IF Statements? [duplicate]
I was cruising around the programming blogosphere when I happened upon this post about GOTO's:
http://giuliozambon.blogspot.com/2010/12/programmers-tabu.html
Here the writer talks about how "one ...
33
votes
3
answers
11k
views
How to tackle a 'branched' arrow head anti-pattern? [duplicate]
I recently read this question that features, the arrow anti-pattern.
I have something similar in code I'm trying to refactor except that it branches. It looks a little something like this:
if(...
20
votes
7
answers
2k
views
Should "else" be used in situations where control flow renders it redundant?
I sometimes stumble upon code similar to the following example (what this function does exactly is out of the scope of this question):
function doSomething(value) {
if (check1(value)) {
return -...
14
votes
5
answers
63k
views
Simple and clean way of comparing three numbers
I've got some code that has a sequence of ifs that work, but just feel messy. Basically, I want to choose the largest of three integers and set a status flag to say which was chosen. My current code ...
3
votes
5
answers
17k
views
Refactoring if-else logic to reflect the OOP principles
I have read some of the related questions regarding how we can refactor a code based on if/else if statements to follow closely the OOP principles but I have difficulties to apply this to a concrete ...
5
votes
6
answers
11k
views
Best approach - convert multiple conditional if -else in a more handy design
I have a class that handles the state of a response, called StockResponse.
The code has multiple ifs to handle each state of the stock. Most of the cases has a default behaviour, but some conditions ...
7
votes
6
answers
6k
views
How to represent a long if-else tree in a concise manner
Long story short, I've inherited a Java piece of code made of methods like this one:
@Override
public Action decide() {
if (equalz(in.a, "LOC")) {//10
if(( //20
equalz(...
6
votes
2
answers
6k
views
Java convention for if/else if order
I have an if/else if structure that on some cases does nothing. From the code I have seen in my career, the "empty" case is normally left out. But when I wrote my code, it just doesn't feel right to ...
3
votes
4
answers
2k
views
Alternative to nested-if on single return functions
I'd like to see if there has been any precedent on alternatives to nested-ifs--particularly for error-code returns. My workplace requires one return per function, so I cannot early exit. Here is some ...