Linked Questions

35
votes
6answers
67k 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 ...
9
votes
5answers
3k 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 ...
3
votes
5answers
7k 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
1answer
1k 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
3answers
636 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 } ----...
45
votes
13answers
14k 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 (...
32
votes
12answers
71k 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 ...
18
votes
7answers
1k 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 -...
26
votes
3answers
7k 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(...
11
votes
5answers
25k 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 ...
2
votes
5answers
9k 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 ...
6
votes
2answers
4k 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 ...
6
votes
6answers
2k 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(...
3
votes
6answers
2k 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 ...
4
votes
4answers
206 views

Elegant way to handle multiple pathways based on compound condition [closed]

Consider the following: if (x == 5 || x == 10 || x == 12) { if (x == 5) { doSomething() } else if (x == 10) { doSomethingElse() } else { doSomeOtherThing() } ...

15 30 50 per page