Blog-Archiv

Samstag, 21. Mai 2016

JS Semicolons

Recently I noticed the hard-to-read source code of bootstrap.js, and I investigated a little on the internet. What I found were two prominent JS authors, one representative of the old programmer generation, one of the young, and their conflict:

"That is insanely stupid code. .... Learn to use semicolons properly."
"I have learned to use them, that's why there isn't one present."

It was about the fact that a minifier did not correctly pack bootstrap.js source, because that source makes excessive usage of the freedom that the JS language provides. When you look into it, you see that actually there are no semicolons at all, except the ones in for-loops, and on "use strict". And there are also a lot of syntactical constructs you see nowhere else. You could say, they heavily try to be unique, or you could say, they have found a better way to use JS. For sure it is hard to read for the average JS programmer that needs to understand why his bootstrap dialog does not show up. But actually no one can say what is right or wrong here.

Until now I couldn't find out why bootstrap authors do not use semicolons. Here is how I interpret that attitude. Look at following JS source:

var a = function() {
  return
  5
};
var b = function() {
  return
  5;
};
var c = function() {
  return 5
};

alert("a(): "+a()+", b(): "+b()+", c(): "+c());

Output is:

a(): undefined, b(): undefined, c(): 5

In this code, you have a() and b() doing the wrong thing, due to JS auto-semicolon-insertion, but c() doing the right thing, although the semicolon is missing.

The question rises:

when the semicolon did not fix the problem in b(), why use it at all?

What we are expected to learn here: semicolon is only a statement separator, not a statement terminator.

Some authors recommend to put semicolons just before an expression that starts with one of +-/[(.
Some recommend to put them everywhere.
Some recommend to leave them out completely.

I am not a JS expert, just a user. My primary goal is to write readable, comprehensible and maintainable source code. But I admit that I also have developed my personal style to write JS code. And I assume that every JS programmer has.

The big freedom of expression in the JavaScript language leads to a lot of different dialects. That means, you might not be able to read or understand the JS code of your fellow developer. I call this a communication bottleneck.

If you have read my older Blogs about all these problems in JS, you may understand my conclusion that ...

JS is not a language for corporate development.

The punchline here is that bootstrap originally was created to better organize corporate work within Twitter :-)

We are not alone on this planet, so let us not forget that communication is the thing that makes us successful. When we want better new solutions, we need to learn from the mistakes of the old ones.


P.S.
You want to know why they start each IIFE function with a plus-sign?
By doing so they avoid to write the enclosing parentheses. It's a trick to put the JS interpreter into the same parsing state.

P.P.S.
Yes, great idea, we also should drop indentations. Spaces are doing nothing, so why write them. Maybe we even can get rid of that programming language?




Keine Kommentare: