if-statement

How do you handle huge if-conditions?

It's something that's bugged me in every language I've used, I have an if statement but the conditional part has so many checks that I have to split it over multiple lines, use a nested if statement or just accept that it's ugly and move on with my life. Are there any other methods that you've found that might be of use to me and anybod...

IIf() vs. If

In Visual Basic, is there a performance difference when using the IIf function instead of the If statement? ...

If, IIf() and If()

I recently asked a question about IIf vs. If and found out that there is another function in VB called If which basically does the same thing as IIf but is a short-circuit. Does this If function perform better than the IIf function? Does the If statement trump the If and IIf functions? ...

Shorthand conditional in C# similar to SQL 'in' keyword

In C# is there a shorthand way to write this: public static bool IsAllowed(int userID) { return (userID == Personnel.JohnDoe || userID == Personnel.JaneDoe ...); } Like: public static bool IsAllowed(int userID) { return (userID in Personnel.JohnDoe, Personnel.JaneDoe ...); } I know I could also use switch, but there are pro...

Formatting of if Statements

This isn't a holy war, this isn't a question of "which is better". What are the pros of using the following format for single statement if blocks. if (x) print "x is true"; if(x) print "x is true"; As opposed to if (x) { print "x is true"; } if(x) { print "x is true"; } If you format your single statement ifs without...

Need refactoring ideas for Arrow Anti-Pattern

I have inherited a monster. It is masquerading as a .NET 1.1 application processes text files that conform to Healthcare Claim Payment (ANSI 835) standards, but it's a monster. The information being processed relates to healthcare claims, EOBs, and reimbursements. These files consist of records that have an identifier in the first few...

#ifdef vs #if - which is better/safer?

This may be a matter of style, but there's a bit of a divide in our dev team and I wondered if anyone else had any ideas on the matter... Basically, we have some debug print statements which we turn off during normal development. Personally I prefer to do the following: \\---- SomeSourceFile.cpp ---- #define DEBUG_ENABLED (0) ... So...

Pro/con: Initializing a variable in a conditional statement

In C++ you can initialize a variable in an if statement, like so: if (CThing* pThing = GetThing()) { } Why would one consider this bad or good style? What are the benefits and disadvantages? Personally i like this style because it limits the scope of the pThing variable, so it can never be used accidentally when it is NULL. However, ...

How often do you worry about how many if cases will need to be processed?

If you have the following: $var = 3; // we'll say it's set to 3 for this example if ($var == 4) { // do something } else if ($var == 5) { // do something } else if ($var == 2) { // do something } else if ($var == 3) { // do something } else { // do something } If say 80% of the time $var is 3, do you worry about th...

Python style: multiple-line conditions in IFs

Hello, Sometimes I break long conditions in IFs to several lines. The most obvious way to do this is: if (cond1 == 'val1' and cond2 == 'val2' and cond3 == 'val3' and cond4 == 'val4'): do_something Isn't very very appealing visually, because the action blends with the conditions. However, it is the natural way using corr...

switch case vs if else

I was wondering if there was any difference in the way the following code was compiled into assembly. I've heard that switch-case is more efficient than if else, but in this example I am not quite sure if that would be the case. if(x==1){ ... }else if(x==2){ ... }else{ ... } and switch(x){ case 1: ... break; case 2: ... break;...

Best way to format if statement with multiple conditions.

If you want to some code to execute based on two or more conditions which is the best way to format that if statement ? first example:- if(ConditionOne && ConditionTwo && ConditionThree) { Code to execute } Second example:- if(ConditionOne) { if(ConditionTwo ) { if(ConditionThree) { Code to execute } ...

Is "IF" expensive?

I can't, for the life of me, remember what exactly did our teacher said that day and I'm hoping you would probably know. The module is "Data Structures and Algorithms" and he told us something along the lines of: The if statement is the most expensive [something]. [something] registers [something]. Yes, I do have a horrible me...

SQL if statement in where clause for searching database

I'm creating a stored procedure to return search results where some of the parameters are optional. I want an "if statement" in my where clause but can't get it working. The where clause should filter by only the non-null parameters. Here's the sp ALTER PROCEDURE spVillaGet -- Add the parameters for the stored procedure here @accomod...

Which is faster: in_array() or a bunch of expressions in PHP?

Is it faster to do the following: if ($var != 'test1' && $var != 'test2' && $var != 'test3' && $var != 'test4') { ... } Or: if (!in_array($var, array('test1', 'test2', 'test3', 'test4') { ... } Is there a number of values at which point it's faster to do one or the other? (In this case, the array used in the second option doesn'...

Which SQL statement is faster? (HAVING vs. WHERE...)

SELECT NR_DZIALU, COUNT (NR_DZIALU) AS LICZ_PRAC_DZIALU FROM PRACOWNICY GROUP BY NR_DZIALU HAVING NR_DZIALU = 30 or SELECT NR_DZIALU, COUNT (NR_DZIALU) AS LICZ_PRAC_DZIALU FROM PRACOWNICY WHERE NR_DZIALU = 30 GROUP BY NR_DZIALU ...

Thorough use of 'if' statements or 'try/catch' blocks?

Give me some of your thoughts on which is a better coding practice/makes more efficient code/looks prettier/whatever: Increasing and improving your ability to use if statements to anticipate and catch potential problems? Or simply making good use of try/catch in general? Let's say this is for Java (if it matters). Edit: I'm presentl...

VB.NET If Statement and the Colon

Here is an interesting piece of code that my fellow team members were just having a slightly heated discussion about... Dim fred As Integer If True Then fred = 5 : fred = 3 : fred = 6 Else fred = 4 : fred = 2 : fred = 1 After executing the above code snippet, what is the value of fred? Try not to cheat and debug the code. This ...

How to define output depending on an variable with an status in LaTeX?

I have some standard-texts, but some portion of it is different. But of these different parts only a few exists. For instance I want: \mytext{...}{a} \mytext{...}{b} That produces: \section{Item: ...}\label{item...} This is a standard item. Items of type a are very precious. \section{Item: ...}\label{item...} This is a standard it...

If vs. Switch Speed

Switch statements are typically faster than equivalent if-else-if statements (as e.g. descibed in this article) due to compiler optimizations. How does this optimization actually work? Does anyone have a good explanation? ...