Live data from Hacker News

Treating JavaScript like a 30 year old language

jeremyckahn.github.com

31–40 of 99 posts

Re: Treating JavaScript like a 30 year old language

#31
I use the Google style guide as well since it's required by my job and while I have learned somethings from it I'm not a fan.

80 characters? I never had that limit for the 27 years of programming proceeding using the Google style guide and it never caused me any grief. I find that naming things descriptively and an 80 character limit are at odds.

I'd rather read

    maxCombinedUniformVectors = maxFragmentUniformVectors + maxVectorUniformVectors;
than

    maxCombinedUniformVectors = 
        maxFragmentUniformVectors + maxVectorUniformVectors;
or

    maxCombinedUniformVectors = maxFragmentUniformVectors + 
                                maxVectorUniformVectors;

I admit it's kind of useful for side by side diff tools but in my previous 27 years of programming I never felt like "If only this code was 80 characters I could read the diff".

More importantly though, the Google Style guide is written by Java programmers to try to make JavaScript into Java, totally ignoring all the benefits of treating JavaScript like JavaScript. That has it's benefits, especially for Java programmers. They don't have to learn some of the cooler things about JavaScript. They can go on treating it like a traditional oop language. And they get static type checking.

On the other hand, all of these FP concepts are out

http://osteele.com/sources/javascript/functional/ http://www.ibm.com/developerworks/library/wa-javascript/inde... http://osteele.com/archives/2007/07/functional-javascript http://www.cubiclemuses.com/cm/blog/archives/000307.html/

Even common JS concepts like encapsulation

    var ErrorLogger = (function(){
      var privateErrorCount = 0;

      return {
        error: function(msg) {
          console.log(msg);
          ++privateErrorCount;
        },
        getNumErrors: function() {
          return privateErrorCount;
        }
      }
    }());
         
Are not allowed by the Google Style guide as well as many other JSisms.

Another nit, Google Style guides disallow formatting for readability except for comments?!?!

Allowed

    var kStateRun = 1;        // character is running
    var kStateRunToWalk = 2;  // character is transitioning from run to walk
    var kStateWalk = 3;       // character is walking
Not allowed

    var kStateRun       = 1;  // character is running
    var kStateRunToWalk = 2;  // character is transitioning from run to walk
    var kStateWalk      = 3;  // character is walking
Either lining things up makes them easier to read or it doesn't. Comments are not some exception. If lining up comments makes them easier to read then lining up anything makes it easier to read.

Re: Treating JavaScript like a 30 year old language

#32
post #17

Doesn't even mention coffescript

It shouldn't have to. He may not have the option to write Coffeescript at his job, or he simply may not want to (for any number of reasons.)

In Scott's defense, I think it's totally valid to bring up CoffeeScript (though perhaps in a more elegant way with a bit more info as to why it's relevant). It's possible the article's author was not aware of its existence (I think this is likely as CoffeeScript seems to address his main concerns about JS).

Even if Coffee isn't in use at the moment, if it provides some value it would be worth promoting within the organization. New technologies routinely pop up, and the onus of proving their worth ultimately lies on the developers, even if they aren't the decision makers when it comes to selecting which ones are adopted and which ones aren't.

Re: Treating JavaScript like a 30 year old language

#33
Though some are good suggestions for writing maintainable codes, I do not see a lot of opportunities to improve javascript from this.

The pieces I would like to have are a) optional arguments and b) strict type checking. They are actually syntactic sugar in a way, because you can get the same effect using typeoff function.

But restriction to 80 characters... would be definitely recommended as coding practice, but never be forced by the language!

Re: Treating JavaScript like a 30 year old language

#34
The use of intermediate variables is something I'm conflicted about.

On the one hand, they can make code easier to reason about. Also, when using a crappy debugger that doesn't display return values you can more easily see what's going on. In some cases they make code that at least looks like it ought to run faster.

On the other hand, ditching intermediate variables makes refactoring more straightforward. You can immediately see the complete set of dependencies of a line of code and extract common bits of code into helper methods with less hassle.

In general I think I prefer the "cram a bunch of nested functions into one line" approach, but then that might be language-dependent (I mostly do Objective-C these days, and Xcode has a pretty smart automatic line-wrapping feature).

Lisp would be an extreme example where that's pretty much all you do.

Re: Treating JavaScript like a 30 year old language

#35

Earlier quoted context omitted.

We're so different, you and I. "Getting code out of sight because it's ugly" is such a foreign concept. If a code block is that ugly, it either needs to be made not ugly or put directly in the line of sight with plentiful comments. "Shouldn't have to read it" !== "won't ever have to read it."

I agree completely with this, all code should be visible, but I still go well over 80 personally because I'm don't, nor will anyone likely edit my code on a 80 char terminal. When I was hacking on an IBM mainframe through a 3270 terminal, the 80 character limit made a lot of sense. Why use the 80 char rule because of an edge case of some throwback editing javascript in a term that only allows for 80 characters? I mea…

Me. When I bring up the Netbeans editor, there is a red line down the right margin at column 80, and I'm not inclined to change that setting, although I sometimes type a few characters past it. This is a wise tradition, handed down from the tribal elders :-) I sometimes need to see at least a little bit of something else on screen besides your hideously wide code.

If you ever have to print out code, it makes a nice line size convention as well. Printing is becoming less common, unless you publish books, but it's still worth considering that it might be easier for the reader if the lines aren't mind-numbingly long.

Why do newspapers have multiple columns of text, when clearly they have space to go 14 inches or more across?

Re: Treating JavaScript like a 30 year old language

#36
I have worked with a strict 80 character rule, and I find that it usually causes worse looking code with excessive multi-line statements, poor variable naming and makes refactoring more labour intensive.

I have a soft limit of around 100 characters for C++, which is good for readability and still allows me to have two side by side editing windows.

Re: Treating JavaScript like a 30 year old language

#37

Maybe I'm an exception, but I generally find code with LESS syntax to be more readable. var makeAdder = function(x) { return function(y) { return x + y; }; } vs makeAdder = (x) -> (y) -> x + y Is anyone else like this? Do you think people are hard-wired to prefer one form of syntax to another, or do you think it's a "whichever you have more experience with" kind of thing?

You can go one step further and make use of automatic function currying (such as in haskell) and just write:

adder(x,y) -> x + y

If you call adder with one argument, you get the same behaviour as your makeAdder (returned closure), but if you call it with 2 args you get addition.

Re: Treating JavaScript like a 30 year old language

#38
post #37

Maybe I'm an exception, but I generally find code with LESS syntax to be more readable. var makeAdder = function(x) { return function(y) { return x + y; }; } vs makeAdder = (x) -> (y) -> x + y Is anyone else like this? Do you think people are hard-wired to prefer one form of syntax to another, or do you think it's a "whichever you have more experience with" kind of thing?

You can go one step further and make use of automatic function currying (such as in haskell) and just write: adder(x,y) -> x + y If you call adder with one argument, you get the same behaviour as your makeAdder (returned closure), but if you call it with 2 args you get addition.

Haha yeah. I don't think CoffeeScript has support for that yet, but I believe LiveScript does.

*I was mainly trying to point out something that is semantically equivalent but syntactically different.

Re: Treating JavaScript like a 30 year old language

#39

"At some point in computer history, somebody (arbitrarily?) created an 80 character line limit for code. ... I’ve been writing JavaScript for three-ish years" For a couple decades, and not ending until the late 90s, most text terminals and text modes for graphics cards were 80 characters wide[1], and dot matrix printers also had an 80 character line length (plus margins) dating back to 80 characters per line punch ca…

Which of course begs the question, why were punched cards 80 characters wide? According to a random StackExchange commenter:

"The cards are that size because in 1890, CTR wanted to reuse currency carriers (the dollar was bigger back then) to carry the census data cards. – Al Biglan"[1]

Assuming this is true, and that the size of the card was chosen to match the size of the US dollar, it can perhaps be assumed that 80 columns was chosen as a reasonable compromize between data density and structural integrity. I'm not sure that this makes sense though, since Wikipedia says that IBM's 80-column cards date from 1928. A more authoritative source would be welcome.

[1] http://programmers.stackexchange.com/questions/148677/why-is...

Re: Treating JavaScript like a 30 year old language

#40

Totally aside, but interesting: 80 characters was not an arbitrary limit, at least not directly. It was the size of the IBM standard punched card, and the terminals that succeeded them. Famously, versions of COBOL (FORTRAN too?) well into the 1990s would not even recognize input past column 80 even though it had long since graduated to text files. http://en.wikipedia.org/wiki/Punched_card I still like to use 80 chara…

Especially when a tab isn't 8 spaces worth and you don't have a near useless level of indentation from the start (e.g. Java's "class" scope), I rarely found a problem with 80 (or 78) character limits. Quite the opposite, usually there's something "wrong" with code that exceeds that limit (a "code smell", as the hip kids like to say).

Granted, quite often it's a "language smell", like Java or earlier C++'s verbose initialization forms and class terminology (AbstractFactoryImpl etc.). Other than that, it's often longer formulas or string building exercises, which usually could benefit from some temporary variables or printf-style format languages. If a statemenet has more than one operator and polysyllabic variable names, I generally prefer some variation of "let x be the overlong constant/class/descriptive name", e.g. "int x = DomainBasedStaticConfigurationSingleton.MAXIMUM_FROBNICATION_VALUE", instead of just adding up those monsters themselves. And being German, I'm actually quite used to silly compound nouns.

Post reply on HN