5

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;
}
JYelton
  • 35,664
  • 27
  • 132
  • 191
Joan Venge
  • 315,713
  • 212
  • 479
  • 689

8 Answers8

12

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
  • 414,610
  • 91
  • 852
  • 789
  • 3
    I agree. The less information I have on the screen for my brain to process, the better. – Vadim May 09 '09 at 22:17
7

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
  • 272,464
  • 47
  • 358
  • 399
3

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.

Shea
  • 11,085
  • 2
  • 19
  • 21
2

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
  • 44,326
  • 9
  • 65
  • 80
1

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
  • 3,681
  • 3
  • 27
  • 36
1

Check out these questions:

Should I always/ever/never initialize object fields to default values

Why does FXCop thinks initializing to the default is bad?

Community
  • 1
  • 1
Aaron Daniels
  • 9,563
  • 6
  • 45
  • 58
1

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-
  • 17,663
  • 14
  • 81
  • 103
0

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
  • 30,403
  • 22
  • 79
  • 107
  • 1
    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 May 16 '09 at 13:01
  • 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 May 16 '09 at 13:40
  • 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 May 16 '09 at 13:41