Because of short-circuit evaluation, both solutions are equally valid technically. However, stylistically, it's usually preferable and possible to arrange for variables like metadata
never to be null
in the first place.
In this particular case, you are using two variables to denote type. metadata.TypeEnum
stores the type, and metadata
itself being null
denotes a "no metadata at all" type. Your programming language lets you create as many types as you want. Take advantage of it.
A better solution is to create a Cell
base class that doesn't even have a metadata
member, so there's no need for it to ever be null
. Then create a StatusCell
derived class that adds any unique properties you need, and same for the other types of cells.
If that kind of refactoring isn't possible, like if you're working with a third-party API, another option to consider is a guard clause, like:
if (metadata == null)
return;
if (metadata.TypeEnum != VariantInfoMetadata.CellTypeEnum.Status)
...
A lot of people (including myself), prefer this style because it limits both the level of nesting and the number of conditions in one if
statement.