Categories Tags

Blog

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:

So, I was doing a consultation this week regarding some database work and got to hear a little horror story from the owner of a site.

Please Note: The owner of the site has next to no programming experience.   The only experience they have is dealing with the code that they were originally sold.

The owner of the site contacted me because they felt there had to be another way of handling this particular situation.  What the problem boiled down to was the fact that they had 1 large table that had more than 256 columns in it!  And better yet, the columns that were actually needed depended 100% on the "earlier" content of the table (i.e. the first few columns).  To me, I immediately saw this table and was appalled.  I thought to myself, well this obviously needs to be a mapping table, and treated as such.

After talking with the owner for a bit, apparently this was their first inkling too.  However, the programmer "swore up and down that it could only be done this way".  Hearing that saddened me a little.

Now, you maybe wondering why this blog post is called "My Way or the Highway".  That's because one thing that I have learned in my years of programming is the fact that there truly is no "one right answer".  In fact, every time I work on a programming problem, I myself come up with 3-5 alternatives before narrowing the problem down into which solution I'm going to take.

In order to develop applications, you need to take a few things into consideration and most importantly, leave the ego at the front door.  In my opinion, this holds true for more than just programming exercises.  Someone else to bounce ideas off of may help you see the problem in a way that you never even thought about before which could lead to a better overall application.

After peering through the code a bit more, it was clear to me that the reason why "it was the only way this could be done", was because they wanted to be lazy when handling access in other pieces of code.  All they really needed to do in order to make this mess manageable was to wrap the database call in some sort of data access layer.  When they do that, they would be able to handle any updates/inserts that are needed and still send back all of the data that was required to process the data instead of that and then so much more.

In short, when programming, try your best to keep an open mind and focus on other alternatives and please don't take the solution that "is done fastest" because that normally leads to pain points and wasted resources in the future.

Had this programmer done what the owner, who was speaking from what logically made sense to them, listened to them and re-evaluated their statement, then I wouldn't have likely needed to be brought into this discussion at all.

I've been programming for years now and I still realize that I know just a tiny bit of the broad spectrum that coding provides.  So, that is why I always ask others for their opinions and leave my ego at the door because at the end of the day, I want the best product out there.  Not a decent product that is filled with patches that is being held together with bandaids and chewing gum.  You only get that solid result from reaching into the depths of programming experiences both that you had, and that everyone else has had in order to get the best results!

Thanks for reading!

Posted in programming

Tags:

Problem:

Capitalizing the first character of each word in a string (i.e. “the final countdown” → “The Final Countdown”).

Solution:

C#:

C# has a built-in function for this. Its called ‘toTitleCase’, hidden deep within the System.Globalization namespace.

So how do you use it?


using System.Globalization;
 
...
// Get the instance of the TextInfo class to use to (no constructor), comes from the current thread
TextInfo info = (System.Threading.Thread.CurrentThread.CurrentCulture).TextInfo;
 
string sample = "hello world";
 
// Print to console the title case
// Outputs: Hello World
Console.WriteLine(info.ToTitleCase(sample));

The ‘ToTitleCase’ function returns an instance of a string which will have all of the first characters in words changed to upper case, and leaves the rest of the text as is. This means that if a word is in all capital letters it will remain that way. A simple work around for this is to call the string object’s ‘ToLower’ function before we send the string into the ‘ToTitleCase’ function.

For example,

using System.Globalization;
 
...
// Get the instance of the TextInfo class to use to (no constructor), comes from the current thread
TextInfo info = (System.Threading.Thread.CurrentThread.CurrentCulture).TextInfo;
 
string sample = "HELLO world";
 
// Print to console the title case
// Emits: HELLO World
Console.WriteLine(info.ToTitleCase(sample));
 
// Pre-lowercase everything
// Emits: Hello World
Console.WriteLine(info.ToTitleCase(sample.ToLower()));

PHP:

The PHP version of this function is a fair bit easier to get to.  PHP’s function is called ‘ucwords’.  However, similar to the C# version you should always have the string sent in in lower case if you want it to only make the first character of each word upper case (it only changes the first character and doesn’t touch the others).


// Outputs: The Final Countdown
echo ucwords('the final countdown');

Posted in programming

Tags: