Is it a good or bad practice reducing nested if statements?

Thanks to ReSharper, I started reducing nesting in my code, like with this method:

public static BindableCollection<T> ToBindableCollection<T:(IEnumerable<T> collection) { if (collection == null) return new BindableCollection<T>(); return new BindableCollection<T>(collection); } 

I would prefer this piece of code:

public bool DoThis(string name, int age) { if (String.IsNullOrEmpty(name)) return false; if (name.Contains("cool")) { //Some code ... return true; } else { //Some code return false; } } 

over this:

public bool DoThis(string name, int age) { bool result; if (String.IsNullOrEmpty(name)) { if (name.Contains("cool")) { //Some code ... result = true; } else { //Some code result = false; } } else { result = false; } return result; } 

My question is what do you think? Do you reduce nesting this way in your code? Is it a good or bad practice?

My point of view: 1, I prefer multiple exit points in my methods, because with sort methods, by having only one exit point you add some clutter to your code. 2, Personally reading nested ifs seems to be harder. 3, Most of the times the first 1-2 lines of my code is input checking

by szntb via /r/csharp

Leave a Reply