1

Which is the common best practice for where to place parenthesis after a function? I see at times function () and I see function(). With parameters I see function (param) and then I see function(param. Is this just a matter of preference or is there a reason as to why there would be whitespace after the function or there would not be whitespace?

Cœur
  • 37,241
  • 25
  • 195
  • 267
pertrai1
  • 4,146
  • 11
  • 46
  • 71
  • 3
    It's purely preference, assuming you work within a team you should try and discover what the guidelines are, to make it easier to work on other peoples' code. Otherwise, work out what you find more comfortable to work with. – David Thomas Nov 26 '14 at 05:39
  • possible duplicate of [Space after function name is wrong?](http://stackoverflow.com/questions/9765942/space-after-function-name-is-wrong) – Colin Brock Nov 26 '14 at 05:40
  • Google also has their own standards: http://google-styleguide.googlecode.com/svn/trunk/javascriptguide.xml – jasonscript Nov 26 '14 at 05:53
  • As long as this is a preference opposed to a standard, please remove if you find that to be according to your standards of how to handle duplicates. This can be considered opinion based, just wanted to make sure this was not a flaw in my code style – pertrai1 Nov 26 '14 at 06:32

2 Answers2

2

JavaScript is not white space sensitive.you define your coding style.

Though having white space between the function and parenthesis is no sin. If you follow crockford's javascript standards. He advises not to have space in between.

http://javascript.crockford.com/code.html#function

  • So neither way there should be whitespace? That seems to be the case so this is what I was looking for. I never understood why some code has space after the function and others do not. If it is preference, I will stay with Crockford's. Thank you – pertrai1 Nov 26 '14 at 05:52
  • I will add comment after reading more from there is that Crockford says there should be one space before left parenthesis for anonymous function. I will read more about this now that I have the reference. – pertrai1 Nov 26 '14 at 05:54
1

The size of the indent is usually independent of the style. Many early programs used tab characters for indentation, for simplicity and to save on source file size. Unix editors generally view tabs as equivalent to eight characters, while Macintosh and Microsoft Windows environments would set them to four, creating confusion when code was transferred back and forth. Modern programming editors are now often able to set arbitrary indentation sizes, and will insert the appropriate combination of tabs and spaces. For Ruby, many shell programming languages, and some forms of HTML formatting, two spaces per indent level is generally used.

Read full on Code Indent Style in Programming

Manish Jangir
  • 5,329
  • 4
  • 42
  • 75
  • Awesome! Thank you for the full read. I am digging into it and getting a better grasp on this. Much appreciated – pertrai1 Nov 26 '14 at 06:30