Thursday, July 23, 2009

Exception Handling - Code for failures - Part-2

Part-1 is here...
Part-3 is here...
Exception can be thrown for two reasons
1. Application failure, which includes operational failures, code issues and all other technical issues. (mostly non recoverable by end user)

2. Business validations. (mostly recoverable by end user by changing the input data)


If any failure happened,
we required below information to fix the issue

1. What was the failure?
2. Where it happened?
3. Which circumstance or state or environment?
4. What are the input data?
5. Is this recoverable?
6. Is it necessary to Alert / Escalate to the support people?


Our Exception handling must provide all these details whenever the failure happened.

How do we provide these information?

1. What was the failure?
Do not throw a generic high level exception, provide a more specific Exception class.

2. Where it happened?
Do not throw a generic high level exception, provide a more specific Exception class, if we use the Same exception class in many places differentiates with the Id or message. so we know exactly where the failure happened.

3. Which circumstance or state or environment?
This is very important, many time we programmers do not mind that our program work only in a Conditional state(or environment), so we need to provide this information while throwing exception by id orMessage.

The exception handler (typically a catch block or caller method) would change the state or look foralternate and re-try before throw the exception.

4. What are the input data?
This information is important for Exception handling, most of the time exception could happened by theinput data, throw proper data exception when the failure happened like NullArgumentException

5. Is this recoverable?
Most of the time, we throw Validation Exceptions, these validation exceptions would be recoverable by the end user, so the handling code must know that these exceptions are recoverable exceptions, so we Provide more meaningful information to recover this to the end user.

Secondly this would use to hold the same state or environment for the end user to continue working with the application.

6. Is it necessary to Alert / Escalate to the support people?
Not all the exceptions required admin or support people to be informed, so providing this information to the handler code will alert the admin or support team accordingly.

If we get all the mentioned information without seeing the log, code and trace is consider the better Exception handling.

The Exception handling tips Will be continued…

Tuesday, July 14, 2009

Fast Lane Reader Pattern - When to use this?

For our last few developments, we follow the Service --> Business --> DAO layer architecture.

We strictly follow that the Service layer can call only Business layer and not DAO, the same way Business layer can call only the DAO layer and not the Service layer, this is approach is very much OK for all the CRUD operations

But this approach is tedious, unnecessary and overhead for some straight forward operations like just to display large data without many validations, manipulations, business logic and so on… in this scenario we use Fast Lane Reader pattern. By this pattern we can call DAO layer directly from Service layer. So we avoid the complexity.

Conditions to use this

1. The data must be sued for Read only purpose and the data access also read only
2. It must be as a tabular form instead of Object form
3. Use only when the Data accuracy is not a crucial factor, means efficiency is more important than the current data.

We could achieve fast retrieval of large data…

We could avoid complexity in code, another way this will increase the design and code complexity by providing another passage to access the data.