The tag has no wiki summary.

learn more… | top users | synonyms

91
votes
22answers
5k 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 = ...
58
votes
33answers
11k 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 ...
34
votes
7answers
2k 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.
29
votes
10answers
5k views

Why do we have to use break in switch

Who decided, and basing on what concepts, that switch construction (in many languages) has to be, like it is? Why do we have to use break in each statement? Why do we have to write something like ...
23
votes
3answers
3k 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 ...
12
votes
7answers
790 views

Most readable way to format long if conditions? [closed]

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 ...
12
votes
3answers
578 views

Approaches to 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 ...
10
votes
3answers
626 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: ...
8
votes
10answers
2k 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 ...
8
votes
4answers
840 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... ...
7
votes
5answers
320 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 ...
7
votes
5answers
313 views

Is doing an assignment inside a condition considered a code smell?

Many times I have to write a loop that requires initialization of a loop condition, and an update every time the loop executes. Here's one example: List<String> currentStrings = ...
7
votes
5answers
548 views

What should developers test before submitting their work to testers?

Is there a checklist the developer must go over before passing their work to testers ? Also, what are the conditions/cases the developer must pay attention to ?
7
votes
3answers
1k views

What is the difference (if any) between (null != $object) and ($object != null) when using PHP?

I am used to Java and therefore always think conditions are interpreted from left to right, i.e. there is a vital difference in null != $obj and $obj != null Now this seems not to be the case with ...
6
votes
7answers
1k views

Making Simple IF Statements Shorter

If we assume we have this little snippet of code: string str = "checked"; bool test1; if (str == "checked") { test1 = true; } else { test1 = false; } Is it bad practice to change a simple ...
6
votes
2answers
597 views

Java convention for if/else if order

I have an if/else if structure that on some cases does nothing. From the code I have seen in my career, the "empty" case is normally left out. But when I wrote my code, it just doesn't feel right to ...
5
votes
4answers
705 views

Differences in If… Else… statement

When I first started to learn programming I remember having an argument with my teacher about If Else statements. I was arguing that: if { ... } else if { ... } ... is basically the ...
5
votes
2answers
190 views

Branch twice for unique condition

Consider an application that displays only Foobars belonging to themselves to normal users, and all Foobars to admins: -- Normal users: SELECT * FROM Foobar WHERE user_id=:user_id; -- Admins: SELECT ...
4
votes
6answers
1k 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 ...
4
votes
4answers
425 views

Using a function's return value as an if condition, good practice?

Do you think it is a good practice to use function return values as if conditions? I'm coding in PHP atm but it holds for many other languages. if(isTheConditionMet($maybeSomeParams)) { } or $res ...
4
votes
4answers
169 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 ...
3
votes
1answer
94 views

Getting your user agreement right

I'm planning to provide a little service with which you can control your computer from anywere. It exists out of a server (which I will be providing), and two clients (a controlled one and a ...
3
votes
2answers
127 views

Writing a unit test for a platform dependent unit

I have a method to be tested which depends on the OS, what's the best approach for testing the method depending on the OS? Is it to test on every OS that I have I require? Is there a better approach ...
2
votes
3answers
193 views

Conditional construct for a kleenean data type

I was thinking of an hypothetical programming language with a kleenean data type which would implement Kleene's three-valued logic. To sum up, it's an extension of the boolean data type with the three ...
1
vote
3answers
203 views

The clock problem - to if or not to if?

Let's say, we have a simple digital clock. To "power" it, we use a routine executed every second. We update seconds part in it. But, what about minutes and hours part? What is better / more ...
1
vote
3answers
251 views

What does 'between' and 'in range' mean in the world of numeric types?

Given is an integer with the value 5. Given is a range with start and end values of 2 and 5. So is the integer 5 now between the range? What should a 'InBetween()' method return and which one is the ...
1
vote
3answers
255 views

How to shorten the case statement from hades? [duplicate]

I'm refactoring code and have reached a horribly gigantic switch statement. Every single API method available to end users is represented as an enum and we have a switch statement iterating over the ...
1
vote
1answer
2k views

Condition coverage for If(A && B) - measuring the input or code exercised?

I have been studying condition coverage for last few days. In a book "The Art of Software Testing" they highlight that it does not assure decision coverage, because for example IF(A && B) ...
1
vote
3answers
149 views

Conditional checks against a list

I was wondering how computers do this. The most logical way I can think is that they are iterating trough all elements of the list until they find one that matches the condition :) For example if ...
1
vote
1answer
137 views

Validating data to nest if or not within try and catch

I am validating data, in this case I want one of three ints. I am asking this question, as it is the fundamental principle I'm interested in. This is a basic example, but I am developing best ...
0
votes
2answers
161 views

Boolean-Integer Typecasting to Replace Conditional

When choosing a value based off of 2 boolean values in this format var foo:int; if (X){ foo = 50; } else if (Y){ foo = -50; } else { foo = 0; } I discovered that I can condense this ...
0
votes
3answers
121 views

Choosing the right resources [duplicate]

Once I heard that a good book on any programming language cannot be short, so the number of pages is the first criterium when choosing a tutorial. The statement was half a joke, but it has some truth ...
0
votes
2answers
87 views

ternary operator usage within IF brackets

could you please help me understand this ternary operator public static <E> void replace(List<E> list, E val, E newVal) { for (ListIterator<E> it = list.listIterator(); ...
0
votes
1answer
79 views

Switch / select case etc. compared to If … ElseIf … Else [duplicate]

I don't understand why the switch or equivalent is so popular in languages. To me, it seems like it had a place back in the days when the alternative was lots of nested if statements in the else part ...
0
votes
1answer
69 views

What is “condition testing” (as defined by the ISTQB Syllabus) actually good for?

I am currently checking through the syllabus for the ISTQB "Technical Test Analyst" certification. This syllabus (henceforth called "TTA syllabus") contains a chapter dedicated to "condition testing" ...
0
votes
3answers
375 views

If statements vs switch cases? in a JavaScript game and if to use a function [duplicate]

I am developing a game in JavaScript where you start with a user input, stored in the variable "controller". The options for the user consists of start to start the game or about to learn about the ...
0
votes
4answers
199 views

How to count condition coverage

I am wondering, What would be the correct condition coverage test cases for the following condition: if(A && E && (B || C || D)) Considering short circuiting, what cases would I ...
0
votes
1answer
88 views

Which is preferred coding style to validate and return from a method [duplicate]

Which of the below is a preferred coding style (in c# .net) public void DoWork(Employee employee) { if(employee == null) return; if(!string.IsNullOrEmpty(employee.Name)) return; // Do ...
0
votes
0answers
46 views

Boolean condition before variable [duplicate]

I have noticed this style from time to time: if ( 0 == myVar ) Rather than: if ( myVar == 0 ) Is this just the individual programmers idiom? A defensive programming style? Does anyone know if it ...
-1
votes
1answer
91 views

Automation Approaches: Events/Triggers/Cron [closed]

It has been my experience, when building websites, that most of the logic of a system is executed when user input is accepted, be it via POSTs, GETs etc. I would like to know what processes or ...
-2
votes
3answers
304 views

Should I use AND or should I use OR [closed]

An order can be in the "status" of Completed, Corrected or some other status. I saw some code that is checking it like this, the purpose is to disable some stuff when the status is in Completed or ...
-4
votes
4answers
221 views

Which if statement requires less computation?

I have if (!b && c || a && c) //do action a else //... Due to some internal relationship between a, b, and c. !b && c || a && c is proved to be equivalent to (! ...