Simberon Design Minute
 

Inside or Outside Assignment

You want to assign different values to a variable depending on some condition. You can put the assignment into each part of the condition or you could evaluate the condition and assign the result to the variable. Which is better? I normally prefer to assign the result to the variable. If you assign the result to a variable, it becomes easier to extract the conditional part into a separate method. It's clearer because you can see at the beginning of the statement that you'll be doing an assignment so you don't have to check each block separately. It makes fewer references to the variable so there's less to change if you need to change the variable name. It just seems cleaner to have one assignment outside the blocks than two assignments inside the blocks.

Download