tags:

views:

261

answers:

8

Is it a bad practice to rely on implicit default values, like:

class Node
{
    int red;
    int green;
    int blue;

    bool grayscale;

    Node next;
}

Instead of explicitly setting them:

class Node
{
    int red = 0;
    int green = 0;
    int blue = 0;

    bool grayscale = false;

    Node next = null;
}
+3  A: 

I don't think it's a bad practice since the default values are something a developer working in the language should know/be forced to learn.

The only downside that comes to mind is that not listing defaults may cause people to never look at the initialized value. Might take a bit to notice in the case where it's initialized to something other than the default.

Arnshea
+10  A: 

No, I think it's OK to rely on default values. Explicitly assigning them will just clutter up the code. Also, it has the advantage of making it easier to distinguish fields you assign non-default values to.

Mehrdad Afshari
I agree. The less information I have on the screen for my brain to process, the better.
Vadim
+6  A: 

I would always put them in, because it proves you've thought about what the initial values of those members should be.

bool isCool;

could mean "I know this is not cool on startup" or it could mean simply that you didn't think about it.

bool isCool = false;

is clearly a deliberate decision.

RichieHindle
+2  A: 

It's mostly subjective, imho.

For me, It's too "wordy" even though it is the default value declared explicitly ... It still slows me down a tiny bit when reading the code and let's face it ... it's redundant.

I also think it's in code more often than it should be because people don't know the default values or have some irrational fear that they might change bool's default value to true in C# 5.0.

Chad Grant
A: 

I prefer giving the values explicitly. It does less strain on the brain tried to load the default values from memory. Better have them precached. No harm done.

like

string name = string.Empty;

or

Guid userID = Guid.Empty;
User
The thing is that the default values for string and Guid is not string.Empty and Guid.Empty, it is null. So if you wanted them to be "Empty" to start with you would have to explicitly set them.
Caleb Vear
For string I agree with you, it's null. For Guid it is actuall Guid.Empty, if you check the value of an "uninitialized" variable.
User
That is why it is better to write it explicitly. You know it this way, the next guys knows it differently. What's the point in bringing confusion?
User
+1  A: 

As an alternative to all answers before, I use to put those initial values in a constructor, so I don't bother looking for variable's initializacion outside it should be.

class Node
{
        int red;
        int green;
        int blue;

        bool grayscale;

        Node next;
        public Node() {
            red = 0;
            green = 0;
            blue = 0;

            grayscale = false;
            next = null;
        }
}
Jhonny D. Cano -Leftware-
+1  A: 

Developers are responsible for knowing what the default values for primitive data types are, so they are not strictly necessary. However, as some have indicated, it does prove you've thought about the issue.

Further, the overwhelming majority of variable types in your code will be custom types that you've created. Developers are not responsible for knowing what the default value of your custom enum is. Good communication requires that you specify default values in these cases. Since we are creatures of habit, it's better to establish a habit of always initializing your variables. It makes no difference whether you do it at the class level or in your constructor so long as you are consistent in your approach.

Chris McKenzie