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.
Paul W Homer
source share