8

IT Enthusiast — Is JavaScript better than PHP

 2 years ago
source link: https://rodiongork.tumblr.com/post/109573287158/is-javascript-better-than-php
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Is JavaScript better than PHP

It is a quite popular thing to claim that PHP is a veeeeery baaad language. Often these claims are made by people who do not have much experience with PHP, but have read some great articles like PHP - a fractal of bad design.

When I ask “which language you recommend instead” - about 50% of these people shout JavaScript for sure! I always suspect there should be something wrong, since JavaScript has many features similar to PHP - c-like syntax, lack of strict typing and lack of complicated data structures offering instead array-on-steroids.

I myself extensively use both (as many as several others) and so I decided to make some comparison.

Can you guess why JavaScript sometimes looks similar to spork like this?

image

1. Basic values

PHP is popularly blamed for usage of two equality operators - double and triple ones. But it is just the same with JavaScript. Moreover you surely encountered many funny things like these:

  • “ \t\r\n” == 0 // yields true
  • “false” == false // yields false
  • !!null == null // yields false

Of course JS have explanation for all such behavior. But it does not make more sense usually :)

2. Basic operations

This is really great. If you use some wrong operations on some wrong operands - in PHP you get an error so you can fix your mistake. But not in JS:

  • sum of two arrays [] + [] yields “ (empty string)
  • sum of array with object [] + {} yields an ojbject
  • sum of object with array {} + [] instead gives you 0 (zero)
  • and sum of two objects {} + {} is NaN (undetermined numeric value)

Subtraction behaves in a different manner, but you may have fun testing it yourself! Using empty string instead of array or object you may get few more curious results.

What is important - no error is generated! So if due to loose-typing your variables get some wrong values (it happens often enough, you know) - you will not at once detect something goes wrong.

3. Basic API

It is important to have a good library of tools at hand. Library functions provided by PHP are often blamed for performing too various functions and their names being spelled in different styles (due to inheritance from C).

Meanwhile in JavaScript:

  • you have substring and substr and you never remember which is what
  • if you want to format number (as with sprintf or number_format) you usually can only use few manual tricks like taking substring of number prepended with string of zeroes etc
  • some methods behave differently across implementations - famous example is with split - less famous with forEach on arrays (not jQuery-based one) which in certain implementations (like Rhino) for array with only two indices 0 and 100000000 will enter a loop of billion iterations.

Of course all these are only minor annoying things, I just mention them to express my point of view that there is nothing JS could boast.

4. Object-Oriented Design

JavaScript simply has no proper implementation for OOP. Yes there are functions and their prototypes which could be used in some verbose and clumsy manner to mimic OO design in some or other way depending on your needs. But far not covering all fundamental principles of OOP.

Meanwhile PHP provides all things about classes, interfaces, methods, constructors, fields, packages, their visibility etc in very similar manner to how Java does this.

Of course JS-guys usually answer "we do not need it. we get used to prototype-based design” however it looks the same as grown individual who got used to diapers.

I’m sorry to say this, but when almost every contemporary language have all necessary features for writing OO-programs, it is strange to say that “we do not need this, we prefer mimic this with some makeshift substitutions”.

5. Using JS + Node + Mongo architecture

The popular thing is to say that with JS you can write the whole application in a single language. Just use it on client side, on server (as Node.js) and in the database (MongoDb).

However this idea looks nice only until you really try it. When you really try, you find out several nasty things:

  • you still need to learn quite different versions of JavaScript - some front-end frameworks, NodeJs API and API for Mongo - so the same language behind them does not help much
  • using NoSQL database for your business-data is ok only while you have few collections and need no complicated queries to them involving joins - usually you found this mistake too late because your customer asked a new kind of report and you see that hundreds lines of code should be written manually instead of three lines of SQL query; there is nothing to do with it - Mongo is a special database, for special purposes - and it is better to use it for such purposes, rather than try to fit it as a universal tool for every need.
  • the last is most important for beginners - to host NodeJs server with MongoDb you have far fewer choices than with PHP and MySQL - for free you can try it mainly at Heroku and OpenShift - or you can use paid plans there, or purchase VPS, however with latter you’ll need to care of setup yourself.

So though it initially sounds interesting, really it appears that the only (vague) advantage this approach offers is “you use a single language”. But is it really an advantage? Look at this picture above - now you know why it is there!

It is one tool to be used for several purpose. But I bet you do not want it to be offered to you at restaurant. You prefer to use spoon for soup and fork for beef.

The same is with building web-applications - it is easier to learn several proper tools (e.g. HTML+JS at frontend, PHP at server and SQL for database) rather than to struggle with universal one.


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK