DEV Community

Zohar Peled
Zohar Peled

Posted on

default vs null - which is a better choice, and why?

Some background:
Some of the projects I'm in charge of at work are basically mapping projects - we consume data from somewhere, and map that to our own types.

This is a part of a bigger process that ultimately unifies and translates data from different sources into a single data repository.

As a part of the mappings, we often have methods like this:

NormalizedData Normalize(RawData rawData)
{
    return new NormalizedData()
    {
        PropA = rawData.A,
        PropB = rawData.B,
        // more of the same here
    };
}
Enter fullscreen mode Exit fullscreen mode

As more data sources are being normalized, the structure of the NormalizedData changes (Unfortunately, not my design) and so in an attempt to make things easier to handle new properties being added, I've decided to populate all the properties of the normalized data - even if they are irrelevant to the specific data source:

NormalizedData Normalize(RawData rawData)
{
    return new NormalizedData()
    {
        PropA = rawData.A,
        PropB = rawData.B,
        PropC = default,
        PropD = default,
        PropE = rawData.E,
        // more of the same here
    };
}
Enter fullscreen mode Exit fullscreen mode

Now - the question is this:

Should I use PropC = default, or PropC = null,?

As a side note - leaving all these properties uninitialized is also an option I would consider, but only if the system will ever become stable enough. Currently, I'm forced to revisit projects that are already in production and add some more properties to the normalization process, so it's much easier to see what properties I have to add when all of the previous properties are already listed.

Top comments (2)

Collapse
 
glsolaria profile image
G.L Solaria • Edited

It was my understanding that with c# if you do not initialise the property it will won't be uninitialised - it will be automatically assigned "default" docs.microsoft.com/en-us/dotnet/cs.... Not all languages do this but the c# language specification does make it explicit.

I tend to not use default or explicit null assignment unless I am dealing with generics. I prefer to use default over null if I did use it.

So I think assigning default or null is generally redundant for c# but in your case I am on the fence. I can see why used default in this case - to show you have considered the initial value and consciously decided it should be default. But I would probably just add a comment about why it is okay for this value to be initially the default if I was reviewing a legacy code base because that is valuable information that can be critically reviewed in the future if a problem arises or if someone else ventures in to the code.

Collapse
 
peledzohar profile image
Zohar Peled

Thanks for your comment. I feel the same. Leaving unitialized properties makes it impossible to know if the author was even aware they existed when committing the code - and since the classes we use tend to have a lot of properties (some have well over 20) it's easier to know what's new when the old list is already coded. (Of course, better interpersonal communication could solve that, but I didn't hire the guy in charge nor can I order him around.)