Is it a bad practice to initialize a variable for a dummy value?

This question is the result of the answers to this question that I just asked.

It was argued that this code is "ugly" because it initializes the variable with a value that will never be read:

String tempName = null; try{ tempName = buildFileName(); } catch(Exception e){ ... System.exit(1); } FILE_NAME = tempName; 

Is this really bad practice? Should variable initialization be avoided for dummy values ​​that will never be used?

(EDIT - What about initializing the String variable to "" before the loop that will concatenate the values ​​into String ...? Or is it in a separate category?

eg.

 String whatever = ""; for(String str : someCollection){ whatever += str; } 

)

+6
default-value variable-initialization
source share
5 answers

I think it makes no sense to initialize variables for values ​​that will not be used unless required by the language.

For example, in C #, the default value for a declared string variable is null, so you don’t even type anything explicitly writing it out. This is basically a style choice, but since the lines are immutable, initializing it to something else will actually allocate an extra line in memory that you just throw away anyway. Other languages ​​may impose other considerations.

+6
source share

As for the loop of strings, if you change it to StringBuilder, you don’t even have to think about it.

Edit: deleted bits are better to respond to others.

+1
source share

As a practice, I try not to set variables to arbitrary values, but instead to initialize their default value.

i.e.

 int x = 0; string name = ""; bool done = false; Person randomGuy = null; //or = new Person(); 

I like this method best because it brings uniformity to your code without forcing the next guy who comes to deal with things like: string name = "luke skywalker";

This is an increasingly personal preference, so it will vary between programmers.

In fact, you must follow the standards set in your project. You will have an idea of ​​how legacy code handles these things, and it is probably best to match this, so the overall coding style is the same throughout the system.

0
source share

It depends on the compiler. The C # compiler requires that the variable be initialized before use. But the CLR does not have this requirement. At run time, the CLR checks to see if the variable is initialized or not. If this is not the case, a zero-error exception will be thrown.

0
source share

In my opinion, it might be more correct to call it "the smell of code" - in the sense of Martin Fowler.

I do not think that you can change the default initialization separately - it will need to be used in combination with other refactoring methods. It is also assumed that you edited your code so that you do not need temporary variables:

 try{ FILE_NAME = buildFileName(); //Do other stuff with file name } catch(Exception e){ ... System.exit(1); } 

Then, an assumption is also made that this code segment is only the code in the method in which it is contained, i.e. a method does only one thing

When I code, it bothers me that I use dummy values ​​with temporary variables, but I would only change it when I finished coding this section, and it solves the problem on my own - and only in combination with other refactoring steps.

0
source share

All Articles