204 votes
Accepted

Developer insists if statements shouldn't have negated conditions, and should always have an else block

Explicit else block The first rule just pollutes the code and makes it neither more readable, nor less error-prone. The goal of your colleague — I would suppose — is to be explicit, by showing that ...
Arseni Mourzenko's user avatar
82 votes

How do I edit a chain of if-else if statements to adhere to Uncle Bob's Clean Code principles?

Ideally I think you should extract your logic for getting the alert code/number into its own method. So your existing code is reduced all the way down to { addAlert(GetConditionCode()); } and ...
Steven Eccles's user avatar
70 votes
Accepted

How do I edit a chain of if-else if statements to adhere to Uncle Bob's Clean Code principles?

The important measurement is complexity of the code, not absolute size. Assuming that the different conditions are really just single function calls, just like the actions are not more complex than ...
cmaster - reinstate monica's user avatar
65 votes

Developer insists if statements shouldn't have negated conditions, and should always have an else block

Regarding the first rule, this is an example of useless typing. Not only does it take longer to type, it will cause huge amounts of confusion to anyone reading the code. If the code isn't needed, don'...
David Arno's user avatar
  • 39.1k
50 votes

Developer insists if statements shouldn't have negated conditions, and should always have an else block

1. An argument in favor of empty else statements. I oftentimes use (and argue for) something akin to that first construct, an empty else. It signals to readers of the code (both human and automated ...
David Hammen's user avatar
  • 8,244
36 votes

Developer insists if statements shouldn't have negated conditions, and should always have an else block

I use an empty else-branch (and sometimes an empty if-branch) in very rare cases: When it is obvious that both the if and else part should be handled somehow, but for some non-trivial reason the case ...
gnasher729's user avatar
  • 43.5k
31 votes

Should "else" be used in situations where control flow renders it redundant?

I think it depends on the semantics of the code. If your three cases are dependent on each other, state it explicitly. This increases readability of the code and makes it easier to understand for ...
André Stannek's user avatar
23 votes
Accepted

How to properly reverse the if statement when you have two conditions in it?

Build a truth table: p q p && q p || q !(p && q) !p || !q !(p || q) !p && !q ========================================================================== 0 0 ...
Vincent Savard's user avatar
23 votes
Accepted

Should "else" be used in situations where control flow renders it redundant?

I like the one without else and here's why: function doSomething(value) { //if (check1(value)) { // return -1; //} if (check2(value)) { return value; } return false; } Because doing ...
candied_orange's user avatar
23 votes

Developer insists if statements shouldn't have negated conditions, and should always have an else block

All else being equal, prefer brevity. What you don't write, nobody has to read and understand. While being explicit can be useful, that's only the case if it makes obvious without undue verbosity that ...
Deduplicator's user avatar
  • 8,701
22 votes

How do I edit a chain of if-else if statements to adhere to Uncle Bob's Clean Code principles?

It's controversial whether this is 'better' than the plain if..else for any given case. But if you want to try something else this is a common way of doing it. Put your conditions in objects and put ...
Ewan's user avatar
  • 72.4k
13 votes
Accepted

Is it better to guard the method call or the method itself?

I think of guards as something the method must obey. In your example, the method must not give apples if Settings.GiveApples is false. If that is the case then the guard definitely belongs inside the ...
Jeremy Hutchinson's user avatar
13 votes
Accepted

Avoid if-else block in favor of default assignment followed by if

This is more of a style issue than anything else, and very susceptible to personal opinion. The second example in the question more clearly shows intent, which increases the readability of the code, ...
Baldrickk's user avatar
  • 714
12 votes

Should conditional logic be always coded via type system where possible?

You have two good answers already; a third reason to stick with the property is that the pattern you are describing is a "one shot" pattern. Everything goes pear shaped when you add a second Boolean. ...
Eric Lippert's user avatar
  • 45.9k
11 votes
Accepted

Conditional vs Logical Testing

If a Then If b Then If c Then If d Then If e Then x = "Passed" Else x = "Failed" That is some horrendous code right there, and it doesn't do what you think it does either: the 3 "methods" you're ...
Mathieu Guindon's user avatar
10 votes
Accepted

Should conditional logic be always coded via type system where possible?

I think the language choice is irrelevant. What is important in making the decision is how the information is used. If Samples behave differently depending on the value of isRelevent then it ...
Daniel T.'s user avatar
  • 3,033
10 votes
Accepted

Is it good practice to use array.pop() assignment in a while loop condition?

It is not necessarily wrong if the code does what it is supposed to do, however, decisions such as this can be a matter of taste or opinion in personally written code. Conversely, avoiding this ...
Nolo's user avatar
  • 216
9 votes

Best approach - convert multiple conditional if -else in a more handy design

The problem with if/else chains is that they are inflexible and tightly couple that class with any implementations used if any. For a dynamic approach, I would recommend the same solution that I ...
Neil's user avatar
  • 22.7k
9 votes
Accepted

What is the most concise way to test a boolean condition with many AND clauses?

Unit testing is about behaviour of the code under test from the point of view of its clients. Therefore, as your test class is one of its clients, you should define what are your expectations. There'...
Vincent Savard's user avatar
8 votes
Accepted

Enforcing order for two consecutive statements

Anyone who can edit the source code can remove anything that you might put in there to "protect" them. You can do two things: Choose a good name for the function that conveys your intention Provide ...
Hulk's user avatar
  • 508
8 votes

Elegant way to handle multiple pathways based on compound condition

Procedures (or functions or methods) exist because of the need to apply the same pattern of actions in many situations. Thus there's nothing wrong with repeating a method call a few times. Repeating a ...
COME FROM's user avatar
  • 2,676
8 votes

Is it bad practice to add "false or" or "true and" to conditionals?

There are some useful corner cases but your examples do not look particularly meaningful to me. I use it mostly for debugging, if I temporarily want to switch of a block or I want to force myself into ...
Martin Maat's user avatar
  • 18.3k
8 votes
Accepted

Multiple if condition optimization

Some code is inherently complicated. We can shuffle the code around to emphasize one aspect or another, but we can't really simplify it. Your code looks like an example of code that cannot be ...
amon's user avatar
  • 133k
7 votes
Accepted

Do nested conditionals have a significant performance impact?

The sequence of steps that you described appears to be a sequence of steps that you must perform, in order for your application to achieve what it is supposed to achieve. So, there is nothing that ...
Mike Nakis's user avatar
  • 32.1k
7 votes

How do I edit a chain of if-else if statements to adhere to Uncle Bob's Clean Code principles?

Consider using return; after one condition has succeeded, it saves you all the elses. You might even be able to return addAlert(1) directly if that method has a return value.
Kilian Foth's user avatar
6 votes

Is it always possible to separate multiple conditions in an IF statement into individual statements?

Nested conditions along the lines of ((foo or bar) and baz) are hard to comprehend, and I understand that you want to avoid them. Indeed, you can avoid the logical operators and, or and not if you ...
amon's user avatar
  • 133k
6 votes
Accepted

Has any language ever supported a conditional assignment target?

This isn't really a theory question, but a practical one. C++ supports what you're asking about: [C++14: 5.16/4]: If the second and third operands are glvalues of the same value category and have ...
Lightness Races in Orbit's user avatar
6 votes

Should "else" be used in situations where control flow renders it redundant?

I prefer the second option (separate ifs with no else if and early return) BUT that is as long as the code blocks are short. If code blocks are long it's better using else if, because otherwhise you ...
Tulains Córdova's user avatar
6 votes

Developer insists if statements shouldn't have negated conditions, and should always have an else block

There is no hard and fast rule about positive or negative conditions for an IF statement, not to my knowledge. I personally prefer coding for a positive case rather than a negative, where applicable. ...
Langecrew's user avatar
  • 142

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