Comment Re:Subs and functions (Score 1) 477
Being able to separate your code into small logical pieces makes it easier to understand, test and maintain.
Well said! Bravo!
Breaking code into smaller logical pieces is paramount to making it maintainable. When you or someone else goes to maintain it in the future and finds there is a problem with a specific portion, such as with writing data to the database, they should be able to find the database portion and trace back from there.
A good example of this principle of logic separation is with code that interacts with a database. I've worked with some custom PHP code that had a lot of interaction with a database, but all the connections were done directly when needed instead of using a class (or even just a function). The effects of this decision had a ripple effect:
* parameters to the connections had to be given inline
* connection strings had to be globally accessible, though were regularly hard-coded
* SQL had to be written & executed inline
* checking for rows returned/rows affected/errors had to be done for each call manually
* code was regularly created via copy/paste
* variable re-use often caused errors (previous call was successful, last call wasn't, but variable continued to have a successful appearance)
The first thing I would have done, had I been allotted the time, was to integrate an abstraction layer for the database. Separating this logic would have allowed for the ability to write-out SQL statements to a text file for analysis--which is very helpful for determining where a database can be optimized (i.e. better indexes, etc).
This specific level of separation would also allow for moving to a different database with a smaller impact on the code: modify a smaller set of code to connect to a new DBMS, then use it to track any issues resulting from the new database. Not only does it help save time, but it avoids rewriting a ton of code!