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
0votes
5answers
164views
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 ...
24votes
6answers
9kviews
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: ...
-1votes
3answers
995views
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....
4votes
2answers
322views
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 { ...
-1votes
1answer
186views
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 ...
-1votes
1answer
830views
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 ...
1vote
2answers
2kviews
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 ...
0votes
0answers
461views
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)...
0votes
1answer
458views
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....
0votes
1answer
507views
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 ...
0votes
1answer
377views
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 ...
5votes
3answers
12kviews
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 ...
2votes
3answers
530views
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<=...
2votes
4answers
4kviews
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 ...
2votes
2answers
353views
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: ...