Questions tagged [conditions]
The conditions tag has no usage guidance.
28
questions
33
votes
3
answers
56k
views
Approaches for checking multiple conditions? [duplicate]
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 ...
32
votes
3
answers
10k
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(...
172
votes
24
answers
108k
views
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 = ...
302
votes
19
answers
204k
views
Should I return from a function early or use an if statement? [closed]
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)
...
46
votes
6
answers
89k
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 ...
52
votes
5
answers
20k
views
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:...
1
vote
1
answer
885
views
Avoid Code Repetition in Condition Statements [duplicate]
I have been programming for over 15 years now. I consider myself a very good programmer, but I understand (like all of us) there are things that I need to work on. One of these things is code ...
17
votes
3
answers
35k
views
What is "short-circuiting" in C like languages?
I have heard of the term "short-circuiting" being used in C, C++, C#, Java, and many others. What does this mean and in what scenario would it be used?
47
votes
12
answers
133k
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 ...
80
votes
11
answers
108k
views
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:
...
66
votes
17
answers
28k
views
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 ...
88
votes
25
answers
46k
views
Ternary operator considered harmful? [closed]
For example, would you prefer this one-liner
int median(int a, int b, int c) {
return (a<b) ? (b<c) ? b : (a<c) ? c : a : (a<c) ? a : (b<c) ? c : b;
}
or an if/else solution ...
5
votes
3
answers
930
views
Should conditional logic be always coded via type system where possible?
I have a choice.
Option 1:
public class Sample
{
bool IsRelevant { get; set; }
}
Option 2:
public class Sample
{
}
public class RelevantSample : Sample
{
}
Is there a clear well-known rule ...
11
votes
4
answers
5k
views
Is the use of explicit ' == true' comparison always bad? [duplicate]
Possible Duplicate:
Make a big deal out of == true?
I've been looking at a lot of code samples recently, and I keep noticing the use of...
if( expression == true )
// do something...
and...
...
13
votes
8
answers
23k
views
How to do a clean refactoring of an If Else Code without leaving any free blocks? [duplicate]
if(condition1)
{
Statement1A;
Statement1B;
}
else if(condition2)
{
Statement2;
}
else if(condition3)
{
Statement3;
}
else
{
Statement1A;
Statement1B;
}
return;
I would like to ...
5
votes
6
answers
9k
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 ...
0
votes
3
answers
1k
views
When not to use early return? [duplicate]
Nesting is unavoidable, however in most cases returning early is a more viable option.
Consider the following snippet:
MyRoutine(argument)
{
if (0 == argument) {
SubRoutine(argument);
...
45
votes
13
answers
17k
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 (...
15
votes
5
answers
5k
views
If Else - Repeated Code Logic
My boss gave me a project with a particular logic. I have to develop a web page which has to lead the navigator through many cases until he/she arrives at the product.
This is the path scheme of the ...
13
votes
5
answers
6k
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
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 ...
35
votes
7
answers
3k
views
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.
2
votes
1
answer
684
views
Proper way to refactor multiple if based conditions [duplicate]
I took over a large legacy code base. It has a code like this:
if ($route == 'login' || $route == 'logout' || $route == 'forgot-password') {
return;
}
if ($loggedInUser == false && $...
1
vote
2
answers
1k
views
How to avoid spaghetti code when I have a lot of conditions? [duplicate]
I have the following problem:
An user can withdraw money from 2 payment systems (but the number of payment systems can change anytime in the future).
If user has a trusted account on either of ...
4
votes
4
answers
512
views
Should I repeat condition checking code or put it in a function? [duplicate]
I have a bunch of calls to a method that may or may not need to be made depending on whether certain features are enabled or not. As such, I've wrapped these calls in if blocks checking the enabled ...
4
votes
2
answers
3k
views
Store conditional expression in database
We have an application that allows users to enter conditionals in the form bound op x op bound2, we store this as a string, and then parse it at runtime to evaluate it.
It is a decent amount of work, ...
3
votes
2
answers
193
views
Does removing unused features from libraries through compiler flags increase or reduce security risks?
Software libraries targetting resource constrained environments like embedded systems use conditional compilation to allow consumers to shave space and thus increase performance by removing unused ...
-2
votes
2
answers
289
views
Conditional jump statements in middle- and high-level languages
In Java, C, and C++ we have the following jump statements: break, continue, goto, and return. In C#, there is also throw.
I'm not really familiar with either of these languages. This is simply what I ...