Skip to main content
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: ...
Oscar Cunningham's user avatar
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 ...
Flater's user avatar
  • 59.5k
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 ...
Robert Harvey's user avatar
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 ?...
David Arno's user avatar
  • 39.6k
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 ...
Christophe's user avatar
  • 82.2k
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). ...
Basile Starynkevitch's user avatar
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 ...
Filip Milovanović's user avatar
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 ...
Doc Brown's user avatar
  • 220k
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 ...
Christophe's user avatar
  • 82.2k
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 ...
auden's user avatar
  • 1,656
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 (...
candied_orange's user avatar
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. &...
Vadim Samokhin's user avatar
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 ...
Lewis Pringle's user avatar
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"...
Greg Burghardt's user avatar
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:...
zwol's user avatar
  • 2,620
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 ...
gnasher729's user avatar
  • 49.4k
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 ...
AnoE's user avatar
  • 5,892
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 ...
Flater's user avatar
  • 59.5k
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 ...
Bart van Ingen Schenau's user avatar
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 ...
Joop Eggen's user avatar
  • 2,629
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 ...
candied_orange's user avatar
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 ...
Dan Monego's user avatar
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)...
Isaac Moreno's user avatar
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 ...
gnasher729's user avatar
  • 49.4k

Only top scored, non community-wiki answers of a minimum length are eligible