A collection of C# tips and tricks
Code that is heavily indented is difficult to read (especially if the method does not fit on a single screen anymore).
Instead of this code …
1: void Test(string input)
3: if (input != null)
5: if (input.Length > 0)
… write this code:
3: if (input == null) return;
4: if (input.Length == 0) return;
I totally agree!
this better: if(string.IsNullOrEmpty(input) return;
On the other hand... methods with multiple exit points are also hard to read.Not that I'm disagreeing with you, I'm just ambivalent on the matter.
I think this is largely a matter of personal style. In your example here, I agree. However there are times when regular old curly-braced, indented blocks are preferred. Consistency is more important to me.
Post a Comment