(16th December 2013)

Compiling (Java) vs Scripting (PHP)

As a Front End Developer who has mainly worked in large organisations using technologies relying on compiled languages such as Java and C#, this can lead to a number of frustrations.

Depending on the set up, it could be necessary to re-compile a project to view even a basic HTML text change. When working on large scale projects this can take a long time. I have previously worked on projects that take up to 45 minutes to re-compile.

Even with bug bears like this in mind it is important that we look at the bigger picture and understand why decisions are made to use certain languages in the first place.

Compiled Languages

Computers cannot actually read or understand the code that we produce. It must be translated into a format that the computer can interpret (bits and bytes). This translation process is called compilation. The output of the compilation process is code in a format that machine-readable and not usually understandable to the human eye. Some examples of these languages are C, C++ and Java.

Scripting Languages

Languages such as Python and Ruby do not require a compilation process, and use interpreters to convert the source code on the fly to a machine-readable and executable format.

Working with PHP

PHP uses a combination of interpretation and compilation to give programmers a good mix of performance and flexibility; especially Front End Developers when working on the fly is of paramount importance.

Under the hood, whenever your code is accessed, PHP compiles it to a series of instructions (called opcodes). These instructions are then executed one by one until the script terminates. This differs from languages like Java where the whole code is compiled to machine executable format. Only when this is complete it can then be executed. Instead, PHP re-compiles your script each time it is requested.

Constant recompilation may seem a waste of processor time, however, it is not as bad as it may seem. You no longer need to worry about hand recompiling your scripts when you make any changes but on the down side, many scripts take longer to compile than they do to execute.

Furthermore, it provides very quick feedback during development. If an error exists in your file, PHP will refuse to compile the page until the issue is fixed. You are then able to step through the execution of your code line by line until you find the problem.

The performance hit of constant compilation is removed entirely by the use of PHP accelerators.

One major advantage to using interpreted code is that all memory used by the script is managed by PHP, and it automatically cleans up after every script has finished. This means that you do not need to worry about closing database links, freeing assigned memory, etc… as PHP does it for you. This does not mean that programmers should become lazy as good developers clean up themselves, and let PHP work as backup in case something is missed.

That all sounds great, so what is wrong with PHP?

Putting it bluntly PHP has more holes than Swiss cheese! Although its flexibility is appealing and allows for pretty quick development, it also makes it too easy to build a spaghetti code base. Its instability compared to other languages makes it difficult to consider for adoption in large-scale projects. Although it can be a cheaper option, larger corporations will usually rely on a more stable product in some cases taking a hit with extra financial overheads.

PHP became popular for web development as it integrates well with existing web tools such as inline HTML, good Apache integration and built-in MySQL support. It did not become popular because it is well-designed language.

It was originally designed as a basic templating language, which has grown and been adapted from there. This leads to a number of strange quirks for a general purpose programming language (magic quotes are a great example of this). Amateurs and beginners love PHP, as they do not need to spend a lot of time understanding things like OOP principles as it lets you do things in next to no time.

PHP is easy to install, and packages like XAMPP get you going in no time at all. Unfortunately, this is where the fun ends. Lets look into just a few of the most common pitfalls.

Parsing text such as a HTTP form input is incredibly tricky to get right. Even if you have configured to override PHP’s string functions, probably breaking the commonly used mail() function, the character-set support is incredibly bad.

Some security-critical functions break if used with certain character sets. Many developers especially beginners will probably not understand character sets and therefore completely oversee this issue.

Organisations with mostly western character-sets never stumble over this problem until their site gets so popular that someone tries to leave a comment in Russian and the page in their application suddenly falls apart. This commonly occurs when organisations in the USA forget that there is a world that exists outside of America.

The quality of PHP’s built-in libraries differs widely, as does the quality of all libraries. In most other languages, the core libraries are designed to a certain set of standards. It seems that PHP’s developers couldn’t decide if they prefer the underscore method of function_naming() or Java-style functionNames(). So they just used both.

PHP does seem to have one of the most approachable syntax’ and this is probably one reason why it is so popular along with the availability of many sophisticated applications, content management systems and other tools such as PhpPgAdmin and PhpMyAdmin.

From my point of view, PHP is only really acceptable for simple solutions. The issues mentioned above are just a few of the most common shortcomings.

So why do big corporations use Java?

Many companies have systems that have to be maintained for a long time, and constantly need to be able to recruit staff with the skill set to do this. With this in mind, it is important that something popular and standardized is used. Java is very popular for writing big business test automation systems. For large organisations that have a lot of assets to protect, this is of significant importance and will always be taken into consideration when choosing a language.

For similar reasons, it is important that you use something stable. With many dynamic/scripting languages, 3 years is considered more than enough notice to deprecate a language or feature. With Java, you can still run an old program without modification. Again, this is hugely important in regards to test automation and maintenance.

The presence of other big corporations backing a language and the associated set of libraries and tools reassures company decision-makers that it is stable, reliable and support is good and won't go away any time soon. If this is true or not, is cause for debate. Python, PHP, and Ruby are originally hobby/academic projects, and have grown some corporate support, mostly from rather small firms that do not impress typical Fortune 500 companies.

Sometimes, Java might be the best option. Although not really in my area of expertise, Java is well known to have a good threading model which is either poor in languages like Python or non existent in PHP. Also, while compiling a project may slow down exploratory programming it does increase the number of bugs that can be statically caught.

It’s Mainstream

This is probably where the most consideration goes when choosing a language and the reason is to mitigate risk. Mainstream languages are proven, stable, thoroughly debugged and strongly supported which is why they are widely adopted. It is easy to find programmers worldwide who know the language and training material is never an issue.

Productivity

Big companies frequently outsource on a big scale and the cost of programmers becomes important. Where the majority of work is maintenance and minor feature additions, the focus is usually on getting the code complete as quick as possible. Whichever language achieves this is generally the one that is given the strongest consideration.

Certified

An Oracle (Sun) certification makes it easy to assess a programmer during the recruitment process. This is usually a good indicator that the programmer knows the basics and will be easier to train than one who is not certified. When recruiting for large teams and costs are important this can save time when triaging the stack of CV’s.

Choice

Java has a vast array of libraries, frameworks, tools, IDEs, and server providers. To a large corporation it’s good to have choice, even if it’s just to negotiate on prices. The language lends itself to code quality tools that allow the enforcement of standards.