57
votes
Best practice for redundant conditions in if-elif-else statements
Even Case 1 can have unexpected behaviour. The value of n that worries me is NaN.
n = float('nan')
if n == 0:
doThis() # Not triggered
elif n < 0:
doThat() # Not triggered
elif n > 0:
...
25
votes
Accepted
Best practice for redundant conditions in if-elif-else statements
Short answer
I would argue that there is no singular correct answer to this question.
It depends on what you want to have happen when one of the above cases changes. Are you assuming that the last ...
17
votes
Accepted
Is it good practice to eliminate zero in a statement if possible (e.g.:rewrite a-b>0 into a>b)?
Semantically, your two examples mean two different things. Your first example checks to see if the result of a subtraction is a positive number. Your second example checks to see if one number is ...
9
votes
Accepted
Avoiding if statements in Nested FOR loops
One way to avoid those ifs inside the loops is to put them outside, by deciding which functions to call in advance:
//set the values of conditionA, conditionB and conditionC;
functionA = conditionA ?...
7
votes
Accepted
Changing large number of if-elif-else statements to use underlying structure
Your initial function executes one specific step for one specific type, both of them being provided as argument. In the second case you iterate over steps trying to find the right one.
Alternative ...
7
votes
Is it good practice to eliminate zero in a statement if possible (e.g.:rewrite a-b>0 into a>b)?
It depends a lot (on the semantics of - and of >).
First, you did not mention the type of a and of b. But let's assume for simplicity they both have the same integral type (e.g. C int or long).
...
7
votes
Accepted
How to structure many complex conditionals on a class
One approach would be to create a bunch of objects (or just functions) each of which takes an Order and only checks for the unique combination that is supposed to trigger a business outcome, put them ...
6
votes
Accepted
Refactor multiple "if" in C#
The number in the ServiceTypes string must match the the member A, B, C, D, right? So start by providing a function which makes access to those members possible by an index. Something along the lines ...
6
votes
Accepted
Highlighting importance of order when using short-circuited conditions
Why ?
In C, C++, Python, Java, C#, Groowy, and Swift (historical order of appearance), and certainly in many other languages as well, the usual logical and-operator (mostly &&) works with ...
4
votes
Changing large number of if-elif-else statements to use underlying structure
This is based on Christophe's answer.
Christophe's answer focuses on using a double dictionary, with one set of keys the typ and the other set of keys the step. However, this doesn't account for the ...
3
votes
Avoiding if statements in Nested FOR loops
There are many ways. Here's a tidy one:
if (conditionA)
{
foreach (var x in X)
{
foreach (var y in Y)
{
//computeA ..makes use of x and y
}
}
}
if (...
3
votes
Highlighting importance of order when using short-circuited conditions
&& and || at least in java (and I guess in groovy as well) mean that the operation is short-circuited, i.e. as you said the right hand side will be evaluated if and only if it's necessary.
&...
3
votes
Accepted
If statement best practice
In this particular case, I don't think there is much to recommend approach A versus approach B, or vice versa.
However, I disagree that in general it's better to use
if A then
stuff
else
...
3
votes
Refactoring multiple non-nested if statements [C#]
This might be solvable with polymorphism. It would still be crazy, but it could be an option.
Roughly, it looks like the logic is:
if (bytes equals dbBytes)
changes += "some sort of text"...
2
votes
Best practice for redundant conditions in if-elif-else statements
In the specific case of n > 0 vs n < 0 vs n == 0, I think it's best to write the == case last and use else for it, but with a comment, like this:
if n > 0:
...
elif n < 0:
...
else:...
2
votes
Best practice for redundant conditions in if-elif-else statements
The real problem is: You are sure there are the three cases n>0, n=0 and n<0, but people who are sure they are right are often wrong. And you would want to express that.
Swift has a switch ...
2
votes
Best practice for redundant conditions in if-elif-else statements
Your own primary objection to your version 2 (with the unconditional else) seems to be that it acts wrongly when the type of the variable is not integer.
I'd argue that your first version is wrong as ...
1
vote
Shared code block followed by different block for mutually exclusive predicates
It depends on context and what makes the most sense reading.
In general, for most use cases I can think of, I would favor:
if cond1 or cond2
shared
if cond1
logic1
if cond2
logic2
Because ...
1
vote
Shared code block followed by different block for mutually exclusive predicates
There is no singular best way to write such an if-elif statement where some of the paths have partially shared code. It depends too much on the specifics of the code you are writing.
What can be said ...
1
vote
Best practice for redundant conditions in if-elif-else statements
As you mentioned python is dynamically typed, hence either ensure using numbers or use elif n > 0:.
(Could even apply some comparison three valued type. Like signum. Or an else exception.)
But you ...
1
vote
Accepted
rails, how to refactor nest if statements?
Short Circuit Evaluation is your friend here. It means a false before an && will drop you to the else much like an if would have. It just leaves you with only one else instead of many. As an ...
1
vote
How to effectively handle 404/500 http errors in server-side rendering web application which uses store for state?
If you're working on a server side rendered app, redux and react router seem like the wrong tools for you. The reason you're having difficulty is because you're taking a reasonably state-light ...
1
vote
Changing large number of if-elif-else statements to use underlying structure
You can call your typ functions in between your common steps if you pass the function as a parameter, then call it during your keystep.
# defining some dummy functions as the common code
def f1(*args)...
1
vote
Is it good practice to eliminate zero in a statement if possible (e.g.:rewrite a-b>0 into a>b)?
It's simple: If you want to know whether a is greater than b, then you write a > b. If you want to know whether the result of subtracting b from a is greater than 0, then you write a - b > 0.
Be ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
if-statement × 33conditions × 7
coding-style × 5
design-patterns × 4
javascript × 4
c# × 3
python × 3
refactoring × 3
programming-practices × 2
clean-code × 2
functions × 2
efficiency × 2
design × 1
java × 1
c++ × 1
php × 1
.net × 1
c × 1
terminology × 1
uml × 1
code-quality × 1
coding-standards × 1
language-agnostic × 1
optimization × 1
error-handling × 1