If style: nest or not nest

My colleague and I had a discussion on the following cutting-edge issue.
Most functions / methods begin by checking some parameters.

I advocate the following style, which avoids nesting.

if (parameter one is ugly) return ERROR; if (parameter two is nonsense || it is raining) return ERROR; // do the useful stuff return result; 

He, who comes from a more functional / logical programming background, prefers the following: he reduces the number of exit points from the function.

 if (parameter one is ok) { if (parameter two is ok && the sun is shining) { // do the useful stuff return result } } return ERROR; 

Which one would you prefer and why?

+7
coding-style nested
source share
9 answers

I personally prefer the first style, as I feel that it provides some logical separation between what we can call "error cases" and "method logic." At the beginning of the method there is a clearly defined block that evaluates and acts on any errors in the input signal, and then the rest of the method is all about what the method should do.

I think this is a kind of separation of problems at the micro level.

+12
source share

As long as the style is consistent between the codebase, either of these two styles will be fine with me.

+5
source share

In the case of two checks, either in order, then as soon as you add more, option 1 quickly becomes more and more desirable!

+2
source share

I prefer to do all my input parameter checks at the beginning of the function and only do return . Therefore, I prefer the first approach in most cases. If there is only one level of nesting, then I can go to the second option.

+1
source share

The second, which I would usually prefer, unless the body of the entire function is wrapped in the number of X if . If so, I would choose the first option.

0
source share

I prefer the first one. 5298529357 indentation levels just decide on my nerves.

Add to this the fact that when you return immediately, it is obvious that (parameter one ugly) is a mistake.

0
source share

Most read style:

 if (parameter one is ok) { if (parameter two is ok && the sun is shining) { // do the useful stuff return result } else { // do other things } } return ERROR; 

At least for me:)

EDIT: Sorry, misunderstood question. I vote for the first, I do not like deep nesting.

0
source share

In my opinion, it depends only on the type of error checking you need.
If, for example, the one parameter is ugly , but in the following code you can change its state to pretty , then the second method is preferable.
However, if the error is fatal and cannot be handled, you should return immediately.
There is a third option, where the second style is best suited, and that is when you want to collect all the errors in one consecutive error message.
The second style does not have to check the correctness, but first the invalidity.
As for personal preferences, I would be much happier with the first style.

0
source share

Our internal style is to avoid multiple return points and also limit the amount of nesting, so I would probably combine your readiness checks and do something like:

 result_t result = OKAY; // Sanity checks if ((parameter_one == ugly) || (parameter_two == nonsense) || (weather == raining)) { result = ERROR; } else { // do the useful stuff } return result; 
0
source share

All Articles