-2

I'm beginning to learn JavaScript thoroughly. I couldn't help but notice that a lot of programmers have a certain "scripty" style in JS where they seem to omit spaces between curly braces and assignments such as

function SomeBaseClass(){ 
    this.publicProperty='SomeValue'; 
}

vs

function SomeBaseClass() { 
    this.publicProperty = 'SomeValue'; 
}

I've never seen this style prevalent in other languages. I can't seem to find out whether there a good reason to use this style or if it is just an aesthetic preference. If it's an aesthetic preference, what's the history behind it?

  • I don't think I've ever seen the first 'style'.... except as output from tools. Don't confuse compiler/minifier output with hand-written code. – Jared Smith Apr 20 '17 at 01:09
  • Similar questions have been asked and [answered before](http://stackoverflow.com/questions/211795/are-there-any-coding-standards-for-javascript). – ingyhere Apr 20 '17 at 01:21
  • @JaredSmith I originally saw this style in a book about Elixir/Phoenix web framework where the author often writes javascript functions `function likeThis(){ return foo; }` emphasis on the lack of spacing between the closing parens and the opening curly brace. I've also seen questions and answers on stack overflow using that style [such as this one](https://stackoverflow.com/questions/13040684/javascript-inheritance-object-create-vs-new). – autoreleasepool Apr 20 '17 at 01:27
  • 1
    Ah, function expressions. That's a different issue than the code you posted. Even the code in your comment is not common. What **IS** common is anonymous function being written as `function(){ ...`. That's partly due to the fact that anonymous functions are clumsy to write (9 characters just to start it). Once js got the arrow function syntax you'll notice people start using whitespace again: `x => ...`. – slebetman Apr 20 '17 at 01:43
  • @slebetman That's it! I apologize for posting the wrong kind of code. Still new to JS. Thank you for answering my nitpick-y question. If you want to post it as an answer I will mark it as accepted. – autoreleasepool Apr 20 '17 at 02:13

1 Answers1

2

Not really a style, I would say is more

  • a lack of IDE that auto-formats code
  • every space is a byte. some people might be really concerned about all those 4 or 5 bytes... For that I'd recommend a minifying tool (actually shortens all the names and save heaps of space).
  • laziness. I personally care about the structure of my code, so I don't do it like that
DesertFox
  • 768
  • 1
  • 4
  • 6