Null or Not Null

  • Thread starter Thread starter shapper
  • Start date Start date
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
 
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.
 
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
 
Back
Top