DEV Community

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

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.)