473,388 Members | 1,383 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Probably an inane question, but why do we use the current JS coding patterns?

RMWChaos
137 100+
Not to go against the grain or anything (or disagree with Douglas Crawford), but why is this considered the de-facto JS coding pattern:

Expand|Select|Wrap|Line Numbers
  1. // The "Crawford" pattern
  2. function myFunc() {
  3.     var a = "Hello";
  4.     var b = "world";
  5.     for (var i = 0; i < 3; i++) {
  6.         alert(a + " " + b);
  7.     };
  8. };
  9.  
When (to me) this is much clearer to read:

Expand|Select|Wrap|Line Numbers
  1. // The RMWChaos pattern ;-)
  2. function myFunc()
  3.     {
  4.     var a = "Hello";
  5.     var b = "world";
  6.     for (var i = 0; i < 3; i++)
  7.         {
  8.         alert(a + " " + b);
  9.         };
  10.     };
  11.  
In both cases, the code will be the same length when minimized, and they are syntacticly identical to one another. However, the second version just seems infinitely easier to read (to see where blocks begin, identify conditionals, etc.) and, therefore, easier to modify, bug-check, etc. (IMHO).

Am I alone here? Maybe this is just my bias from working with BASIC and COBOL for so long, and after a while I will see the first version as easier to read ... perhaps.

Oh, and what's the big deal about using spaces to indent (4, 8, 16, etc.) vs. Tab? So what if Tab lengths are not standardized between programs...as long as they are indented?
Dec 23 '07 #1
29 1652
acoder
16,027 Expert Mod 8TB
See this thread in the Software Development forum. It's more generic.

btw, I prefer the first method and I don't see why the second one has to be easier to read. In fact, the last bracket looks like it closes the for loop, but that's probably because I'm so used to the first method.
Dec 23 '07 #2
RMWChaos
137 100+
See this thread in the Software Development forum. It's more generic.

btw, I prefer the first method and I don't see why the second one has to be easier to read. In fact, the last bracket looks like it closes the for loop, but that's probably because I'm so used to the first method.
I stated coding with Notepad and now with Notepad+. N+ highlights the code blocks with red lines when they are directly inline with one another, but only highlights the brackets themselves when they are indented differently.

Expand|Select|Wrap|Line Numbers
  1. // With Notepad+, brackets inline
  2.  
  3.     {           // All these lines are
  4.     |   (...)   // highlighted when
  5.     }           // brackets are inline.
  6.  
  7. // With Notepad+, brackets not inline
  8.  
  9.     {           // only this line...
  10.     (...)
  11. };              // and this line are highlighted
  12.  
But even with just plain text, I still think it's easier to read. Just preference, I guess. I will read the link you provided, and may change my mind over time.

Thanks!
Dec 23 '07 #3
RMWChaos
137 100+
I actually use the first method. I've never had bracket problems because I use indenting too and all brackets line up with the command.

I don't know how I started using this method as opposed to the second method, but it's stuck and I actually find the second method unsightly. If I'm copying code from somewhere or altering code, I'll change it to the first style (if it's not a lot of code). To save even more space, I'll also get rid of brackets altogether if it's a single line of code in if, for, while, etc.

Just personal habits.
Well, at least you're consistent. =D Funny, when I get code that looks the way you write it, with opening bracket on the same line, closing bracket inline with the function statement, I change it to look the way I prefer.

But unsightly!? C'mon, even a blind man can see my way looks waaaay prettier. =D

It seems that I am not, after all, alone on this point.
Dec 23 '07 #4
Logician
210 100+
Not to go against the grain or anything (or disagree with Douglas Crawford), but why is this considered the de-facto JS coding pattern:

Expand|Select|Wrap|Line Numbers
  1. // The "Crawford" pattern
  2. function myFunc() {
  3.     var a = "Hello";
  4.     var b = "world";
  5.     for (var i = 0; i < 3; i++) {
  6.         alert(a + " " + b);
  7.     };
  8. };
  9.  
When (to me) this is much clearer to read:

Expand|Select|Wrap|Line Numbers
  1. // The RMWChaos pattern ;-)
  2. function myFunc()
  3.     {
  4.     var a = "Hello";
  5.     var b = "world";
  6.     for (var i = 0; i < 3; i++)
  7.         {
  8.         alert(a + " " + b);
  9.         };
  10.     };
  11.  
In both cases, the code will be the same length when minimized, and they are syntacticly identical to one another. However, the second version just seems infinitely easier to read
The first layout is often known to C programmers as K&R notation (Kernighan and Ritchie) Its sole purpose/advantage is to conserve space in published listings; it does nothing to help readability or debugging and makes checks for correct brace pairing a nightmare. Unfortunately it gets copied globally by beginners who don't know any better. A more useful way to save space is not to put single statements within braces. I would like to show the way I would write it, but this forum's broken software makes a complete mess of my indentation.
Oh, and what's the big deal about using spaces to indent (4, 8, 16, etc.) vs. Tab? So what if Tab lengths are not standardized between programs...as long as they are indented?
Tab characters used to indent listings are just unnecessary and annoying. Any decent editor can configure the tab key to insert a fixed number of spaces.
Dec 24 '07 #5
RMWChaos
137 100+
I would like to show the way I would write it, but this forum's broken software makes a complete mess of my indentation.
Feel free to email me an example, if you don't mind! Like you, I don't like K&R for the same reasons you supplied.

Currently, I am partial to either of the two methods below, but would be interested to see yours as well.

Expand|Select|Wrap|Line Numbers
  1. function myFunc()
  2.     {
  3.     var a = "Hello";
  4.     var b = "world";
  5.     for (var i = 0; i < 3; i++)
  6.         {
  7.         alert(a + " " + b);
  8.         };
  9.     };
  10.  
  11. // ...or... //
  12.  
  13. function myFunc()
  14. {
  15.     var a = "Hello";
  16.     var b = "world";
  17.     for (var i = 0; i < 3; i++)
  18.     {
  19.         alert(a + " " + b);
  20.     };
  21. };
  22.  
The only difference being the lack of additional indentation on the main function and for loop code blocks in the 2nd example. I can see where the 2nd example would be easier to read by those used to seeing the close bracket inline with the initiating statement. Otherwise, I like the first way, which is the way I have done it since starting to code JS.

At this point, I figure if other people don't like my method, they don't have to use it, and if they want to borrow from my code, they can modify it the way they prefer it. I will do the same. =D

Minify is going to take care of all these extra line breaks, indents, spaces, and dev notation anyway. The rest is just personal readability I suppose.

Thanks!
Dec 24 '07 #6
Logician
210 100+
Feel free to email me an example, if you don't mind! Like you, I don't like K&R for the same reasons you supplied.
Sadly only an image is reliable here.

Dec 24 '07 #7
RMWChaos
137 100+
So you align your brackets with the initial statement, but you are not using brackets with the for loop? Interesting. Is that reliable?

Expand|Select|Wrap|Line Numbers
  1. function myFunc()
  2. {
  3.   var a = "Hello", b = "world";
  4.  
  5.   for (var i = 0; i < 3; i++)
  6.     alert(a + " " + b);
  7. };
  8.  
Is that about right? What if there is additional code after the alert? I don't see how that would not confuse the end of the for loop.
Dec 24 '07 #8
gits
5,390 Expert Mod 4TB
if you leave the brackets then only one following statement is executed in that loop ...

i would never do that for my own ... i always use brackets even in the if-statements ... but its allowed and a lot of people do it that way

kind regards
Dec 24 '07 #9
acoder
16,027 Expert Mod 8TB
Well, at least you're consistent. =D Funny, when I get code that looks the way you write it, with opening bracket on the same line, closing bracket inline with the function statement, I change it to look the way I prefer.

But unsightly!? C'mon, even a blind man can see my way looks waaaay prettier. =D
Hey, you weren't supposed to quote that! :D When did I write that?

I accept the points about brackets being easier to pair, but I don't have that problem because I'm consistent.

If I had to choose between yours and the 'other' method with one less indent, I'd choose that one any day.

But, hey, can't get too worked up about it. I can read all of these. The worst are something like this:
Expand|Select|Wrap|Line Numbers
  1. function something()
  2. {
  3. if (blah)
  4. {
  5. for (...)
  6. {
  7. ...
  8. }
  9. }
  10. }
Argh! No indentation - makes it a nightmare!
Dec 24 '07 #10
gits
5,390 Expert Mod 4TB
The first layout is often known to C programmers as K&R notation (Kernighan and Ritchie) Its sole purpose/advantage is to conserve space in published listings; it does nothing to help readability or debugging and makes checks for correct brace pairing a nightmare. Unfortunately it gets copied globally by beginners who don't know any better.
i strongly disagree with that and in case you call douglas crockford a beginner who didn't know any better then i think you should tell us what javascript-guru you are :) i think i should bow to you ... ;)

kind regards
Dec 24 '07 #11
RMWChaos
137 100+
i strongly disagree with that and in case you call douglas crockford a beginner who didn't know any better then i think you should tell us what javascript-guru you are :) i think i should bow to you ... ;)

kind regards
LOL! There are so many levels of amateur, professional, and guru between me and Douglas...I just bow to everyone to be safe. =D

If I had to choose between yours and the 'other' method with one less indent, I'd choose that one any day.
Now you're just being contrary. ;-)

Hey, you weren't supposed to quote that! :D When did I write that?
Um...

See this thread in the Software Development forum. It's more generic.
Maybe this is why you're stuck using the same ugly old style...short memory. =D
Dec 24 '07 #12
Logician
210 100+
i strongly disagree with that and in case you call douglas crockford a beginner who didn't know any better then i think you should tell us what javascript-guru you are :) i think i should bow to you ... ;)

kind regards
Anyone in the business of publishing listings, finds themselves under the same pressure to save space, which is why they'll use the 'opening brace at end of line' format regardless of their opinion of it.
Dec 24 '07 #13
acoder
16,027 Expert Mod 8TB
Now you're just being contrary. ;-)
Um...
Maybe this is why you're stuck using the same ugly old style...short memory. =D
I was only joking about you quoting me from another thread.

I'm not being contrary about choosing a particular method. I meant if I was forced to choose between your method and the method I find 'unsightly', I'd choose that one because yours seems a bit confusing being so used to using the command to line up with the closing bracket.
Dec 24 '07 #14
acoder
16,027 Expert Mod 8TB
Anyone in the business of publishing listings, finds themselves under the same pressure to save space, which is why they'll use the 'opening brace at end of line' format regardless of their opinion of it.
I'm not so sure of that. He wouldn't then include it as a coding convention, would he?
Dec 24 '07 #15
RMWChaos
137 100+
I was only joking about you quoting me from another thread.

I'm not being contrary about choosing a particular method. I meant if I was forced to choose between your method and the method I find 'unsightly', I'd choose that one because yours seems a bit confusing being so used to using the command to line up with the closing bracket.
Yeah, I know, it's just fun to pick on you. ;-)
Dec 24 '07 #16
pbmods
5,821 Expert 4TB
Here's what I don't like about omitting braces for single-line blocks:
Expand|Select|Wrap|Line Numbers
  1. var element = document.getElementById('theElement');
  2. for( var i = 0; i < 10; ++i )
  3.     doSomething();
  4. element.doSomethingElse();
  5. andSomeMoreStuff('table');
  6.  
It's borderline-legible if indented properly; it would be a nightmare otherwise:
Expand|Select|Wrap|Line Numbers
  1. var element = document.getElementById('theElement');
  2. for( var i = 0; i < 10; ++i )
  3. doSomething();
  4. element.doSomethingElse();
  5. andSomeMoreStuff('table');
  6.  
Or occasionally I see this (*shudder*):
Expand|Select|Wrap|Line Numbers
  1. var element = document.getElementById('theElement');
  2. for( var i = 0; i < 10; ++i )
  3.     doSomething();
  4.     element.doSomethingElse();
  5.     andSomeMoreStuff('table');
  6.  
And just to throw my hat in the stack, I absolutely *hate* it when programmers put the opening brace on the same line as the declaration.
Dec 25 '07 #17
I prefer the first method, dunno why. Just used to it.
Dec 26 '07 #18
RMWChaos
137 100+
There is only one situation I have come across where the opening brace absolutely had to be on the same line as the opening statement...

Expand|Select|Wrap|Line Numbers
  1. var EventCache = function()
  2.     {
  3.     var listEvents = [];
  4.     return {    // This brace must be on the same line or it errors
  5.         listEvents : listEvents,
  6.         ( ... )
  7.  
I don't know why this is the case. All I do know is that if I move the brace one line lower, she throws an error.
Dec 26 '07 #19
acoder
16,027 Expert Mod 8TB
Here's what I don't like about omitting braces for single-line blocks:...
There's no excuse for non-indentation whatever the style.
And just to throw my hat in the stack, I absolutely *hate* it when programmers put the opening brace on the same line as the declaration.
Why do those who like the brace-on-new-line style always seem to hate the brace-on-same-line style? Just wondering...
Dec 26 '07 #20
gits
5,390 Expert Mod 4TB
Why do those who like the brace-on-new-line style always seem to hate the brace-on-same-line style? Just wondering...
:) i'm wondering too ...
Dec 26 '07 #21
pbmods
5,821 Expert 4TB
Why do those who like the brace-on-new-line style always seem to hate the brace-on-same-line style? Just wondering...
For me, it's because I'm used to following the curvy thing. It's just easier for me to line up matching braces rather than have to match it against indented text.

It also helps spread the code out a bit and makes it look a lot cleaner to me.

Of course, I usually take it a step further and mess with parentheses, too. You'll often see a lot of this in my code:

Expand|Select|Wrap|Line Numbers
  1. $__stmt =
  2.     $db->prepareStatement
  3.     (
  4.         "EXEC [Some].[Stored].[Procedure] ?, ?, ?",
  5.         array
  6.         (
  7.             (int) $customerID,
  8.             (int) $offerID,
  9.             $somethingElseIDunno
  10.         )
  11.     );
  12.  
Dec 27 '07 #22
acoder
16,027 Expert Mod 8TB
For me, it's because I'm used to following the curvy thing. It's just easier for me to line up matching braces rather than have to match it against indented text.

It also helps spread the code out a bit and makes it look a lot cleaner to me.
Oh, I know the reasons, understand them and accept them to an extent, but it still doesn't explain the absolute hatred for the on-same-line method as opposed to a simple dislike or just a preference of one over the other.
Dec 27 '07 #23
RMWChaos
137 100+
Oh, I know the reasons, understand them and accept them to an extent, but it still doesn't explain the absolute hatred for the on-same-line method as opposed to a simple dislike or just a preference of one over the other.
Oh, I don't hate it (I try never to hate anything; causes too much grief in this world). In fact, I am considering moving to it, but as a relatively new coder, I like to have my braces and brackets line up so that I do not forget to close them. It makes for easy bug fixing IMHO. Helps when you forget to open one too, or am I the only person that ever does that? =D
Dec 28 '07 #24
acoder
16,027 Expert Mod 8TB
Oh, I don't hate it (I try never to hate anything; causes too much grief in this world). In fact, I am considering moving to it, but as a relatively new coder, I like to have my braces and brackets line up so that I do not forget to close them. It makes for easy bug fixing IMHO. Helps when you forget to open one too, or am I the only person that ever does that? =D
That's OK then. At least we've got one. I'm not claiming the brackets/braces-on-the-same-line method is superior. I just feel that its supposed disadvantages seem a bit exaggerated.
Dec 28 '07 #25
Logician
210 100+
There's no excuse for non-indentation whatever the style.

Why do those who like the brace-on-new-line style always seem to hate the brace-on-same-line style? Just wondering...
Simply because it requires more (unnecessary) effort to scrutinise the code for missing braces, when their presence should be made clear.
Dec 29 '07 #26
Logician
210 100+
I'm not so sure of that. He wouldn't then include it as a coding convention, would he?
The fact that he's reiterating a convention doesn't mean that he agrees with it, just that he has to be seen to be consistent.
Dec 29 '07 #27
acoder
16,027 Expert Mod 8TB
Simply because it requires more (unnecessary) effort to scrutinise the code for missing braces, when their presence should be made clear.
Each to their own, I guess. I don't find it difficult as long as the code is properly indented. However, I'm not going to argue about which one is better. I might even admit that having braces on a new line is probably better from a logical point of view. If we started all over again, we probably would've had that as the de facto standard, so to speak.
Dec 29 '07 #28
pbmods
5,821 Expert 4TB
However, I'm not going to argue about which one is better.
I will.

0>:} (<-- Horns holding up cheap tinfoil halo.)
Dec 30 '07 #29
gits
5,390 Expert Mod 4TB
remember this is not a religious forum :)) ... no, just kidding ... but to me it seems that there are two groups of developers ... the ones that includes the brackets in their (even personal) 'code-view' and the others that don't ... i have no special preference for one of them ... but we use the 'crockford'-way (bracket-on-the-same-line) at work and so i use to always use it ... like acoder said ... i find the pointed problems with that exaggerated ... the much more important point is about the indentation ... and one more point to mention :) ... long lines ... i really hate that, just to say that i hate something too ... i hate long lines > 80 chars :)

kind regards
Dec 30 '07 #30

Sign in to post your reply or Sign up for a free account.

Similar topics

32
by: Christopher Benson-Manica | last post by:
Is the following code legal, moral, and advisable? #include <iostream> class A { private: int a; public: A() : a(42) {}
5
by: Don Vaillancourt | last post by:
Hello all, Over the years as I design more database schemas the more I come up with patterns in database design. The more patterns I recognize the more I want to try to design some kind of...
1
by: R Reyes | last post by:
Hello All, I'm always looking for ways to improve my code. Most of the time (whenever I'm working on a project) I write a bunch of functions. Then after the project is finished, I put all the...
2
by: Silent Ocean | last post by:
Hi All I have following questions regarding C# Assembly and Threading. Let me know the precise answer or lead me to the proper materials. 1. Is memory leakeage possible in .Net Manager...
2
by: Stan | last post by:
I want to make two pages interact through a controller. 1. Page A has a grid and Add button. 2. When Add button is clicked Page B pops up. 3. User enters information and clicks Save 4....
13
by: Chris | last post by:
Can someone tell me the OOP way to code classes for small tables of data? For example, i'm loading several tables that will be stored in the current.cache of an asp.net app. One table is credit...
7
by: CSharpguy | last post by:
I'm coding my first business web app in .NET 2.0 and its only a read only web app. I'm just pulling data from the database and allowing users to filter the data in the grids by using dropdowns....
12
by: Tyno Gendo | last post by:
Hi everyone I wondered what if any methods people here use to speed up their PHP code (ie. speed at which you produce your code). Things like 'code templates', 'base objects' for this and...
3
by: Peter D. | last post by:
I have been programming PHP for a while now and always seem to run into the same problem when working on more than trivial apps. Most of my coding is for personal projects anyway so it really isn't...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.