As a good developer you must not reinvent the wheel. But sometimes, the wheel works for you but doesn’t align well with the vehicle. What should we do in such situations? Should we consider using an available library/method or create our custom code? This is one of the major decisions a developer has to make while coding an application/a website/a mobile app. Recently I was reviewing 2 different projects and both had a very basic problem.
The first project was in WordPress. The website has 1000s of users, and for all users there is a CRON that inserts 2 columns in the database. The user can edit the value from frontend. 2 double values nothing more. The admin can see the records in the backend in a very nice looking List and edit the data in a nice looking edit screen. That’s because the developer had used Custom Post Type to do this. At the very first glance it will look like a very good strategy to save time and finish the thing very efficiently. But now it’s a big disaster. Let me explain it to you “How?”.
WordPress is no doubt a very good platform to build websites that can cater a number of types of industries. It is actually a very powerful tool for developers as well. But when it comes to choosing between Custom Post Type or a Custom script. Then most of us prefer to use Custom Post Type. Because of its ease and quick turnaround it’s famous among developers. But when we should actually use Custom Post Type? The simple is very easy, whenever you need at least 3 of the following:
1. Field similar to title
2. Description3. Taxonomy (simply called Category)4. Featured Image5. Most important is Meta Data
If you only need #1 or #2 or just a few more but not from 3,4, and 5 then it’s good to not to choose Custom Post Type. FAQ:Q: Why? A: Because a Custom Post Type saves a lot of data into a Database table that is not of any use in general purpose.
Q: What does that mean? A: It means that you are compromising the performance of the whole system. With an increased number of records the performance will reduce further.
Q: How do I save the data without custom queries and less efforts?A: There is a global WordPress Database variable called $wpdb. It has all the methods to save, update, delete and retrieve the data i.e. CRUD.
Q: How will it increase the performance?A: The number of columns will be way lesser than a Post and Post Meta table. You just keep the data that you need. While selecting data from DB you just fetch what you need, not everything. So the lesser amount of the data will lead to high performance.
Q: What if I have to migrate the code to another Website and forget the Database table?A: The best approach is to develop a plugin and place the Create table query in the plugin activation hook. Whenever you migrate the plugin the Database table will get created automatically with plugin activation.
Concluding the WordPress project, I think Custom script was a better solution than Custom Post Type.
Now another project. It was a PHP framework that was developed by the website developer himself for the website I was reviewing. It was used nowhere else and will never be used again in future for other websites. Because it is fragile.
The code was well written, nicely formatted, good documentation, use of OOP and OOD was very impressive. Here in this project we can see that the developer has developed from scratch for the client and spent a lot of time developing that framework. But on the other side the functionality (business model) was very simple and should have not taken more than a few weeks. I can imagine that the developer has spent a lot of time building that framework alone.
Now the situation here is very different from the WordPress project. Here, the developer has made a mistake to do custom code while there are 10s of famous PHP frameworks available with exactly the same functionality. Why is this custom framework a problem in this project?
1. Too much time to build custom framework
2. Increased cost of project
3. No future updates available if the developer left the project.
4. No online help available to handle problems or issues.
In both cases we observed that both developers had the option to use a Custom Script or an in-built method to solve the problem. But what they opted was not correct. There might be some conditions when they took the decision and built the code or chose the option. Honestly speaking, I am not saying that they were wrong or they don’t know how to code. It was just to share an experience. So that if you are in the same situation in the near future you can make the right decision. I hope this post will help a few of you reading this article.
If you have any questions, feel free to comment.