Categories Tags

Why should I use a framework?

I've heard and seen the following so many times.  "Why should I use a framework when I can just code my own framework?".  Or, "using a framework slows down the application, and I need it to be speedy.  So I need to use raw PHP".  Yes, those two statements are 100% accurate.  You can build your own framework, and technically yes your code would hypothetically speaking be faster if you wrote barebones PHP.  

However, script speed isn't the only thing you should be worried about!

There are several PHP frameworks out there that are being fairly actively worked on. Let me ask you a question.  What do you think would work better?  An application/framework that is being developed by one individual who has a certain set of skills?  Or, would you rather use a framework that is being actively worked on by a bunch of software developers around the world who are actively talking about ways to improve?

To me, the answer is simple.  Use the framework.  I have been developing software for over 10 years now, and I still find myself learning from those around me.  Be it through code reviews from co-workers, or from merge requests and issues logged from other people on GitHub.  Yes, I have a fair bit of knowledge but everyone picks up things differently.  Could I make a decent framework?  Possibly, would it benefit from other people's input?  Probably.

The fact that something is "open source" doesn't mean that there is nobody monitoring check ins and updates for it.  Take Laravel for example.  When I wrote this post, there is a total of 294 people who have contributed fixes to the framework.  However, they have core maintainers.  It is these people (who are normally core to the project and have proven themselves already), that are reviewing every check in that someone does and making sure it meets the standards that they wish to enforce.

"I like to know everything about what is going on in the application".  My answer to that is who cares?  Do you know how the operating system that your application is running on works?  Do you know the inner workings of PHP converts the code you write to code that the computer can understand?  Likely your answer to both of these is "no", so why do you need to know each and every inner working of the application too?  Yes, it helps when you are trying to design and write the code (to some degree), but it shouldn't change your mind.  Treat the code that they give you like an extension on the PHP language.  But more than that, let it help guide you on what some best practices are / may be when you actually start building your application.

"It is all written by other people, so there could be bugs."  Yes, this is true.  Just like PHP can have bugs.  Code has bugs no matter where you get it from so don't let that be a deterrent.  Is your code 100% bug-free?  Probably not.  So why not let others handle the core of the framework while you reap the benefits when they have bug fixes available?  Lots of frameworks use unit tests in order to test and validate each "unit of work", so they work on proving their code works.  And if a bug is found, more unit tests can be added to ensure that they don't crop up again.

If you write your own framework, are you able to search Google for your specific issues that you have?  Likely not because it is fairly hard to give enough information if you built a framework because you need flatten it out as much as possible.  With a framework, you are able to Google the framework name you are using and your particular question.  Or, post on the framework's forums.  Both places will likely get you a much better answer and, depending on how active the community is you could have a response in a matter of minutes.  Sure there are moving parts, but there are also more people willing to help to lubricate them.  So why not?

"But there is so much to learn if I use a framework". Yes, yes there is. Learning curves aren't necessarily a bad thing. They are a way that you become a much better and much more rounded developer. They help you to flex to expand what you know which in turn allows you to make better decisions going forward.

"What do frameworks buy me?" Most frameworks come with at minimum an easy way to interact with your database. By profession, I am a database developer. So I don't mind writing SQL queries. But if I'm given the option to have them auto-generated based on a object, why wouldn't I take it? It's 1 less thing that I have to manually do. It's these small efficiencies you gain through the development of an application that allow you to:

  1. Create the site quicker
  2. Make the site more robust
  3. Make most more easily maintainable code

Not writing SQL queries isn't enough for you? Then how about the fact that for the most part, the use of a framework will not necessarily force best practices on you. But, they do their best to help push you in the right direction! I've seen so many sites which have code that is a nightmare because essentially every layer is tangled together. You have the database being directly summoned by the front end and huge "if-then-else" statements that start forcing more and more levels of indentation which can get confusing. I'm not saying you can't do this in a framework. But, after using it a bit, you will quickly realize that it is not the right thing to do. The "Model View Controller" architecture has been around since the late 1970s and is still a prominent setup. Because it pushes you to ensure that every section does it's own work and that is all.

Sure frameworks can cause an extra little bit of processing time in your application, but that is nothing a little extra RAM, a better CPU or even, an SSD cannot solve. If you are able to determine that those are your bottlenecks that is. For the most part we are talking milliseconds which isn't much time at all. Your system could be processing someone else's request at that time. However, you will make up for it in the amount of code throughput you can have as you work on your application.

If you ask me why do I use a pre-existing framework, the answer is simple:

  1. Support of a Community - I trust the developers around the world to come up with solid frameworks and the communities that back them.
  2. Time Efficiencies - I don't like to write a lot of the boilerplate code that you need to when writing from scratch because they take time and don't provide a lot of value.  Let the "magic" happen.
  3. Code Maintainability - I want my code to be maintainable in the future. Yes, it is possible outside of a framework. However, it is a lot harder.
  4. Learning - By reading through the code that exists in the framework, I am able to pick up a few "neat tricks"

Hopefully this helps to guide you in your decision of whether or not to use a pre-existing framework or to make your own a little bit easier!

Posted in programming

Tags: