DEV Community

Discussion on: Simplify foreach loops with the null-coalescing operator

Collapse
devguyky profile image
devguyky

My personal preference for more readable code would be to adopt a 'happy path' and return from the method before reaching the loop.

if (listOfStrings is null)
   return;

foreach (string item in listOfStrings)
{ ... }
Enter fullscreen mode Exit fullscreen mode

A good article on adopting the happy path