Clean Code – Chapter 7: Error Handling

Tram Ho

Use Exceptions Rather Than Return Codes (Use Exceptions rather than returning Codes)

You can set an error flag or return an error code that the caller can check.

The problem is that it’s messy with the caller. The caller must check directly after the call. Unfortunately, it is easy to be forgotten. For that reason, it is better to throw an exception when you encounter an error. It makes the code cleaner and the logic is not obscured by error handling.

The code is better because there are two messy relationships here, the algorithm for device shutdown and error handling, and now it’s separated.

Write Your Try-Catch-Finally Statement First

Try – Like transactions. Catch – Leaves the program and no problems occur during try.

Using try-catch-finally is a good way to start writing code when you’re writing code and throwing exceptions. This helps us define what users should expect, no matter what the code in try.

For example: Write a piece of code with the purpose of accessing files and reading some objects in order.

We start with Unit Test telling us the exception when the file does not exist:

This test motivates us to write a stub:

This test failed and it didn’t throw an exception. Next we change the reality, so that it tries to access an invalid file. This action throws an exception:

Test passes because it has caught an exception. Restructuring:

Use Unchecked Exceptions

Checked: are the exceptions that are checked at compile time. Unchecked are the exceptions that are not checked at compiled time. C # doesn’t have checked exceptions. Checked exceptions can sometimes be useful if you are writing a critical library: You must catch them. But in general application development the dependency costs outweigh the benefits.

Provide Context with Exceptions (Provide Context with Exceptions)

Each exception should provide context to identify the source and location of the error. In Java, you will get a trace from any exception. However, that stack does not tell you the meaning of the exception that failed. Create an error message and pass it to your exception. Mention the actions that cause the error and the type of error.

Don’t Pass Null

Returns null from a bad method, but passing null is even worse. Unless you are working with an API that hopes to overcome null, you should avoid passing null in your code whenever possible,

What happens if passing a null parameter? calculator.xProjection(null, new Point(12, 13)); We will have a NullPointerException. Not fixed:

There is another better alternative:

In most programming languages ​​there is no way of dealing with passing a null with an accidental call. Because this is the case to approach Null transmissions by default. When you do you can code with the understanding that a null appears in the parameter list is a sign of the problem and ends it with the least careless error,

Conclusion

Clean code is readable, but it also needs to be strong. This is not a contradiction. We can write clean and powerful code if we find that error handling is a particular concern, and sometimes we can treat it as independent of our basic logic. To the extent that we do that, we need to interpret it independently, and we can take a big step forward in maintaining our code.

Share the news now

Source : Viblo