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 ...
4
votes
4
answers
271
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()
}
...
4
votes
4
answers
2k
views
Defining logic without nested if-else statements
I'm working on a somewhat simple game. Currently trying to implement the game logic for moving the pieces around.
Logic is something like this:
does player have pieces in inventory?
if yes:
...
0
votes
2
answers
737
views
Is this a case where 'else' is inevitable?
I am writing some code that enables / disables a certain kind of hardware function. To enable it on, I have to call some methods, and to disable it, some others.
Of course I want this code to be clean,...
3
votes
3
answers
284
views
patterns for converting user command-line input into computer commands
The problem: 50 different commands which are typed into a command line for a program, lets say for telling a robot what to do. Some of the commands have user determined values such as travel north 5 (...
1
vote
3
answers
187
views
Should conditionals be embedded in the function whose execution is contingent on them?
I have a large data structure that is about to be persisted to the database. Before that can happen I have to validate it and then update a bunch of it's properties based on various specific ...
0
votes
2
answers
705
views
What to do about complex business logic based on many options
To preface, I think this problem would be easier to understand if I gave an example: How does a complex tax evaluation application manage multiple tax laws and personal situations?
I inherited a web ...
0
votes
1
answer
821
views
Better pattern than large if/else if/else for checking conditionals [duplicate]
I've got a fairly large set of booleans I'm checking in javascript, and then using them to alter the state of a layout in my React app. The whole thing is unwieldy, difficult to read, inelegant, and ...
3
votes
2
answers
222
views
What is a proper way to chain instructions if previous one is ok [duplicate]
I'm currently working on a big file I have to parse and process and each step needs to be done in an order as I do SQL queries and need inserted IDs to make other insertions ...
The problem is I often ...