Questions tagged [conditions]
127 questions
302
votes
19 answers
Should I return from a function early or use an if statement?
I've often written this sort of function in both formats, and I was wondering if one format is preferred over another, and why.
public void SomeFunction(bool someCondition)
{
if (someCondition)
{
// Do Something
}
}
or
public…

Rachel
- 23,979
- 16
- 91
- 159
185
votes
15 answers
Developer insists if statements shouldn't have negated conditions, and should always have an else block
I have an acquaintance, a more seasoned developer than me.
We were talking about programming practices and I was taken aback by his approach on 'if' statements.
He insists on some practices regarding if statements that I find rather…

Patsuan
- 1,627
- 2
- 9
- 10
174
votes
24 answers
Elegant ways to handle if(if else) else
This is a minor niggle, but every time I have to code something like this, the repetition bothers me, but I'm not sure that any of the solutions aren't worse.
if(FileExists(file))
{
contents = OpenFile(file); // <-- prevents inclusion in if
…

Benjol
- 3,747
- 5
- 33
- 41
89
votes
25 answers
Ternary operator considered harmful?
For example, would you prefer this one-liner
int median(int a, int b, int c) {
return (a

fredoverflow
- 6,854
- 8
- 39
- 46
85
votes
11 answers
Why do we have to use break in switch?
Who decided (and based on what concepts) that switch construction (in many languages) has to use break in each statement?
Why do we have to write something like this:
switch(a)
{
case 1:
result = 'one';
break;
case 2:
…

trejder
- 2,386
- 3
- 19
- 39
69
votes
17 answers
Why Use !boolean_variable Over boolean_variable == false
A comment on this question: Checking if a method returns false: assign result to temporary variable, or put method invocation directly in conditional? says that you should use !boolean instead of boolean == false when testing conditions. Why? To me…

ell
- 966
- 1
- 10
- 14
56
votes
6 answers
Most readable way to format long if conditions?
Long winding if conditions should be avoided if at all possible, yet sometimes we all end up writing them. Even if it's a very simple condition, the involved statements are sometimes simply very wordy, so the whole condition ends up being very…

deceze
- 2,215
- 1
- 19
- 19
55
votes
5 answers
if ('constant' == $variable) vs. if ($variable == 'constant')
Lately, I've been working a lot in PHP and specifically within the WordPress framework. I'm noticing a lot of code in the form of:
if ( 1 == $options['postlink'] )
Where I would have expected to see:
if ( $options['postlink'] == 1 )
Is this a…

Tom Auger
- 847
- 1
- 7
- 10
55
votes
11 answers
How would you refactor nested IF Statements?
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 must come to the conclusion that there are situations…

saunderl
- 655
- 1
- 6
- 7
50
votes
6 answers
Clarification of "avoid if-else" advice
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 advice confuses me. Do they say that I should not…

deviDave
- 2,933
- 5
- 26
- 30
46
votes
13 answers
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 (checkCondition2()) {addAlert(2);}
else if…

Ev0oD
- 559
- 1
- 4
- 8
35
votes
7 answers
How can I reformat my condition to make it better?
I have a condition
if(exists && !isDirectory || !exists)
{}
how can I modify it, so that it may be more understandable.

Spynet
- 459
- 6
- 9
33
votes
3 answers
How to tackle a 'branched' arrow head anti-pattern?
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(fooSuccess==true&&barSuccess!=true){
…

AncientSwordRage
- 1,033
- 1
- 11
- 24
31
votes
3 answers
Approaches for checking multiple conditions?
What is the best practice for checking multiple conditions, in no particular order?
The example in question needs to check four distinct conditions, in any order, and fail showing the correct error message.
The examples below use a C-like…

Redandwhite
- 419
- 1
- 4
- 6
20
votes
7 answers
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 -1;
}
else if (check2(value)) {
return…

rhino
- 357
- 1
- 7