What Does Hard Coding Mean?
We have seen beginners come and go in our company and most of them started with a hardcoding approach. When learning Drupal, there is always this temptation to do hard coding. Most beginners to Drupal have PHP knowledge and have previously worked with PHP and it’s good. What’s not good is when they tend to use shortcut methods (in their minds) in order to complete a task.
What do we really mean by hard coding? First let me tell you what it doesn’t mean though. It doesn’t mean that working with Drupal does not require PHP knowledge and that you just click buttons and the whole system works -- No, that’s not what we mean by not hard coding.
Hard coding is when you create a custom function to handle a very specific case. Some examples of hard coding are as follows :
- Writing a script that queries the database and then return a value or a script that updates a field
- Checking a markup for a specific node ID to replace its title
- Craftily creating login script to add flexibility on the login theming
These are just some of the examples and they might work most of the time. This however is just like hacking Drupal core, where the speed of completing the task does not justify the maintainability of the entire site.
Why we Don’t Recommend Hard Coding for Most Projects
In one word -- It’s not the “Drupal Way”.
Most of the modules and core parts of Drupal including templates have comments that serve as both warnings and guides. Comments like “use template_preprocess_…” or “The above defaults are designed for database portability. Changing them may cause unexpected behavior”. Going against these guidelines and warnings could cause the whole site structure to be inefficient.
If you’re still not convinced, here are some scenarios you should consider :
- Maintainability
Oftentimes in our company, we assign tenured developers to new projects while we leave the new members with maintenance of already existing web sites. The idea here is so the new members can support and at the same time study how the site was created. New requests come in and the new member will handle the modifications. Most hardcoded functions are not flexible enough which could lead the new member to think of an alternative solution. This could turn into creating another function altogether which is time consuming and may need a little downtime when deployed to Production due to the changes.
The scenario above is just within the company setting. The tenured member can just pass on the idea of what is going on in the function. But this is not always the case, some projects are one shot deals wherein you only build the site and then the client’s developers for example will do the maintenance. This could impact your company’s integrity in creating quality sites.
- Compatibility
You created a very elaborate function that retrieves information using specifically defined values instead of retrieving using API. This works on your current project well and your boss is impressed with how well it delivers so he asks you to apply the same thing on another project. But the function is structure specific so that it could not just be copied to another project that could have used the same feature.
- Resource Exhaustion
Instead of using a readily available API, you created a function that loops around node IDs just to find specific values to be retrieved and displayed on a page. It works fine when you test it but you have to keep in mind that during the real world application, there are hundreds if not thousands of site visitors. After all, high site traffic is the aim of all websites for increased exposure and profit.
Off course there are exceptions
We do recognize that there are instances where hardcoding is really necessary. For example a project wherein you need to place a promotional banner in only one node and it’s the only time you are ever going to use it. In this case it is more efficient to add it to that node directly instead of looking for a module and adding it to Drupal.
If you are going to hardcode, exercise caution, and always ask for advice if you're uncertain about whether to do it at all. Or if you're certain, because that's even more dangerous.