Null or Not Null

S

shapper

Hello,

I am getting a Post with its Tags.

When there is not Tags what should I do?

Not initialize the tags and then Post.Tags will be null.
Or initialize the tags and then Post.Tags.Count is 0 but Post.Tags is
not null.

Linq query initializes the tags.

ASP.NET Mvc model update does not initializes it when the text box has
no content.

Basically I have different behaviors ...
.... for everything to work (validation, binding) I am trying to choose
one approach.
If not the code gets a little bit confusing because I never now if it
is coming null or Count=0.

I am trying to decide which approach should I use ...

Thanks,
Miguel
 
P

Pavel Minaev

Hello,

I am getting a Post with its Tags.

When there is not Tags what should I do?

Not initialize the tags and then Post.Tags will be null.
Or initialize the tags and then Post.Tags.Count is 0 but Post.Tags is
not null.

Linq query initializes the tags.

ASP.NET Mvc model update does not initializes it when the text box has
no content.

Basically I have different behaviors ...
... for everything to work (validation, binding) I am trying to choose
one approach.
If not the code gets a little bit confusing because I never now if it
is coming null or Count=0.

In general, it is usually easier to avoid nulls. You question is
roughly equivalent to: "if I have no Tags, should I say that I have an
empty set of Tags, or should I say that I have no set of tags?". From
practical point of view, the "empty set" case is easier because you
don't have to check for null all the time. Unless you really need to
distinguish between two cases of having no tags, go for empty list.
 
S

shapper

In general, it is usually easier to avoid nulls. You question is
roughly equivalent to: "if I have no Tags, should I say that I have an
empty set of Tags, or should I say that I have no set of tags?". From
practical point of view, the "empty set" case is easier because you
don't have to check for null all the time. Unless you really need to
distinguish between two cases of having no tags, go for empty list.

Thank you Pavel for the advice.
I am following it.
I was having some problems with the MVC project because each time I
need to check different things.
Now it is working fine.

Thank You Once Again,
Miguel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

XML and SQL database 3
Circular Reference. I really need help ... thanks. 4
Filter 1
Null and Generic Type 2
Repository and Services 1
Condition 2
Linq. Aggregate 4
String.Empty or "" 14

Top