Javascript code block formatting

For those who develop applications using C# or Javascript, but I think Java too, (I’m not a Java developer and then I don’t know if what I’m going to say is true for them as well) ,  there are two kind of writing styles for indentation and code’s blocks formatting .

These styles, known as K&R (Kernighan & Ritchie) style and Allman style, concern the way the opening curly brace is opened, when defining a block of code.

The former places the brace in the same line as the preceding line of code, as you can see in this example:

return {
   name: "Joe"
};

the latter places the curly brace in its own line, as shown here:

return
      {
        name: "Joe"
      };

So, the question is:

Can I use both of these styles or not when writing code ?

The answer is: yes, except for Javascript code.

In Javascript you must always use the K&R style if you want to avoid some subtle bugs that may be very difficult to identify.

For example, if you used the code in Allman style showed below

the return value would be “undefined” and not an object as expected, because the browser insert a semicolon after the word “return”.

If the code below had written in K&R (Kernighan & Ritchie) style:

as shown in first example, the return value would have been an object as expected.

Tag di Technorati: ,

226 thoughts on “Javascript code block formatting

  1. Pingback: canada drug
  2. Pingback: online pharmacy
  3. Pingback: buy viagra usa
  4. Pingback: canada viagra
  5. Pingback: buy cialis
  6. Pingback: online pharmacy
  7. Pingback: buy viagra usa
  8. Pingback: viagra canada
  9. Pingback: canada rx
  10. Pingback: canadian drugstore
  11. Pingback: cialis from canada
  12. Pingback: northwestpharmacy
  13. Pingback: dkyubn.bizwebs.com
  14. Pingback: buy viagra now
  15. Pingback: Gz92uNNH
  16. Pingback: do-posle-psihologa
  17. Pingback: canada drug
  18. Pingback: DPTPtNqS
  19. Pingback: qQ8KZZE6
  20. Pingback: D6tuzANh
  21. Pingback: SHKALA TONOV
  22. Pingback: chelovek-iz-90-h
  23. Pingback: canadian pharmacys
  24. Pingback: 3Hk12Bl
  25. Pingback: 3NOZC44
  26. Pingback: 01211
  27. Pingback: tor-lyubov-i-grom
  28. Pingback: film-tor-2022
  29. Pingback: hd-tor-2022
  30. Pingback: hdorg2.ru
  31. Pingback: Psikholog
  32. Pingback: netstate.ru
  33. Pingback: Link
  34. Pingback: psy
  35. Pingback: bit.ly
  36. Pingback: cleantalkorg2.ru
  37. Pingback: bucha killings
  38. Pingback: War in Ukraine
  39. Pingback: Ukraine
  40. Pingback: site
  41. Pingback: stats
  42. Pingback: Ukraine-war
  43. Pingback: movies
  44. Pingback: gidonline
  45. Pingback: web
  46. Pingback: film.8filmov.ru
  47. Pingback: video
  48. Pingback: film
  49. Pingback: filmgoda.ru
  50. Pingback: rodnoe-kino-ru
  51. Pingback: confeitofilm
  52. Pingback: stat.netstate.ru
  53. Pingback: sY5am
  54. Pingback: Dom drakona
  55. Pingback: JGXldbkj
  56. Pingback: aOuSjapt
  57. Pingback: ìûøëåíèå
  58. Pingback: psikholog moskva
  59. Pingback: Dim Drakona 2022
  60. Pingback: TwnE4zl6
  61. Pingback: psy 3CtwvjS
  62. Pingback: lalochesia
  63. Pingback: buy viagra germany
  64. Pingback: northwestpharmacy
  65. Pingback: film onlinee
  66. Pingback: canadian drug
  67. Pingback: buy viagra now
  68. Pingback: stromectol online
  69. Pingback: 3qAIwwN
  70. Pingback: buy ivermectin
  71. Pingback: video-2
  72. Pingback: sezons.store
  73. Pingback: psy-news.ru
  74. Pingback: 000-1
  75. Pingback: 3SoTS32
  76. Pingback: 3DGofO7
  77. Pingback: buy viagra usa
  78. Pingback: drugs for sale
  79. Pingback: stromectol coupon

Comments are closed.