Programming tips: constraints and exceptions
Quick summary
The article explains how constraints and exceptions help control invalid or unexpected situations in a program. It distinguishes between validating data and handling errors that interrupt the normal flow.
Contents
OpenERP provides a standard mechanism for adding constraints to records. This is used, for example, in account.move.line to ensure that no account of type view is used in an account move line. At first glance, this functionality seems useful, but experience shows that the current constraints mechanism has serious usability problems.
The reason is quite simple. If we consider the example of account.move.line, a user may fill in a large bank statement and then try to confirm it once it is complete. This is when the account moves are created, and if just one of them uses an account of type view, the user will always receive the same message: “You cannot create a move line on a view account.” Perfect. But which of the 100 lines in the statement is the incorrect one?
Something similar happens when developers fail to consider this issue when raising exceptions. For example, if one of the 100 lines in the bank statement has already been reconciled, the user will receive the highly informative message: Already Reconciled.
These are very important considerations in production environments, and we have had to make several changes to improve exceptions, especially in accounting modules. A simple message such as “Account move line %s, with debit %.2f and credit %.2f, has already been reconciled” does the job and can save OpenERP users a great deal of trouble.
It is important to bear in mind that, if the record has just been created, the user may be unable to locate the problematic record because rolling back the transaction may remove it. It is therefore always advisable to provide as much information as possible.
Finally, because standard constraint messages do not allow all the necessary information to be included, the best approach is to use the constraints mechanism while raising an informative exception from the constraint function, instead of allowing OpenERP to return the standard constraint message to the user.
Frequently asked questions
What is the difference between a constraint and an exception in programming?
The article presents constraints and exceptions as different mechanisms for handling invalid or unexpected situations in a program.
When is it useful to apply a constraint?
It is useful when data must be prevented from being saved unless it meets a defined condition, making validation part of the application’s normal behaviour.
What are exceptions used for?
They handle situations that interrupt the program’s normal flow and allow the application to deal with them in a controlled way.
Related articles
Open Source and Trends
The Definitive Guide for Choosing Business Management Software - CHAPTER 7
The technology lead should be involved from the start of ERP selection because they can assess each option’s robustness, performance and underlying technology. Their experience also helps estimate customisation, integration with existing tools, and the effort required for implementation and maintenance.
ERP and Business
The Definitive Guide for Choosing Business Management Software - CHAPTER 8
Choosing an ERP involves more than comparing software prices: implementation, data migration, training, maintenance, upgrades, and other related costs must also be assessed. The finance director should be involved because an ERP affects investment as well as the company’s control, planning, and analytical capacity. A phased implementation or a SaaS model may reduce the financial impact, although each option involves a different balance between flexibility and cost.
Open Source and Trends
The Definitive Guide for Choosing Business Management Software - CHAPTER 4
Including free and open-source software in an ERP search broadens the options beyond vendors and products based on proprietary licences. Using a culinary metaphor, the article explains how access to source code brings transparency, lawful copying and adaptability, while noting that these alternatives do not always appear in initial searches.