Questions tagged [conditions]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
53
votes
5answers
19k 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 see:...
2
votes
1answer
242 views

What is the name of this pattern / style?

We have a "workflow orchestration" system at work. It works something like this: You configure what to run (in a database table), such as: NameOfStepATHingToRun ="weather_data" ...
3
votes
0answers
95 views

Refactoring nested if-else statement [duplicate]

I have an app where every object is checked based on various types of business rules. For this, i used multiple nested if-else statement which was done in one class. I am not happy with this situation ...
5
votes
3answers
3k 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 ...
0
votes
1answer
271 views

Why does a condition variable's wait() release the associated mutex before blocking and reacquire it before returning?

Stallings' Operating System book says about condition variable in Solaris, A condition variable is used to wait until a particular condition is true. Condition variables must be used in ...
-2
votes
1answer
111 views

Refactoring nested if-else interface method in Java8

I have the below default method in an interface and it seems to be pretty complex because of the many if-else conditions. default void validate() { Application application = application().get(); ...
1
vote
5answers
232 views

When to extract boolean conditions into their own function?

I commonly use a boolean condition/variable when it's something too big or complicated and takes too much space by itself inside ifs - basically to avoid repeatability and thus improve readability. E....
-2
votes
1answer
102 views

Conditional command pattern

I have an abstraction that defines something like a command pattern, interface Participant { void proceed(); } Participants are grouped in a collection and are called all together. But each ...
0
votes
3answers
199 views

Is there an elegant way to structure consecutive short-circuited assignments?

The following code works and is clear, but it's also verbose. I suspect that there's a way to make it more terse, so that it could be skimmed quickly and it'd be more obvious what's happening. // The ...
3
votes
5answers
217 views

Dealing with unwanted usages of a function

I am writing a function that I would not like to get called given a certain context and am wondering how best to convey that to possible users of the function. Assume, for exemplification, I am ...
3
votes
1answer
121 views

Is there a pattern for choosing one of a set of options at random when their selection criteria are equal?

I want to choose between several options, and if the criteria I'm using for selection happen to be equal, I want one at random (reasonably so, so it's equal-chance each time, rather than arbitrary). ...
-2
votes
2answers
240 views

Conditional jump statements in middle- and high-level languages

In Java, C, and C++ we have the following jump statements: break, continue, goto, and return. In C#, there is also throw. I'm not really familiar with either of these languages. This is simply what I ...
8
votes
3answers
694 views

Is it bad practice to add “false or” or “true and” to conditionals?

Is it bad practice to add false or ... or true and ... for the sake of promoting code genericness and/or ease of use? As in: SELECT * FROM table WHERE TRUE AND IsEnabled AND SomeField = some_value ...
3
votes
2answers
172 views

Does removing unused features from libraries through compiler flags increase or reduce security risks?

Software libraries targetting resource constrained environments like embedded systems use conditional compilation to allow consumers to shave space and thus increase performance by removing unused ...
7
votes
2answers
215 views

How does conditional compilation impact product quality, security and code complexity? [closed]

Software libraries targetting resource constrained environments like embedded systems use conditional compilation to allow consumers to shave space by removing unused features from the final binaries ...
0
votes
1answer
55 views

Analyse/show user why data has been filtered out by SQL

Sorry for the bad title. Couldn't really think of a good name without an explanation. In our system (source is inside Oracle PL/SQL packages) we have quite a lot large SQL queries with a sh..load of ...
65
votes
16answers
24k views

Why Use !boolean_variable Over boolean_variable == false

A comment on this question: Checking if a method returns false: assign result to temporary variable, or put method invocation directly in conditional? says that you should use !boolean instead of ...
1
vote
1answer
70 views

Using define in a conditional expression in Scheme

In Scheme, the general form of a procedure definition is: (define (<name> <parameters>) <body>) where <body> accepts a sequence of expressions, allowing this kind of procedure ...
2
votes
2answers
155 views

Should special case be inside or outside the for loop here?

For example, suppose I have 2 arrays: let arr1=[5,2,1]; let arr2=["abcde","ab","a"]; my work is simple : to check if length of strings in arr2 are larger than corresponding element with same index ...
2
votes
4answers
387 views

Conditional vs Logical Testing

I would like to get your code thought and views on using conditional vs logical testing. For example: To test the conditions of truthness of all of the following variables, their currect status is ...
10
votes
5answers
550 views

If-else ladder that is supposed to catch all conditions - should a redundant final clause be added?

This is a thing I'm doing a lot lately. Example: setCircle(circle, i, { current }) { if (i == current) { circle.src = 'images/25CE.svg' circle.alt = 'Now picking' } else if (...
2
votes
2answers
224 views

API Race conditions

I have an API in which we expect a emailID and a certain deal data. The case is we want to keep the dealId and contactId(email) to be unique. Creating a deal requires data insertion in multiple ...
77
votes
11answers
105k views

Why do we have to use break in switch?

Who decided (and based on what concepts) that switch construction (in many languages) has to use break in each statement? Why do we have to write something like this: switch(a) { case 1: ...
1
vote
2answers
366 views

Avoid if-else block in favor of default assignment followed by if

A colleague, and frankly better software engineer that me, is telling me that this pattern let variable = someDefaultVariable(); if (some_boolean) { variable = someOtherValue(); } is better than ...
2
votes
3answers
618 views

Reconciling MVC with a strategy pattern

I am working on a Rails application which employs a classic MVC as its fundamental structure. In that structure the controller is supposed to be responsible for "which view to render when". Now after ...
2
votes
1answer
220 views

Dealing with conditional verification logic using guard assertion

I have parametrized test with 2 variants: NULL value and any NOT NULL value From this value is depends one assert: In case NULL it should be checked if object has field1 with value1 In case ...
42
votes
6answers
83k 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 ...
20
votes
7answers
2k views

Should “else” be used in situations where control flow renders it redundant?

I sometimes stumble upon code similar to the following example (what this function does exactly is out of the scope of this question): function doSomething(value) { if (check1(value)) { return -...
3
votes
4answers
4k views

What is the most concise way to test a boolean condition with many AND clauses?

I have a function which returns the availability of a module, where the module is only available if multiple conditions are all met. The code looks like this: bool isShipAvailable() { bool ...
0
votes
3answers
124 views

Approaches for not complex conditions

I encountered code like this below. I was told its fine, and boilerplate is not always better. I agree boilerplate might be bad, but I am not sure about something like this: if (entity....
-1
votes
1answer
163 views

Enforcing IF, ELSEIF and ELSE on collection of ojects

I am designing a project where I have to enforce IF, ELSEIF and ELSE conditions on collection of objects. For example: public class Box { private List<Item> items; } I want to define IF ...
5
votes
6answers
7k views

Best approach - convert multiple conditional if -else in a more handy design

I have a class that handles the state of a response, called StockResponse. The code has multiple ifs to handle each state of the stock. Most of the cases has a default behaviour, but some conditions ...
1
vote
1answer
695 views

How to compute cyclomatic complexity for empty whiles and if + breaks?

This is closely related to this question which asks about the complexity of the following: while (x > level) x = x – 1; x = 0 Using the graph method it has a Complexity = 2. Fred Swartz ...
3
votes
3answers
681 views

Best practice to avoid partial branches

I do a lot of coding in python and got a lot of if conditions without an else statement so to say partial branches. E.g.: # if a certain kwarg was passed to a function call if kwargs.get('a_option'):...
0
votes
3answers
745 views

Which is more readable: early returns, or conditionals? [duplicate]

I’m writing an asynchronous, Promise-returning function. In the processing I test some conditions, and if the conditions pass, the promise should be fulfilled (resolved), but if one fails, then the ...
45
votes
13answers
15k views

How do I edit a chain of if-else if statements to adhere to Uncle Bob's Clean Code principles?

I'm trying to follow Uncle Bob's clean code suggestions and specifically to keep methods short. I find myself unable to shorten this logic though: if (checkCondition()) {addAlert(1);} else if (...
0
votes
1answer
92 views

Multi-dimensional if-elseif-else block to call different functions

I have a function a plugin that is called whenever the WebAPI's "Plugin" endpoint is called in the main project, and that has to process the HTTP request. The request holds more information about what ...
31
votes
3answers
52k 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 ...
3
votes
3answers
510 views

Does if-else optimization in PHP have any effect?

Consider a scenario, where A occurs frequently like 4 out 5 times, and B only in rare cases, we can write our conditional statement in two ways: if(A) //do something else // do something if B, only ...
3
votes
2answers
12k views

PHP : Better way to print html in if-else conditions

Please refer the following two forms of the same code First: <?php if(some_condition){ ?> <li><a target="_blank" href="example.com/channel1/" class="xyz">If content</a>&...
12
votes
5answers
5k 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 ...
169
votes
24answers
99k 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 = ...
302
votes
19answers
185k views

Should I return from a function early or use an if statement? [closed]

I've often written this sort of function in both formats, and I was wondering if one format is preferred over another, and why. public void SomeFunction(bool someCondition) { if (someCondition) ...
176
votes
15answers
61k views

Developer insists if statements shouldn't have negated conditions, and should always have an else block

I have an acquaintance, a more seasoned developer than me. We were talking about programming practices and I was taken aback by his approach on 'if' statements. He insists on some practices regarding ...
2
votes
2answers
2k 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 "[...
1
vote
1answer
99 views

How do I implement a run-only-once method when all criteria are met for the first time run it?

I need a run-only-once method that when all the criteria are met, say A&B&C are true, run the codes once, but only once. So if later on all the criteria are met again (A&B&C are true ...
7
votes
5answers
4k 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 = ...
3
votes
2answers
2k views

Store conditional expression in database

We have an application that allows users to enter conditionals in the form bound op x op bound2, we store this as a string, and then parse it at runtime to evaluate it. It is a decent amount of work, ...
4
votes
4answers
230 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() } ...
8
votes
2answers
6k views

Is it good practice to use array.pop() assignment in a while loop condition?

Just saw a code snippet that used an array pop assignment in the while condition; was wondering if this is acceptable/good practice? var arr = [0,1,2,3,4,5]; var current; while (current = arr.pop()) {...