Do you usually set the default value earlier or set it in another place?

Which of the following actions do you perform:

var = true; if (...) var = false; 

or

 if (...) var = false; else var = true; 

Is there a reason you choose one or the other?

I am working on the premise that nothing else happens with var. The following line of code might look something like this:

 if (var) { ... } 
+6
optimization
source share
13 answers

how about var = {...} directly since it is boolean?

+9
source share

I prefer the latter in Java, doing something like this:

 int x; if (cond) { x = 1; } else { x = 5; } 

because if something changes later (for example, I turn the else block into else if ), the compiler will tell me that the variable was not initialized, which I can skip if I used your first strategy.

+5
source share

You can also use the ternary operator if your language supports it :)

Usually I would only do the first if there was a chance that the IF could fail, and the variable should have a default value, if any.

+3
source share

If you set the default value, then you reset again visited something else, although this is a very small amount, but it is still a waste of resources. Therefore, most of the time for most of the code, balanced if / else or even syntax (? :) is clearer and more appropriate, with the exception of:

Sometimes, if what you are doing creates an end-to-end code (or decision function) where you start with a certain condition and then check a bunch of other conditions to see if that changes, then you want to definitely set the default value first :

 int final = 27; if ( some condition ) final = 86; if ( another condition ) { final = 98; return final; } if ( some state ) { final += 2; } return final; 

Or something like that.

BTW: in your example, if you install "var", then the next line just checks for "var", do you really not need "var"? If the condition is so ugly that using "var" helps make it readable, then it is probably best to move the condition to its own function, considering that an extra function call is necessary for reading. In general, you can spend resources if and only if you get something meaningful, such as readability, in return.

Pavel.

+2
source share

It depends on the context. I would use the second option when it became clear that "var" should be true when IF is not working.

+1
source share

I use the first type if the value for the job does not require significant calculations.

+1
source share

Always the first, as many have said. However, it is worth emphasizing why, and this is because it makes the program more resistant to future errors caused by improper maintenance.

For example, this is quite common for any additional business condition to arise, and the service coder adds some additional conditions or two inside the if to enable more business logic and incorrectly change the code - for example

  if (a == b) {
   if (a == c) {
     [new logic]
     var = false
   }
 }
 else {
   var = false
 }

At first glance, this seems unlikely, but it happens quite often (rightly often the situation arises after the original, if it was much more complicated). Initialization prevents this from happening first.

+1
source share

Do you prefer short and compact code or code that is easier to read?

If you prefer short and compact code

 var x = true; if (...) x = false; 

But it can even be "improved." Most languages โ€‹โ€‹set initial values, and usually the default value for the Boolean type is false. So you can write

 var x; if (...) x = true; 

If you like code that is easy to read, use

 if (...) var x = false; else var x = true; 

because it makes your intentions understandable.

The performance is the same.

0
source share

It depends on the language. In C ++, I highly recommend setting it to default as quickly as possible, otherwise you run the risk of getting garbage results.

In most other languages, you can be a little more flexible. In fact, I would say that it is more readable for explicitly defining conditions than for a default installation.

0
source share

Since the variable is not written later, for general values, I would write the following in Java:

 final Type var; if (cond) { var = value1; } else { var = value2; } 

The Java compiler will catch an error that var has not been assigned a value before using it. The last keyword expresses the fact that the variable is constant after the conditional.

In your particular case with boolean values, I would use

 final boolean var = !cond; 

Using a conditional expression in this case indicates that you are suffering from "booleanophobia."


In C, I would initialize the variable in its declaration.

0
source share

I usually set the default value and use if statements to change it. If the default does not exist, then only the if statements.

 int timeout = 100; if (moreTime) timeout = 1000; int searchOption = null; if (sometest1) searchOption = 1; if (sometest2) searchOption = 2; // then later.. if (searchOption != null) ..... 
0
source share

If the initialization is complex enough that the direct expression cannot be written cleanly, sometimes I find it useful to use this case as

 boolean isFoo = determineWhetherFoo(...); 

where determineWhetherFoo accepts any arguments necessary for the definition and returns the corresponding logical result. This allows you to understand what a variable means and what it depends on. Initializing a variable to a possible incorrect value, followed by a code code that can change its value, can sometimes hide what is being expressed.

0
source share

Wherever you write if (), also write else - even if it is empty.
The compiler will optimize it, but it makes you (and any programmers after you) think when if () does not work, what are the consequences?

0
source share

All Articles