Montag, 18. Mai 2015

C# 6 in action: Null propagation applied

Just another nice everyday example on how C# 6 can reduce verbosity. In case you don't let ReSharper teach you, be sure to spend 8 minutes on C# 6.


This is one of the more noteable of today's refactorings where null-propagation was applied.

Before:
private bool CurrentContextHasPlayer()
{
  return _currentViewContext != null &&
         _currentViewContext.AudioView != null &&
         _currentViewContext.AudioView.AudioPlayer != null &&
         _currentViewContext.AudioView.AudioPlayer.Player != null;
}

After:
private bool CurrentContextHasPlayer()
{
  return _currentViewContext?.AudioView?.AudioPlayer?.Player != null;
}

You might argue that this is an instance of the Feature Envy code smell and maybe you would be right ;-)

Keine Kommentare:

Kommentar veröffentlichen