All programmers deal with software errors. In this post I will talk about types of errors and how to deal with them not specific to any programming language and also touch upon code smells. Broadly the errors can be classified as following.
1. User Input Error: When user keys in an invalid data and program is not handling it
2. Programmer Error: This comprises of coding errors, broken code
3. Exception Circumstances: Network Connectivity Loss, No memory issue
1. Raise an error when something goes wrong
2. Detect all possible error reports
3. Handle them appropriately
4. Propagate errors when we can't handle them
Error handling Guidelines
1. Never ignore an error condition. If you do not know how to handle the problem, signal a failure back to the calling code.
2. Do not sweep an error under the rug and hope for the best.
3. Do not return null in case of error condition
4. Too many getter and setter methods are indicators of 'ASK & Decide' approach, eliminate or minimize Getter & Setter methods. Moving from ASK & Decide approach to TELL approach gives better encapsulation and better design. Remember that get/set are violation of encapsulation. Reducing get/set methods improve code quality and readability.
5. Develop the code that is 'hard to misuse' and easy to use. This reduces the chance for an error.
1. User Input Error: When user keys in an invalid data and program is not handling it
2. Programmer Error: This comprises of coding errors, broken code
3. Exception Circumstances: Network Connectivity Loss, No memory issue
What to do with errors?
1. Raise an error when something goes wrong
2. Detect all possible error reports
3. Handle them appropriately
4. Propagate errors when we can't handle them
Error handling Guidelines
1. Never ignore an error condition. If you do not know how to handle the problem, signal a failure back to the calling code.
2. Do not sweep an error under the rug and hope for the best.
3. Do not return null in case of error condition
4. Too many getter and setter methods are indicators of 'ASK & Decide' approach, eliminate or minimize Getter & Setter methods. Moving from ASK & Decide approach to TELL approach gives better encapsulation and better design. Remember that get/set are violation of encapsulation. Reducing get/set methods improve code quality and readability.
5. Develop the code that is 'hard to misuse' and easy to use. This reduces the chance for an error.
No comments:
Post a Comment