Why you should NOT choose PHP for your next project

Despite having a myriad of problems, PHP is still the most popular choice for new web based projects. Whenever someone asks my opinion regarding the language of choice for a new project, I immediately shy away from PHP and suggest something else that fits the project well. Yet many end up blindly choosing PHP, often for the wrong reasons, for example:

“PHP is an industry standard”

… and so is polluting river water. Just because PHP positioned itself as an industry standard doesn’t mean it’s a Good Thing. Choosing it because of that reason misses the point, you are not trying to choose a programming language for everyone. You are trying to choose a programming language for your own project.

“But facebook uses PHP!”

Google uses Java, Apple uses Objective-C, Microsoft uses ASP.NET. One (or a million) success stories of a company that just happens to use PHP does not mean “1. use PHP ; 2. become rich”

“There are a lot of great tools built on PHP, such as WordPress and Drupal”

These are indeed great tools (this very blog uses WordPress,) they are easy to install and easy to use by non-developers. Your project might even work well enough by just downloading Drupal and installing some contributed modules, in which case PHP is the best choice in the same way that premium gas is the best choice for that new Toyota you just bought.

If you are creating something a bit more complex, remember that Drupal and WordPress are in constant development for the past 10 years, and they started out back when PHP was the only legitimate choice if you were against Microsoft technologies. Who knows if they would have made the same choices today?

“My client wants it”

This one is a bit tricky. Your client wants something stable and maintainable. Your client wants to know that if you begin to overcharge them on maintenance, they can replace you (and that’s a Good Thing!*). Your client just read an article in some business magazine that mentioned PHP as The Best Thing Since Sliced Bread.

PHP isn’t stable and isn’t maintainable. If you believe you can cut costs and development time by using a technology that is really stable and maintainable, convince your client.
If your client insists, well

and of course, my personal favorite:

“I personally prefer Python**, but there aren’t a lot of Python developers around”

That’s just wrong thinking. Whenever a new developer joins your project there is a period of several weeks when the new hire is learning. Their effectiveness at this time is laughable. Developers can learn new languages and tools, as well as your own project, in a surprisingly short period. Just give them a chance!

When you are looking for team members, don’t look for the languages section on their cv. Ask yourself if they understand the MVC architecture, if they can write efficient asynchronous methods, if they know when it is proper to sub-class, etc.

If your project is using PHP, would you choose the person with 5 years of PHP experience? Or the person with 1 year of PHP, and 10 years of Python, Java, C# and Haskell? What if the latter had no experience with PHP? Would you still think this person is unsuitable for your PHP project? I’m willing to bet that your answer is “no”. So why choose PHP because there are more developers who use it?

Obviously this example was reduced to the extreme basics, but the point is still clear. Do you really want to choose your project’s technology because you can’t afford two weeks of little productivity for new hires? You’re just cheating yourself into thinking you can skip this slow period.


I think PHP should die. Rasmus Lerdorf, the person who came up with PHP, once said:

I’m not a real programmer. I throw together things until it works then I move on. The real programmers will say “Yeah it works but you’re leaking memory everywhere. Perhaps we should fix that.” I’ll just restart Apache every 10 requests.[1]

If this doesn’t cause you to raise your eyebrow, this post isn’t for you.

What should you use instead? Use Django, use Ruby on Rails. My personal experience with these two made me very happy. Use node.js, use ASP.NET, use Java. Don’t use PHP. Your developers will thank you.


* If your company grows and you want to increase your charge rate, it means that maybe that old project is just a hassle for you to maintain at this point
** … and it’s always Python when this reason is used


This post doesn’t belong in this blog’s standard subject, but I recently finished my 4 year run at a company that uses Drupal and PHP exclusively. I personally stumbled on many of the problems described in the blog post linked to in the first paragraph, and lots of people recently asked for my opinion regarding their choice of technology for their next project. I decided to put this in writing and publish it so I can easily refer them here.

I also decided to bluntly refuse any job offer that touches PHP. I want to distance myself from this world. It’s a principle thing.

Version control systems with adaptable source code formatting

One of the biggest challenges for geek programmers when starting a new job or project is getting used to a new of source code formatting. Each and every one of us is used to our own way of formatting source code and vehemently defend it while denouncing every other way as “unreadable” and “doesn’t make any sense.”

When beginning a function implementation, do you put the curly bracket on the same line or in the next? Do you use else if or elseif? The compiler (or interpreter) really doesn’t care, why should your teammates do?

A VCS can be extended to allow each individual programmer to choose their own source code format. On commit to the repository, the VCS will save the source code in its most condensed form by removing all extraneous whitespace that the compiler tends to ignore, for one. On checkout, the code will be formatted according to the choice of the programmer and everyone will be happy.

Of course, this requires the VCS to be aware of the language in use. If the VCS replaces else if with elif in a .c file or removes all whitespace from a .py file the code won’t compile. Another problem is that auto-formatters won’t allow deviations from the format. A programmer might choose to refrain from using the standard format on one piece of code to increase its readability. For example, by adding line breaks in the parameters list when calling a function with a lot of parameters:

...
boolean status = getStatusByForm(
  status,
  "Hard coded string",
  variable1,
  variable2,
  complexType1.getValue1(),
  complexType2.getValue2(),
  SomeEnum.ValueOne,
  SomeOtherEnum.ValueTwo,
  variable1,
  variable2,
  ...
  variableN);
...

Auto-formatters can be told to either format all function calls like the above, or to add line breaks after a certain number of characters was reached:

...
boolean status = getStatusByForm(status, "Hard coded string", variable1,
  variable2, complexType1.getValue1(), complexType2.getValue2(),
  SomeEnum.ValueOne, SomeOtherEnum.ValueTwo, variable1, variable2, ..., ...,
  ..., ..., ..., variableN);
...

Another example is formatting a long list of variable initializers in a table:

void example() {
  int              status              = 0x00;
  String           myName              = "Daniel";
  String           nameOfFavoritePet   = "Kangaroo";
  FavoritePetType  enumFavoritePetType = FavoritePetType.Marsupial;
  ...
}

which is lost in almost all auto-formatters:

void example() {
  int status = 0x00;
  String myName = "Daniel";
  String nameOfFavoritePet = "Kangaroo";
  FavoritePetType enumFavoritePetType = FavoritePetType.Marsupial;
  ...
}

This proposed VCS extension doesn’t free programmers from following the team’s standard practice in everything, but it can liberate them from the most vexing and meaningless argument: Tabs, or 4 spaces?

Adding logic programming to object oriented programming

This post will use Java (again) as the language of choice, but the concepts presented are relevant for most object-oriented languages.

A classic tutorial for understanding concepts of object oriented programming is the class Animal which is subclassed into Cat, Dog and Panda. This example usually gives Animal as an abstract class with an abstract method called makeNoise(), which all subclasses must implement.

Here is a slightly different example:

abstract class Animal {
  ...
  abstract boolean isAlive();
  ...
}

Each subclass must implement the isAlive() method. The Cat’s method indicates whether the cat has spent its 9 lives, the dog’s checks if its tail is wagging, etc. But what about the subclass Zombie? Zombie objects will always return false for isAlive(), which makes the implementation of the method unnecessary. Instead of writing a method that simply returns false all the time the language could be changed in the following way:

class Zombie extends Animal {
  ...
  false isAlive();
  ...
}

That results in less code and makes it clear that this method is of no interest in this particular subclass.

 

Expanding on this idea, switch statements, which control a method’s response for parameters, could be made with a construct that is similar to overloading:


public class SomeHandler {
  
  enum State {
    STATE_OK,
    STATE_FILE_NOT_FOUND,
    STATE_NO_DATA_CONNECTION,
    STATE_GPS_INACTIVE,
  }
  
  true handleState(STATE_OK) {
    // Do something when the parameter is STATE_OK,
    // but always return true
  }
  
  boolean handleState(STATE_FILE_NOT_FOUND) {
    // Do something when the parameter is STATE_FILE_NOT_FOUND,
    // notice that this time the method isn't declared to return a constant value,
    // therefore it must return a boolean value by itself
  }
  
  false handleState(State state) {
    // Do something when the parameter is neither STATE_OK nor STATE_FILE_NOT_FOUND,
    // but always return false
  }
  
}

Similar to a switch statement, the first matching signature will cause that method to run and return either its pre-declared value or the value that it calculates during its run. The default case being the last method, which must be declared or otherwise a compilation error will occur. Readers familiar with Prolog will find this idea very familiar.

In the above example, if the “default” state should always return false, the following handler, which is much simpler, could be used:

public class SomeHandler {
  ...
  false handleState(State);
  ...
}

This way if the method is overloaded with another method signature (such as boolean handleState(String)), the runtime environment would still know which of the overloaded methods to call depending on the parameters.

 

While logic programming never caught in the industry, I believe it has a lot to offer. As an aside, if you never touched Prolog in your life and you’re interested in trying something completely different from what you’re used to as a programmer, now would be a good time to find a good introduction to it.

Readable Math

A while back I was working on a proof-of-concept for an Android game when the following code found its way to the codebase:

It’s readable, but it might take a moment to realize what goes where. What if that method could look like this?

While this example is relatively easy to read and understand, the following example is less so:

And with math styling:

Suddenly it seems familiar, right?

This feature can be easily implemented in existing IDEs as a plugin. Programmers will write the expression and select an option in the IDE that will convert the expression to a stylized display – behind the scenes a LaTeX code will be generated and rendered. The IDE will then display the stylized expression and the compiler will get the equivalent expression in a form that it can understand. Programmers will have the ability to edit the actual LaTeX code so that they can style it however they choose, and the feature will make sure that the expression is always in sync with its LaTeX presentation.

Of course, we also need to think about programmers that don’t use an IDE that supports this feature – this can be made to work by injecting the LaTeX code inside comments that surround the expression. An example of how this will look in the source file (which still remains in text format) as well as in an IDE that doesn’t support this feature:

Why the duplication? There are many ways to format the same equation in LaTeX, and if we auto-generate the LaTeX every time the file loads in the IDE it might not be formatted as the programmer wanted it. Granted, this is not the most readable or elegant solution. I’m sure someone can think of an implementation that’s more readable and more stable from a technical standpoint.

 

This feature idea opens up new possibilities for embedding more intuitive formats of code inside the plain-text. Perform operations on a matrix, replace method calls with equivalent mathematic notations (such as replacing Math.abs(x) with |x|), etc. Any ideas of your own?