![]() Otherwise, we may get unexpected behavior from our code. So, for JavaScript, we should put our opening curly braces on the same line. Of course, he puts this into a section called "The Awful Parts." This is also mentioned in JavaScript: The Good Parts by Douglas Crockford ( Amazon Link + Jeremy's Review). This can cause problems.įor more information on this, just do some searches for "JavaScript semicolon insertion", and you'll find tons of results. This means that if the JavaScript parser thinks that you forgot to put a semicolon at the end of a line, it will put one in for you. JavaScript has a quirk in the language called semicolon insertion. There is one exception to the "it doesn't matter" rule: JavaScript. But really, there are more important things that we should be discussing. People on both sides are extremely passionate about how they do it "the one true way". That's why I don't spend time in my session to talk about this particular topic. What's important is that we pick one and stay consistent.Īs with everything else in the development world, we need to find the balance. Ultimately, which style we choose doesn't really matter. I think that we can all agree that consistency should be high up on the list So, we'll take K&R off the list of possible choices. But for other blocks (such as the "if" statement), the curly brace is on the same line. Here's a sample (based on the same code block from above):įor methods, the opening curly brace is on a separate line. So, I went back and took a look through my K&R and was a bit surprised at what I saw. As mentioned above, books often use the "same line" version to save space. When I'm reading books, I generally don't pay attention to curly brace layout. I'll have to admit that I didn't know what the K&R method was. I recently ran across a curly brace discussion, and they decided that no matter what, K&R is wrong. This is the K&R method (from the book The C Programming Language by Brian W. Now, there is a third camp that I should mention just because you may run across it. This makes it easy to visually spot the beginning of the code block - we just need to find the end brace and then scan up until we see an opening brace in the same column. The biggest advantage to having the opening curly brace on a separate line is that the curly braces will always line up visually (assuming that we are also using good horizontal spacing in our code). This is also welcome in books and blogs that include code samples since more code can fit in less vertical space.Ĭamp Two: Opening Curly Braces on a Separate Line This means that we have more space on the screen to see additional code. ![]() Vertical density is an important topic when talking about easy-to-read code, so this is definitely something that we should consider.īy having the curly brace on the same line, we reduce the (already short) code sample above by 3 lines. The biggest advantage to having the opening curly brace on the same line is to save vertical space. So, let's take a look at the advantages, and I'll let you know my personal preference.Ĭamp One: Opening Curly Braces on the Same Line Each of these styles has its pros and cons.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |