Questions tagged [if-statement]
An if statement is a form of conditional statement that enables or disables the execution of a group of statements based on the evaluation of a condition. More elaborate forms include the if-then-else statement and the if-then-elsif statement.
33 questions
0
votes
5
answers
189
views
Shared code block followed by different block for mutually exclusive predicates
What is the best way to express this code? (It was difficult to phrase in words)
if cond1 or cond2:
# shared code
if cond1:
# cond1 code
else:
# cond2 code
The above ...
24
votes
6
answers
9k
views
Best practice for redundant conditions in if-elif-else statements
What is considered better practice?
Case 1:
if n == 0:
doThis()
elif n < 0:
doThat()
elif n > 0:
doSomethingElse()
Case 2:
if n == 0:
doThis()
elif n < 0:
doThat()
else:
...
-1
votes
3
answers
1k
views
Refactoring multiple non-nested if statements [C#]
I have a number of non-nested if statements that look like this:
if (!bytes[nameof(PropertyVersion.Price)].SequenceEqual(dbBytes[nameof(PropertyVersion.Price)])) changes += $"Price {TextHelper....
4
votes
2
answers
364
views
How to structure many complex conditionals on a class
I have a class (as a protobuf) OrderChange, that represents when an order (imagine Amazon.com) changes:
message OrderChange {
Order old_order = 1;
Order new_order = 2;
}
message Order {
...
-1
votes
1
answer
207
views
Optimizing methods with multiple if checks on getter values
I have a method where I fetch user input, check if certain values exist, and based on that build my own custom input object that I would use to search in a database. The code for the search method is ...
-1
votes
1
answer
877
views
rails, how to refactor nest if statements? [duplicate]
I'm working a project where I found another developer wrote a method as you see it below.
How would I clean those IF statements and refactor this method.
Also is it ok ti set a variable to nil ?
def ...
1
vote
2
answers
2k
views
How to effectively handle 404/500 http errors in server-side rendering web application which uses store for state?
I'm working on a website using server-side rendering approach. I'm using React and Redux for this. Redux handles the state of the application and provides API to change the state. All asynchronous ...
0
votes
0
answers
488
views
Parsing custom if statement input in PHP
I'm working on a feature where users can get data based on the if statement they write. The if statement looks something like the excel's conditionals.
Basic syntax:
IF ( lhs == rhs, ifTrue, ifFalse)...
0
votes
1
answer
469
views
Refactor multiple "if" in C#
for (int i = lstReportCount - 1; i >= 0; i--){
if ( (input.ServiceTypes == "1" && lstReport[i].A == 0)
|| (input.ServiceTypes == "2" && lstReport[i].B == 0)
|| (input....
0
votes
1
answer
540
views
What are the pro or cons between a if statement and two different functions [duplicate]
I was wondering what are the pro and cons between having one function with a if statement dictating 2 sections of code or two discreet functions that are called on separately.
In terms of pro and ...
0
votes
1
answer
394
views
If statement best practice [duplicate]
I am reviewing best practice with if statements. Below in example A I include the entire code to be run and ensure it remains within the if statement. While for example B the code to be executed ...
5
votes
3
answers
13k
views
Changing large number of if-elif-else statements to use underlying structure
I have a function that looks something like this:
function_name(step, ... , typ):
if typ == 'some type of calc method':
if step == 1:
do_me_at_step_1(...)
elif step ...
2
votes
3
answers
559
views
Is it good practice to eliminate zero in a statement if possible (e.g.:rewrite a-b>0 into a>b)?
Sometimes, I would write if-statements with conditions like it:
if(a-b>0){
}
but in fact, I can move 'b' from left hand side to right hand side:
if(a>b){
}
Similar cases like 'a-b!=0','a-b<=...
2
votes
4
answers
4k
views
Avoiding if statements in Nested FOR loops
Please pardon me if this is a duplicate question.
I have two nested for loops which will iteration for around mn times (the complexity is around 3k).
Inside these for loops, I have 3 If conditions ...
2
votes
2
answers
379
views
Highlighting importance of order when using short-circuited conditions
I was working on a piece of code when I noticed that an if statement could work or crash depending on the order used for the parts connected with and.
You can replicate the problem like this:
...
2
votes
3
answers
3k
views
Avoiding two if statements for same condition with common code in between [closed]
This is a problem I run into often, and am looking for the best solution. I will have code like this (python):
def func(var, opt):
if opt:
var = var.set_opt(opt)
result = var....
1
vote
3
answers
546
views
If-statement, organize by conditional or effect
A co-worker submitted code for a PR the other day, which effectively follows this flow:
if (a)
do something_a
else if (b)
do something_b
else if (c)
do something_a
where a, b, and c are ...
3
votes
3
answers
483
views
Representing a status as single letter strings [duplicate]
I worry that I'm too concerned with code smells. I've spent the last two days procrastinating over implementation details and how I would actively refuse using the approach suggested.
We have a ...
2
votes
2
answers
3k
views
Long chained method calls contained within an if statement shown on a sequence diagram
Just wondering if there is a good way to do this? Currently i'm performing the method calls as if they were happening prior to the conditional block, then comparing what would be the result in "[...
2
votes
3
answers
427
views
Why doesn't the compiler assume the if statement condition is correct inside it?
First off all: sorry for the title, but I didn't now how to better formulate the meaning of my following question in a single phrase.
While I was writing the following Swift code:
if errorData....
2
votes
2
answers
1k
views
how to program a lookup table of input and output variables
This is a small C++ application (and the table is not huge either) so if possible I would like to avoid include anything other than STL (and boost).
So to the problem:
I have a table of predefined ...
2
votes
2
answers
32k
views
IF statement with OR logical operator
Just a basic question on IF statements in programming languages, specifically C++. Consider the following basic code example:
int i = 2;
if(i == 2 || i == 4) {
//do something
}
Because the first ...
1
vote
1
answer
144
views
Using expressions instead of statements
In javascript you can't use statements in expressions. Because of this you are forced to use logic and ternary operators in an expression when you want to use if or switch etc.. Since I didn't want to ...
1
vote
3
answers
8k
views
In C, why is NULL and 0 triggering an if statement
I have a function that is called by
myStruct *structName = myFunction(0);
The function looks like
myStruct *myfunction(int x)
{
if ( x == NULL)
{
return NULL;
}
/*rest of code*...
7
votes
6
answers
1k
views
Should an Else statement be used just for a comment?
I came across the following in some sample code:
if (urlStr)
{
NSImage *iconImage = [[NSWorkspace sharedWorkspace] iconForFile:urlStr];
node.nodeIcon = iconImage;
}
else
{
// it's a ...
0
votes
3
answers
188
views
Eliminating the bad cases in if to get a nicer code
I have read this link Should I return from a function early or use an if statement? and it triggered a conflict in my head. I agree that it looks nicer and cleaner and I guess that would be the way I ...
4
votes
1
answer
2k
views
Is it always possible to separate multiple conditions in an IF statement into individual statements?
I'm trying to find the simplest way to model user-defined conditional statements without resorting to text parsing. This is fairly easy when there is only one condition in the statement because you ...
6
votes
2
answers
14k
views
Do nested conditionals have a significant performance impact? [duplicate]
This is a question that lives in my mind from a long time.
Does the use of multiple nested conditional statements impact the performance of a taken code? So far I know that programmers have created a ...
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 ...
2
votes
4
answers
3k
views
Overwriting and returning the value of the argument used as conditional of an if statement, inside the same if statement
A simplified example:
function logTheColor (color){
if(color == "red"){
color = "The color is red "
} else if (color == "yellow") {
color = "The color is yellow "
} else {
...
0
votes
4
answers
6k
views
What if(event) statement means in JavaScript? [closed]
I'm rather new to JavaScript and programming in general so I am pretty much only used to seeing if statements that have some kind of comparison operator like, if (x < 10) or if(myBool).
I have ...
4
votes
2
answers
5k
views
In an if statement, what are an "if clause" and a "then clause"?
I am a bit confused about the nomenclature for the parts of an if statement. Consider the following example:
1: if condition then
2: statement_1;
3: else
4: statement_2;
5: end if;
What ...
20
votes
3
answers
44k
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?