The tag has no usage guidance.

learn more… | top users | synonyms

0
votes
1answer
74 views

Rewriting conditionals in OOP without generics

Let's say I have a set of objects, foo f; bar br; baz bz; I also have a string of JSON data, string JSONstring; Depending on the object type of the JSON string, I need to transform it into ...
10
votes
3answers
389 views

Is it better to guard the method call or the method itself?

I am writing an application and I got to this point: private void SomeMethod() { if (Settings.GiveApples) { GiveApples(); } if (Settings.GiveBananas) { ...
2
votes
3answers
138 views

Enforcing order for two consecutive statements

I have the following code: subroutine foo(int index) { // Check A. // Critical: Check A must precede Check B below. if (index == 1) { return true; } // Check B. ...
2
votes
4answers
234 views

How to properly reverse the if statement when you have two conditions in it?

In PHP I have this if statement ( $first and $second will evaluate to true or false): if ($first && $second) { // evereything is OK } else { throw new Exception()... } My real code ...
2
votes
8answers
220 views

Best way to deal with two independent booleans [duplicate]

Is there a better way to write the following code? if a { if b { print "a and b" } else { print "just a" } else { if b { print "just b" } else { print "neither a nor ...
2
votes
2answers
163 views

Has any language ever supported a conditional assignment target? [closed]

I've never seen a programming language with conditional assignment targets, eg.: // If (x == y), then var1 will be set to 1, else var2 will be set to 1 ((x == y) ? var1 : var2) = 1 The target of ...
-1
votes
2answers
80 views

Whitelisting and Blacklisting with Condition [closed]

I currently have the issue, that I have an application that filters out objects by blacklisting. For example: Filter: price<4&&name=salzburg An object is inserted that has price 3 and ...
2
votes
3answers
315 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 ...
1
vote
1answer
150 views

How harmful is using switch(true) as a way to handle multiple conditional statements? [duplicate]

I recently saw something like the following: switch(true) { case iAmAmazing(): { return 'some html'; } case isVariable: { return 'some other html'; } case ...
0
votes
0answers
64 views

Is there a school of thought where while(1) or while (true) is a code smell? [duplicate]

My employer requires code reviews for all devs, and new employees are required to to have their code reviewed by a senior dev. Initially, trainees get to pick a small project they are working on and ...
0
votes
3answers
144 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 ...
3
votes
1answer
137 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 ...
1
vote
2answers
174 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 ...
3
votes
4answers
152 views

Elegant way to handle multiple pathways based on compound condition [closed]

Consider the following: if (x == 5 || x == 10 || x == 12) { if (x == 5) { doSomething() } else if (x == 10) { doSomethingElse() } else { doSomeOtherThing() } ...
0
votes
2answers
118 views

Why does presence of value evaluate as “Truthy” in Python? [e.i. Types evaluate True in conditionals]

Example: if float: print('float is true?') returns: Why do all objects other than ones explicitly defined as False or "False" evaluate truthfully?
2
votes
2answers
151 views

Refactoring wordy conditional tests [duplicate]

My first attempt at this question was too theoretical, so I've rewritten it with actual code. See the edit history if you care. Supposing this logic, "suffering" from the arrow anti-pattern: /** ...
1
vote
4answers
143 views

Better way of handling pre conditions and logging

1. Imagine you have the following: void Foo::doFoo() { if (!isConditionValid()) { log("doFoo not possible because condition is not valid"); return; } if ...
2
votes
0answers
141 views

What's the best pattern to validate a conditional entity? [duplicate]

I'm developing an ASP.NET MVC 5 website and I have a view with a grid. In this grid I have a column which represents an URL property. This url is a link to download some files, but this url should be ...
-1
votes
3answers
84 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); ...
3
votes
2answers
140 views

What is a proper way to chain instructions if previous one is ok [duplicate]

I'm currently working on a big file I have to parse and process and each step needs to be done in an order as I do SQL queries and need inserted IDs to make other insertions ... The problem is I often ...
0
votes
1answer
110 views

Code Inside Catch Block

This question is related to both of these questions - Efficient try / catch block usage? and Dealing with error in data - Idempotent approach. When I encounter a void while reading a GIS data file I ...
0
votes
2answers
340 views

What algorithm should I use for a lot of if else(s)?

I have to implement a lot of if else statements and I'm thinking of using Decision Tree but I'm not sure if it's going to be the right algorithm. I don't want to insert a piece of html code based on ...
15
votes
5answers
2k 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
1answer
108 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 && ...
0
votes
3answers
160 views

Using conditions again and again or just create different methods [duplicate]

I have a method, function DoThis(bool isHuman) { // Do something if(isHuman) { //call his dad } else { //its a animal - let animal resuce know } // add some blah blah code ...
3
votes
1answer
305 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 ...
0
votes
2answers
301 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
3answers
334 views

Ifology - how to write this statement better? [closed]

I'm wondering how to write the if statement in the following block in a better way. It's supposed to operate when $a is 14, 22, 30 and for all following values at intervals of 8, up to some limit. The ...
2
votes
2answers
590 views

What is the difference between if('' == $variable) and if($variable == '') [duplicate]

While using an 'if' statement to check whether a variable is an empty string or not we can write it in two ways i.e. if('' == $variable) and if($variable == ''). I want to know what is the impact of ...
0
votes
1answer
218 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 ...
4
votes
2answers
386 views

How to layer if statements when order of logic is irrelevant?

Basically I have a series of logic in my website that can lead to 5 total outcomes. Basically two different if tests and then a catch all else statement. For example: if cond1: if mod1: ...
-1
votes
2answers
652 views

Confusion in my If Else ,Else If Condition. in C# [closed]

I have three column as follows: Month Tech Circle According to this column I need to fetch the data. In Month Column, Data is : Jan,feb, March... and so on. In Tech Column, Data is : Gsmnqi, ...
1
vote
2answers
129 views

Apply filter only if not null

My function takes an optional parameter, type, which is used to filter through a collection. However, the filter should only apply if the parameter is passed in (in other words, non-null). If the ...
0
votes
2answers
556 views

Why elseif instead of else if?

Few days ago I stopped on some PHP's developers quide (for contributors to particular project) and it stated, that elseif must be strictly used instead of else if -- without giving any reason, why? ...
1
vote
2answers
2k views

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

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" ...
7
votes
5answers
520 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 = ...
5
votes
2answers
221 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 ...
0
votes
1answer
194 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 ...
1
vote
3answers
374 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 ...
4
votes
2answers
730 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 ...
1
vote
3answers
6k 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 ...
9
votes
8answers
5k 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 ...
6
votes
5answers
515 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
4answers
843 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 ...
0
votes
1answer
108 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 ...
1
vote
3answers
217 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
1answer
148 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
3answers
132 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 ...
6
votes
2answers
2k 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 ...
0
votes
0answers
50 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 ...