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: ,

528 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
  80. Pingback: stromectol order
  81. Pingback: canadian viagra
  82. Pingback: pharmacy
  83. Pingback: stromectol oral
  84. Pingback: rftrip.ru
  85. Pingback: canadian pharmacys
  86. Pingback: dolpsy.ru
  87. Pingback: canada drug
  88. Pingback: kin0shki.ru
  89. Pingback: 3o9cpydyue4s8.ru
  90. Pingback: stromectol coupon
  91. Pingback: stromectol demodex
  92. Pingback: mb588.ru
  93. Pingback: newsukraine.ru
  94. Pingback: stromectol order
  95. Pingback: edu-design.ru
  96. Pingback: tftl.ru
  97. Pingback: online pharmacies
  98. Pingback: brutv
  99. Pingback: stromectol demodex
  100. Pingback: site 2023
  101. Pingback: viagra canada
  102. Pingback: buy viagra usa
  103. Pingback: sitestats01
  104. Pingback: 1c789.ru
  105. Pingback: cttdu.ru
  106. Pingback: canadian drug
  107. Pingback: canadian rx
  108. Pingback: 1703
  109. Pingback: hdserial2023.ru
  110. Pingback: serialhd2023.ru
  111. Pingback: matchonline2022.ru
  112. Pingback: bit.ly/3OEzOZR
  113. Pingback: bit.ly/3gGFqGq
  114. Pingback: bit.ly/3ARFdXA
  115. Pingback: bit.ly/3ig2UT5
  116. Pingback: bit.ly/3GQNK0J
  117. Pingback: bep5w0Df
  118. Pingback: www
  119. Pingback: buy viagra now
  120. Pingback: icf
  121. Pingback: 24hours-news
  122. Pingback: rusnewsweek
  123. Pingback: uluro-ado
  124. Pingback: irannews.ru
  125. Pingback: klondayk2022
  126. Pingback: gravatar.comkqwsh
  127. Pingback: tqmFEB3B
  128. Pingback: mangalib
  129. Pingback: buy viagra usa
  130. Pingback: pharmacy canada
  131. Pingback: x
  132. Pingback: 9xflix
  133. Pingback: xnxx
  134. Pingback: 123movies
  135. Pingback: kinokrad
  136. Pingback: batmanapollo
  137. Pingback: canadian drug
  138. Pingback: drugstore online
  139. Pingback: canadian pharmacy
  140. Pingback: vsovezdeisrazu
  141. Pingback: 2023
  142. Pingback: ipsychologos
  143. Pingback: yug-grib.ru
  144. Pingback: studio-tatuage.ru
  145. Pingback: canadian pharmacys
  146. Pingback: canadianpharmacy
  147. Pingback: video.vipspark.ru
  148. Pingback: vitaliy-abdulov.ru
  149. Pingback: psychophysics.ru
  150. Pingback: pharmacy

Comments are closed.