0

In my last job, my manager told me he had read a Microsoft Best Practice guildine regarding the declaration of variables.

Say for example I want to declare an int, a string and a generic type, with their default values:

int intValue = 0;
string stringValue = null;
T genericValue = default(T);

The example above illustrates that all variables have been initialized with their default values, explicitly (and literally), except in the case of the generic type; here we use default(T) to implicitly initialize the member to it's default value.

The argument that my manager made was that all variables, even in the case where we are initializing them with default values, should be initialised with default(...);

int intValue = default(int);
string stringValue = default(string);
T genericValue = default(T);

In some respects I can see why this approach might be favourable, since the declaration and initialization of variables is consistent.

I want to know if anyone knows of the legitimacy of my managers claim, and in that respect, if you could point me in the direction of the guildine within Microsoft's Best Practice documentation for .NET and C#

Matthew Layton
  • 39,871
  • 52
  • 185
  • 313

3 Answers3

3

I'd say your code is more readable than what your manager suggested. Write code which is readable for all. Also I've not come through such a guideline from microsoft(someone can cite it if any).

For the sake of argument, consider the following snippet: How sure are you what is the default value of it?

SomeCustomType something = default(SomeCustomType);

null? or new SomeCustomType? No, you can't say unless you know whether it is struct or class. In order to know that you need to navigate to type's definition(or metadata if no source code). Consider there are 10+ fields like this. It becomes tedious. So straight away tell the reader of your code that SomeCustomType something = null; which is a reference type(Ignoring nullable types, we can find it easily which will have ?suffix). Trust me readers of your code will be happy..

All these arguments started to break when we start to talk about instance or static fields. because it is redundant, CLR does that for you. You don't need to. Interestingly if you compile the program against .Net1.1 initializing the fields to leads to performance penalty. Which has been optimized later.

Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
  • +1. The problem I see is that what if the programmer reviewing this code later doesn't know what the `default` value of `int` is? They'll have to Google it and then find this StackOverflow question. You can save some trouble by just setting the variable to `0`. – Steve Wortham Jul 30 '14 at 16:10
  • @SteveWortham Situation becomes even worse when internet is not working at that moment :-D – Sriram Sakthivel Jul 30 '14 at 17:39
0

I don't like your manager suggestion. For me is much clear initialize an int to 0, instead to default(int). Is more explicit.

But, if I needed to use your manager suggestion I prefer using var instead of declaring the type:

var index = default(int);
var ordersPerCustomer = default(Dictionary<string, Order>);

This reduces "noise" in the code as the type is clearly made explicit in the default.

eiximenis
  • 628
  • 4
  • 8
0

I've always heard the default(T) keyword used with generic types, when it is unknown if the var will point to a value type or to a reference. I think of it as the right-side equivalent of the var keyword, but used mostly in generic templating.

Source: http://msdn.microsoft.com/en-us/library/xwth0h0d(v=vs.110).aspx

Edit: See this related thread with a general description of the default(type) keyword: What is the use of `default` keyword in C#?

Community
  • 1
  • 1
cjcurrie
  • 624
  • 1
  • 4
  • 15