Note that there are some explanatory texts on larger screens.

plurals
  1. PODefault string initialization: NULL or Empty?
    primarykey
    data
    text
    <p>I have always initialized my strings to NULL, with the thinking that NULL means the absence of a value and "" or String.Empty is a valid value. I have seen more examples lately of code where String.Empty is considered the default value or represents no value. This strikes me as odd, with the newly added nullable types in c# it seems like we are taking strides backwards with strings by not using the NULL to represent 'No Value'.</p> <p><strong>What do you use as the default initializer and why?</strong></p> <p><em>Edit: Based on the answers I futher my further thoughts</em></p> <ol> <li><p><strong>Avoiding error handling</strong> If the value shouldn't be null, why did it get set to <code>NULL</code> in the first place? Perhaps it would be better to identify the error at the place where it occurs rather than cover it up through out the rest of your codebase? </p></li> <li><p><strong>Avoiding null checks</strong> If you are tired of doing null checks in code, wouldn't it be better to abstract the null checks? Perhaps wrap (or extend!) the string methods to make them <code>NULL</code> safe? What happens if you constantly use <code>String.Empty</code> and a null happens to work it's way into your system, do you start adding <code>NULL</code> checks anyways?</p></li> </ol> <p>I can't help but return to the opinion that it is laziness. Any DBA would slap you nine ways to silly if you used '' instead of <code>null</code> in his\her database. I think the same principles apply in programming and there should be somebody to smack those upside the head who use <code>String.Empty</code> rather than <code>NULL</code> to represent no value.</p> <blockquote> <h3>Related Questions</h3> <ul> <li><a href="https://stackoverflow.com/questions/263191/in-c-should-i-use-stringempty-or-stringempty-or">In C#, should I use string.Empty or String.Empty or “” ?</a></li> <li><a href="https://stackoverflow.com/questions/151472/what-is-the-difference-between-stringempty-and">What is the difference between String.Empty and “”</a></li> <li><a href="https://stackoverflow.com/questions/167952/null-or-empty-string-to-represent-no-data-in-table-column">Null or empty string to represent no data in table column?</a></li> </ul> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload