JavaScript and PHP – Just Take the Red Pills and Be Happy in Wonderland

Matrix Pills by YPT-Love-Me-Dead ( I want to say Happy 2015 everyone! It seems to me the more frustrated programmers get with their code problem domains the more likely they are to take it out on the languages they are using. Naturally JavaScript and PHP both take the brunt of the abuse because of their popularity, sometimes necessity and of course their numerous implementation oddities. A lot of programmers working with these languages == a lot of programmers needing to complain about something. Are these languages really all that flawed? Heck yeah! But I argue that it is also their strength at times.

In the movie “The Matrix” Morpheus tells Neo that if he takes the blue pill “the story ends, you wake up in your bed and believe whatever you want to believe” but that if he takes the red pill “you stay in Wonderland and I show you how deep the rabbit-hole goes.” For sure JS and PHP are the red pills of the programming world. You can find tons and tons of examples of why a seemingly illogical statement should not work yet it does in these languages. The popular examples often used are ones where two types of objects are compared and while the statement should be false it comes out true. In the case of PHP people point out the lack of true OOP principles or a demo of using type juggling that just goes horribly wrong. Yes, many of us get that. You like languages that are strongly typed or somewhat regimented. Believe me I too like some of that structure in languages like C# or Java.

However, I find both JS and PHP entirely fun to program in and I have created stuff that I know would have been twice as long and twice as complex in a more strictly enforced language. Yes you have to use a little more caution when dealing with a language that features rules where “Some of them can be bent, others can be broken.” But in some cases this might be exactly what you need… as long as you know how it will be bent or broken.

I find both of these languages incredibly flexible. This flexibility often times leads to code that is shorter and gets right to the point without any intermediate lines of code. Ever work in C# and need to have this one type of object returned but you have another object type that you then have to some how cast over? Those lines of code to cast exist purely to usher one object type to another. I think we all know how verbose Java itself can be in that department! Most of the time JS and PHP can do that same conversion without needing these filler lines of code.

Where I find JS really excels is defining quick object literals both for returning data from a function and simply whipping up a custom object on the fly. You have a few little pieces of data related to one another but not something you really want to have to build a class or structure for to then instantiate and use. You quickly throw the bits of data together in some curly braces and you are good to go. One area I really like about PHP is the type juggling hands down. Especially when it comes to function return types. I have a function that returns an object of type A, later down the road I want to get into that function and instead return type B for some odd reason. I don’t have to mess with changing the types on variables that might hold the return value of that function. In other languages where you have a defined type, unless they share some kind of base class, you would be forced to change that variable type to something of type “B” in addition to altering everything that uses that variable. PHP would just need to tweak how it uses the variable.

I guess I am saying I am happy to have taken the red pills of JS and PHP and seeing how far the rabbit hole goes. If you are a good programmer, you know where the pitfalls are in the languages you use and you simply avoid them. In JS don’t do silly comparisons of “1” == 1 or crazy nested callback hell with a 1000 scopes and most of the time you can steer clear of the problems. Do this and you can enjoy all the intentional rule bending you want. I believe the best programmers learn when to bend a language construct and when not to. Believe me when I say I have worked with both languages over a decade and once in a great while these crazy antics of the languages bite me, but it is a small price to pay to live in Wonderland. Life is too short to complain about it.

Thanks for reading! 🙂

About The Author

Martyr2 is the founder of the Coders Lexicon and author of the new ebooks "The Programmers Idea Book" and "Diagnosing the Problem" . He has been a programmer for over 25 years. He works for a hot application development company in Vancouver Canada which service some of the biggest tech companies in the world. He has won numerous awards for his mentoring in software development and contributes regularly to several communities around the web. He is an expert in numerous languages including .NET, PHP, C/C++, Java and more.