8. Misunderstanding the Relationship between Code and Data
One of the most critical aspects of programming is data management. A program serves as an interface for adding, editing, and deleting records. Even a small bug in the code can have a significant impact on the data, especially if validation is only done on the side of the program with the bug. New programmers may not notice the problem right away if a data bug affects a secondary functionality. This error can multiply programmatically for a long time before being detected.
What's worse is that fixing the code without fixing the data already created by the code can increase errors in the data to the point of no recovery. So, how do you protect yourself against this? You can use several levels of validation, including frontend, backend, transfer, and database. At a minimum, use built-in constraints in the database.
It's essential to know all types of constraints in the database and use them when creating new columns and tables. For instance,
NOT NULL places a constraint on a column so that it does not accept empty values. This is mandatory if your application assumes this value exists.
UNIQUE he uniqueness of a column value across the table, making it ideal for storing a user's login or email.
CHECK checks for an arbitrary expression, such as percentages, where you should check for an occurrence between 0 and 100.
PRIMARY KEY implies both uniqueness and a non-empty value, and every database table must have such a field to identify records.
FOREIGN KEY tells you that the values in that column are contained in another table.
Another problem for beginners is that they can't think in transaction categories. If several operations change the same data source on which they all depend, they should be wrapped in a transaction that will be canceled if one of the operations produces an error.
Last updated