#147 enhancement
Nick Stakenburg

Consistent coding style. Source and unittest cleanup.

Reported by Nick Stakenburg | June 5th, 2008 @ 05:35 PM | in 1.6.1

There is inconsistency in coding style throughout the source and unittests. I decided to take care of this with a big cleanup, available in my fork:

http://github.com/staaky/prototy...

I've attempted to pick the most elegant solutions while keeping good readability. Everything is based on what was there, only making things more consistent.

Also removed whitespace, tabs and added missing semicollons.

Here are some guidelines I used while cleaning up:

// No double var declarations, comma seperated those.
// Used one space to seperate from double space notation.
var one = foo.first(),
 two = foo.last(),
 three = element.tagName ? element.tagName.toUpperCase() :
  'blah';


// For readability: if else, do while, try catch
// all start with a newline
  } else {
// became
  }
  else {


// Only used { } on multiple lines
// Newline is only used when required on long lines.
if (foo.bar) foo.bar();
else {
  var foo = blah;
  if (foo.bar == creates.a.veryLongLine)
    foo.bar();
}


// Ended function declarations with a semicolon.
// Always used semicolon at end of function.
// (arguments) is follow by a space.
function foo() { bar++; };

Comments and changes to this ticket

  • Nick Stakenburg

    Nick Stakenburg June 5th, 2008 @ 05:54 PM

    One correction/addition to that guideline.

    // newlines don't start with operators.
    var one = foo.first(),
     two = foo.last(),
     three = element.tagName ? element.tagName.toUpperCase() :
      'blahblahblah';
    
  • John-David Dalton

    John-David Dalton June 5th, 2008 @ 06:05 PM

      • → State changed from “new” to “enhancement”
      • → Assigned user changed from “” to “Andrew Dupont”

    Thanks for all the cleanup and things.

    You may want to wait for the core to become stable before doing all of this though.

    A lot of the core code is in flux. So many of your changes wont have any bearing.

  • Nick Stakenburg

    Nick Stakenburg June 5th, 2008 @ 07:07 PM

    No problem. When 1.6.1 is there I'll have another look.

  • John-David Dalton

    John-David Dalton June 5th, 2008 @ 09:56 PM

      • → Milestone changed from “” to “1.6.0.3”
  • Juriy Zaytsev

    Juriy Zaytsev June 6th, 2008 @ 04:55 AM

      • → Milestone changed from “1.6.0.3” to “1.6.1”

    I would vote for:

    var foo = element.tagName 
      ? element.tagName.toUpperCase()
      : 'blahblahblah';
    

    Imo, by bringing operators upfront, it's much easier to navigate through the code. This syntax makes ternaries stand out from other expressions.

    Also, I find 1 space in front of comma-separated variable declarations less readable than when using 2 spaces.

    var foo = 'bar',
        bar = 5,
        baz = true;
    

    The left alignment in such case is very helpful.

  • Nick Stakenburg

    Nick Stakenburg June 6th, 2008 @ 01:58 PM

    The 4 spaces on extra vars could be nice, it would make things a bit easier to read.

    For readability I'd prefer the operators at the end, so you know what to expect on the next line before you read it.

    Operators are not often used up front in the code. When adding strings together for example, the + is always added at the end, it's something most people do naturaly. I'd leave it like that.

  • John-David Dalton

    John-David Dalton June 6th, 2008 @ 02:29 PM

    Ya now that there is discussion over the formating a 1.6.1 milestone is more appropriate.

    I was thinking that the patches just made things more consistent in the coding using established coding styles in the code. If this is about changing them or adding new styles I think everything would need to be run by Sam as I believe most of the style is dictated by his preference.

    For example I believe Sam doesn't like this

     element.observe('click', function foo() { bar++; });
     //but instead likes this
     element.observe('click', function foo() { bar++ }); //without the inner semi-colon
    

    When dealing with a list of variables I have seen Andrew prefer the "1 space" style:

     var foo = 'happyCowsComeFromCalifornia',
      bar = true,
      baz = 10;
    

    I think its good but, I think somtimes the "4 space" style, as Juriy points out, is helpful. I think it depends on the context of the surrounding code and I don't think one style is appropriate in all situations. For example, I wouldn't add those extra spaces if its a list of two or something.

  • Juriy Zaytsev

    Juriy Zaytsev June 6th, 2008 @ 03:31 PM

    The number of declarations doesn't seem to matter.

    I still find left alignment more readable (both, first character and "=" sign alignment definitely bring a balance).

    var foo = 'bar',
        baz = 5;
    
  • Nick Stakenburg

    Nick Stakenburg June 6th, 2008 @ 04:02 PM

    Good idea JDD, I never liked those inner semi-colons.

    I think '4 spaces' is more readable. '1 space' is good and might combine a bit better with the '2 space' style. Unless you want to go for 6 spaces in a situation like this:

    var foo = 'happyCowsComeFromCalifornia',
        bar = (moo == 1234567890) ? 'blablah' :
          (baz == moo) ? 'blahblabarbapapa';
    
    var foo = 'happyCowsComeFromCalifornia',
     bar = (moo == 1234567890) ? 'blablah' :
      (baz == moo) ? 'blahblab' : 'barbapapa';
    

    I'm undecided. What would Sam do?

  • Juriy Zaytsev

    Juriy Zaytsev June 6th, 2008 @ 04:20 PM

    The reason for bringing ternaries upfront is that it's easy to confuse ":" with ";" at the end of the line:

    var foo = 'happyCowsComeFromCalifornia',
        bar = (moo == 123) ?
          'blahblah' :
          'harharhar';
    

    vs.

    var foo = 'happyCowsComeFromCalifornia',
        bar = (moo == 123)
          ? 'blahblah'
          : 'harharhar';
    
  • Nick Stakenburg

    Nick Stakenburg June 6th, 2008 @ 04:41 PM

    For me operators feel more natural when they stay at the end. In front they might improve readability when skimming through code. But it doesn't make things easier when reading from top to bottom, most of the time.

    I'd expect something at the end of each line that tells me how to interpret the next, that adds to readability. I'd rather have ':' and ';' there for that reason, how easily do we mix those up?

Please Login or create a free account to add a new comment.

You can update this ticket by sending an email to from your email client. (help)

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

The Prototype JavaScript library.

Shared Ticket Bins