What is the best practice when concluding a cessation clause ... see explanation :)

Things are as follows: You have the boolean property FullScreenEnabled. You enter a method and the code inside this method executes if FullScreenEnabled is true. Which of the two approaches below do you use in your daily programming:

private bool FullScreenEnabled { get; set; } // Check if FullScreenEnabled is false and return; private void Case1() { if (FullScreenEnabled == false) { return; } // code to be executed goes here! } // Surround the code by an if statement. private void Case2() { if (FullScreenEnabled) { // code to be executed goes here! } } 
+4
source share
10 answers
 private void MyMethod(bool arg){ if(arg) return; //do stuff }; 

(for voting)

+11
source

I usually prefer the first version (folding at the beginning of the method). This leads to a decrease in nesting, which slightly increases readability. If you decide that you do not need to check the status in the future, it is also easier to remove the if condition in the first version, especially if you have several such checks. In addition, it can be easily written on one line: if (! FullScreenEnabled) return;

+3
source

It depends on the length and complexity of the method. If the method is short, nesting inside the if is not a problem (and may be clearer). If the method has many nested statements, then an immediate return will reduce the amount of indentation needed and may slightly improve readability.

+1
source

The first approach (using the guard clause) scales better if you add more if . The problem with the second approach is that adding if statements will lead to the creation of code that demonstrates the arrow against the template where the code begins to be identified as an arrow.

There is a very good article that explains this in more detail below:

Horror Coding: Compress Arrow Code

+1
source

None of them have been published. You should read the editing help to make sure that the code is actually displayed.

0
source

About whether to check a positive or negative result, that is, return at the beginning of the method if the condition is not met, or code execution only when the condition is met. In short, I would go with the last case, in a long method, I would go with the first. I always came with an early exit when there were several conditions for testing. In fact, it does not matter.

Note that in your example this is a comparison with false. Instead, you should write FullScreenEnabled. Makes the code more readable.

0
source
 if (!FullScreenEnabled) throw new InvalidOperationException("Must be in fullscreen mode to do foo."); 

My two cents, what it costs.

0
source

In any case, it works the same.

However, if you run code coverage metrics for your unit tests, if (!FullScreenEnabled) return; will be considered a separate block, and you will need to create a unit test to cover it in order to get 100%.

Of course, even with a different approach, you might need a unit test that checks that you are not executing your code when FullScreenEnabled is false. But if you cheat and don't write it, you still get 100%. :-)

0
source

I would go with the first approach, I consider it more readable, and then the second. I basically think that:

  • if (FullScreenEnabled == false) more readable, then if (FullScreenEnabled) .
  • if you keep checking your β€œsanity” at the beginning of the method, the method gets a good structure that is very easy to understand.

However, it seems to me that there is a fine line here that does not need to be crossed, adding the return expression to too many places in the middle of the method makes it more complicated

0
source
 private void MyMethod(bool arg){ if(!arg){ //do stuff } } 

(for voting)

-1
source

Source: https://habr.com/ru/post/1277268/


All Articles