if statement - How do you write the error checking part of a method to make it readable and error prone? -


I had a disagreement with another programmer about how many errors are to be typed with the method:

  Public Zero Execution () {if (test1) {if (test2) {if (test3) {// DO STUFF} and {return "Error 3"; }} And {return "Error 2"; }} And {return "error 1"; }}   

-

  Public Zero Execution () {If (Test1) {Return "Error 1"; } If (! Examination 2) {return "Error 1"; } If (! Examination 3) {return "Error 1"; } // DO STUFF}   

For me, If making a dark nest of the statement makes it difficult to read the first example.
The second, despite having three returns s, is more readable.

I have examined the curiosity about what the code was saying and it believes me less about how it handles it:

under the nest The heap of error conditions indicates the well written error processing code.

But then

If the main body of the routine is closed around then the statement becomes unconscious, especially if the maximum code in the statement it happens.

And considering using the Guard Clauses, as a second example

Minimize the number of returns in each routine. This is a regular form. It is difficult to understand from, when reading it below, you are unaware of the possibility that this is something-where is returned above.

How do you write error checking part of the method to make it readable and error prone?

Programmer did not find anything faster than the stylistic debate in combat () then briefly answered "Whatever style you and your team decides is best for your project / language."

It is being said, I would like to add two cents for the comment of the code complete about the multiple returns, many successful returns, and Distinguish between multiple returns. If I need to check the 5 returns that are not due to errors, then the function must be rewritten. If you exit your work immediately after passing the error, then a maintenance programmer (i.e. you in 6 months) should not have any more problems after the main argument of your function, if you want to check all those errors Had nested.

So, my personal opinion is that your second code example is a better option.

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -