librelist archives

« back to archive

Friday afternoon styles

Friday afternoon styles

From:
Swain, Simon
Date:
2013-06-28 @ 06:03
Me, I code like this:

if (foo) {
   bar();
}

But some folks do it like this

if (foo) bar();

Although the second way is a one-liner, I see it as kind of dirty,
potentially error prone if you need to add another statement to the
second way, and you're introducing two different styles in one
codebase -- but I see it a heap in JS.

I've never liked C done that way either.

Can anyone convince me why it should be done one way or the other?

Re: [sydjs] Friday afternoon styles

From:
Chris Berkhout
Date:
2013-06-28 @ 06:22
My reason for the block being better...

Cos Douglas Crockford says so:

http://oreilly.com/javascript/excerpts/javascript-good-parts/jslint.html#required_blocks

And I personally don't feel any excitement about the shortness of the
blockless form.

I do sometimes like a one-liner, but I'm happy to do it like this:

    if (foo) { bar(); }

I agree with the others that the best answer is to go with your current
coding standards or whatever new ones you can agree on.

Cheers,
Chris


On Fri, Jun 28, 2013 at 4:03 PM, Swain, Simon <simon@modus.com.au> wrote:

> Me, I code like this:
>
> if (foo) {
>    bar();
> }
>
> But some folks do it like this
>
> if (foo) bar();
>
> Although the second way is a one-liner, I see it as kind of dirty,
> potentially error prone if you need to add another statement to the
> second way, and you're introducing two different styles in one
> codebase -- but I see it a heap in JS.
>
> I've never liked C done that way either.
>
> Can anyone convince me why it should be done one way or the other?
>

Re: [sydjs] Friday afternoon styles

From:
Swain, Simon
Date:
2013-06-28 @ 06:32
Yeah I'm not a fan of if (foo) { bar(); } either

I'm with Crockford on this too.

Vertical space is cheap, best to use it for clarity.

I agree that it's really either define a coding standard and stick to
it, and/or stick with the style that already exists in the code.

Emacs paredit does a stellar job of keeping things tight.
--

Simon Swain
Technical Director
Modus

+61 418 30 20 20
www.modus.com.au


On Fri, Jun 28, 2013 at 4:22 PM, Chris Berkhout <chrisberkhout@gmail.com> wrote:
> My reason for the block being better...
>
> Cos Douglas Crockford says so:
> 
http://oreilly.com/javascript/excerpts/javascript-good-parts/jslint.html#required_blocks
>
> And I personally don't feel any excitement about the shortness of the
> blockless form.
>
> I do sometimes like a one-liner, but I'm happy to do it like this:
>
>     if (foo) { bar(); }
>
> I agree with the others that the best answer is to go with your current
> coding standards or whatever new ones you can agree on.
>
> Cheers,
> Chris
>
>
> On Fri, Jun 28, 2013 at 4:03 PM, Swain, Simon <simon@modus.com.au> wrote:
>>
>> Me, I code like this:
>>
>> if (foo) {
>>    bar();
>> }
>>
>> But some folks do it like this
>>
>> if (foo) bar();
>>
>> Although the second way is a one-liner, I see it as kind of dirty,
>> potentially error prone if you need to add another statement to the
>> second way, and you're introducing two different styles in one
>> codebase -- but I see it a heap in JS.
>>
>> I've never liked C done that way either.
>>
>> Can anyone convince me why it should be done one way or the other?
>
>

Re: [sydjs] Friday afternoon styles

From:
Jason Hinch
Date:
2013-06-28 @ 06:44
Jason Hinch

Sent from my iPhone

On 28/06/2013, at 4:32 PM, "Swain, Simon" <simon@modus.com.au> wrote:

> Yeah I'm not a fan of if (foo) { bar(); } either
>
> I'm with Crockford on this too.
>
> Vertical space is cheap, best to use it for clarity.
>
> I agree that it's really either define a coding standard and stick to
> it, and/or stick with the style that already exists in the code.
>
> Emacs paredit does a stellar job of keeping things tight.
> --
>
> Simon Swain
> Technical Director
> Modus
>
> +61 418 30 20 20
> www.modus.com.au
>
>
> On Fri, Jun 28, 2013 at 4:22 PM, Chris Berkhout <chrisberkhout@gmail.com> wrote:
>> My reason for the block being better...
>>
>> Cos Douglas Crockford says so:
>> 
http://oreilly.com/javascript/excerpts/javascript-good-parts/jslint.html#required_blocks
>>
>> And I personally don't feel any excitement about the shortness of the
>> blockless form.
>>
>> I do sometimes like a one-liner, but I'm happy to do it like this:
>>
>>    if (foo) { bar(); }
>>
>> I agree with the others that the best answer is to go with your current
>> coding standards or whatever new ones you can agree on.
>>
>> Cheers,
>> Chris
>>
>>
>> On Fri, Jun 28, 2013 at 4:03 PM, Swain, Simon <simon@modus.com.au> wrote:
>>>
>>> Me, I code like this:
>>>
>>> if (foo) {
>>>   bar();
>>> }
>>>
>>> But some folks do it like this
>>>
>>> if (foo) bar();
>>>
>>> Although the second way is a one-liner, I see it as kind of dirty,
>>> potentially error prone if you need to add another statement to the
>>> second way, and you're introducing two different styles in one
>>> codebase -- but I see it a heap in JS.
>>>
>>> I've never liked C done that way either.
>>>
>>> Can anyone convince me why it should be done one way or the other?
>>
>>

Re: [sydjs] Friday afternoon styles

From:
Jared Wyles
Date:
2013-06-28 @ 06:48
Is this some sort of Denny crane tactic? 

Sent from my phone

On 28/06/2013, at 4:44 PM, Jason Hinch <jhinch8290@gmail.com> wrote:

> Jason Hinch
> 
> Sent from my iPhone
> 
> On 28/06/2013, at 4:32 PM, "Swain, Simon" <simon@modus.com.au> wrote:
> 
>> Yeah I'm not a fan of if (foo) { bar(); } either
>> 
>> I'm with Crockford on this too.
>> 
>> Vertical space is cheap, best to use it for clarity.
>> 
>> I agree that it's really either define a coding standard and stick to
>> it, and/or stick with the style that already exists in the code.
>> 
>> Emacs paredit does a stellar job of keeping things tight.
>> --
>> 
>> Simon Swain
>> Technical Director
>> Modus
>> 
>> +61 418 30 20 20
>> www.modus.com.au
>> 
>> 
>> On Fri, Jun 28, 2013 at 4:22 PM, Chris Berkhout 
<chrisberkhout@gmail.com> wrote:
>>> My reason for the block being better...
>>> 
>>> Cos Douglas Crockford says so:
>>> 
http://oreilly.com/javascript/excerpts/javascript-good-parts/jslint.html#required_blocks
>>> 
>>> And I personally don't feel any excitement about the shortness of the
>>> blockless form.
>>> 
>>> I do sometimes like a one-liner, but I'm happy to do it like this:
>>> 
>>>   if (foo) { bar(); }
>>> 
>>> I agree with the others that the best answer is to go with your current
>>> coding standards or whatever new ones you can agree on.
>>> 
>>> Cheers,
>>> Chris
>>> 
>>> 
>>> On Fri, Jun 28, 2013 at 4:03 PM, Swain, Simon <simon@modus.com.au> wrote:
>>>> 
>>>> Me, I code like this:
>>>> 
>>>> if (foo) {
>>>>  bar();
>>>> }
>>>> 
>>>> But some folks do it like this
>>>> 
>>>> if (foo) bar();
>>>> 
>>>> Although the second way is a one-liner, I see it as kind of dirty,
>>>> potentially error prone if you need to add another statement to the
>>>> second way, and you're introducing two different styles in one
>>>> codebase -- but I see it a heap in JS.
>>>> 
>>>> I've never liked C done that way either.
>>>> 
>>>> Can anyone convince me why it should be done one way or the other?
>>> 
>>> 

Re: [sydjs] Friday afternoon styles

From:
Sugendran Ganess
Date:
2013-06-28 @ 07:04
It's worked because now I just want to walk around saying 'Jason Hinch'

Sent from the internet.

On 28 June 2013 07:48, Jared Wyles <jared.wyles@gmail.com> wrote:
> Is this some sort of Denny crane tactic?
>
> Sent from my phone
>
> On 28/06/2013, at 4:44 PM, Jason Hinch <jhinch8290@gmail.com> wrote:
>
>> Jason Hinch
>>
>> Sent from my iPhone
>>
>> On 28/06/2013, at 4:32 PM, "Swain, Simon" <simon@modus.com.au> wrote:
>>
>>> Yeah I'm not a fan of if (foo) { bar(); } either
>>>
>>> I'm with Crockford on this too.
>>>
>>> Vertical space is cheap, best to use it for clarity.
>>>
>>> I agree that it's really either define a coding standard and stick to
>>> it, and/or stick with the style that already exists in the code.
>>>
>>> Emacs paredit does a stellar job of keeping things tight.
>>> --
>>>
>>> Simon Swain
>>> Technical Director
>>> Modus
>>>
>>> +61 418 30 20 20
>>> www.modus.com.au
>>>
>>>
>>> On Fri, Jun 28, 2013 at 4:22 PM, Chris Berkhout 
<chrisberkhout@gmail.com> wrote:
>>>> My reason for the block being better...
>>>>
>>>> Cos Douglas Crockford says so:
>>>> 
http://oreilly.com/javascript/excerpts/javascript-good-parts/jslint.html#required_blocks
>>>>
>>>> And I personally don't feel any excitement about the shortness of the
>>>> blockless form.
>>>>
>>>> I do sometimes like a one-liner, but I'm happy to do it like this:
>>>>
>>>>   if (foo) { bar(); }
>>>>
>>>> I agree with the others that the best answer is to go with your current
>>>> coding standards or whatever new ones you can agree on.
>>>>
>>>> Cheers,
>>>> Chris
>>>>
>>>>
>>>> On Fri, Jun 28, 2013 at 4:03 PM, Swain, Simon <simon@modus.com.au> wrote:
>>>>>
>>>>> Me, I code like this:
>>>>>
>>>>> if (foo) {
>>>>>  bar();
>>>>> }
>>>>>
>>>>> But some folks do it like this
>>>>>
>>>>> if (foo) bar();
>>>>>
>>>>> Although the second way is a one-liner, I see it as kind of dirty,
>>>>> potentially error prone if you need to add another statement to the
>>>>> second way, and you're introducing two different styles in one
>>>>> codebase -- but I see it a heap in JS.
>>>>>
>>>>> I've never liked C done that way either.
>>>>>
>>>>> Can anyone convince me why it should be done one way or the other?
>>>>
>>>>

Re: [sydjs] Friday afternoon styles

From:
Blake Haswell
Date:
2013-06-28 @ 06:43
> Although the second way is a one-liner, I see it as kind of dirty,
potentially error prone if you need to add another statement to the second
way, and you're introducing two different styles in one codebase

I’m with you Simon!

My standard is that all compound statements should be wrapped in curly
braces (even if they only contain one statement), and they should span
multiple lines (again, even if they only contain one statement).

Given that, the following are all bad form in my book:

    if (foo) bar();

    if (foo) { bar(); }

    if (foo)
        bar();

    foo && bar();

IMO, the **only only only** way this statement should be written is:

    if (foo) {
        bar();
    }

My justifications for this:

1. Readability. Conditional logic should always look the same. Why make
people struggle having to decipher your code for the sake of 2 lines? White
space is free, developer confusion is not. This is especially true for `foo
&& bar();` – that’s not even an `if` statement! Why make me go through your
code character-by-character to understand that there is conditional logic
going on there?

2. Maintenance. If a developer has to add another statement then they
shouldn’t have to unpick existing code to turn it into a “real” `if`
statement. Secondly, the possibility of introducing a bug because someone
was _clever_ enough not to use braces around there one-line if statement
should not exist. So please, don’t be clever – just write your `if`
statements properly.

--
Blake Haswell
http://simpleweb.com.au/

Re: [sydjs] Friday afternoon styles

From:
Jared Wyles
Date:
2013-06-28 @ 06:08
How about foo && bar() 

:D

I fall into the second camp. Usually prefer to add the braces and new line
for the next person that needs to add a second expression.

I also don't like making white space significant in a language that 
normally ignores it. 

On 28/06/2013, at 4:03 PM, "Swain, Simon" <simon@modus.com.au> wrote:

> Me, I code like this:
> 
> if (foo) {
>   bar();
> }
> 
> But some folks do it like this
> 
> if (foo) bar();
> 
> Although the second way is a one-liner, I see it as kind of dirty,
> potentially error prone if you need to add another statement to the
> second way, and you're introducing two different styles in one
> codebase -- but I see it a heap in JS.
> 
> I've never liked C done that way either.
> 
> Can anyone convince me why it should be done one way or the other?

Re: [sydjs] Friday afternoon styles

From:
James Hiscock
Date:
2013-06-28 @ 06:10
Personally I'm a fan of

if foo then bar()

in coffeescript, which you'd think would translate to me preferring

if (foo) bar();

in JavaScript but I don't, I prefer the {} if I'm writing JS. If I were to
guess why I'd say it seems less ambiguous when you're scanning the code as
a newcomer but I don't know that that is why (or true). 


On Friday, 28 June 2013 at 4:08 PM, Jared Wyles wrote:

> How about foo && bar() 
> 
> :D
> 
> I fall into the second camp. Usually prefer to add the braces and new 
line for the next person that needs to add a second expression.
> 
> I also don't like making white space significant in a language that 
normally ignores it. 
> 
> On 28/06/2013, at 4:03 PM, "Swain, Simon" <simon@modus.com.au 
(mailto:simon@modus.com.au)> wrote:
> 
> > Me, I code like this:
> > 
> > if (foo) {
> > bar();
> > }
> > 
> > But some folks do it like this
> > 
> > if (foo) bar();
> > 
> > Although the second way is a one-liner, I see it as kind of dirty,
> > potentially error prone if you need to add another statement to the
> > second way, and you're introducing two different styles in one
> > codebase -- but I see it a heap in JS.
> > 
> > I've never liked C done that way either.
> > 
> > Can anyone convince me why it should be done one way or the other? 

Re: [sydjs] Friday afternoon styles

From:
Jared Wyles
Date:
2013-06-28 @ 06:15
Going to be whatever is most prevalent in your code base. Things that are 
not normal will stand out and force you to think :)

On 28/06/2013, at 4:10 PM, James Hiscock <james@small.mu> wrote:

> Personally I'm a fan of
> 
> if foo then bar()
> 
> in coffeescript, which you'd think would translate to me preferring
> 
> if (foo) bar();
> 
> in JavaScript but I don't, I prefer the {} if I'm writing JS. If I were 
to guess why I'd say it seems less ambiguous when you're scanning the code
as a newcomer but I don't know that that is why (or true).
> On Friday, 28 June 2013 at 4:08 PM, Jared Wyles wrote:
> 
>> How about foo && bar()
>> 
>> :D
>> 
>> I fall into the second camp. Usually prefer to add the braces and new 
line for the next person that needs to add a second expression.
>> 
>> I also don't like making white space significant in a language that 
normally ignores it.
>> 
>> On 28/06/2013, at 4:03 PM, "Swain, Simon" <simon@modus.com.au> wrote:
>> 
>>> Me, I code like this:
>>> 
>>> if (foo) {
>>> bar();
>>> }
>>> 
>>> But some folks do it like this
>>> 
>>> if (foo) bar();
>>> 
>>> Although the second way is a one-liner, I see it as kind of dirty,
>>> potentially error prone if you need to add another statement to the
>>> second way, and you're introducing two different styles in one
>>> codebase -- but I see it a heap in JS.
>>> 
>>> I've never liked C done that way either.
>>> 
>>> Can anyone convince me why it should be done one way or the other?
> 

Re: [sydjs] Friday afternoon styles

From:
Ben Taylor
Date:
2013-06-28 @ 06:27
Because I'm one of those silly Ruby hipsters I very much like the:

bar if foo

style or with more context:

def some_function argument
  return nil unless argument.some_precondition
  # do stuff
end

way of writing things. So when I see a situation where a one-liner if 
statement can be written I'll often write:

function someFunction(argument){
  if (!argument.somePrecondition()) return null;
  // do stuff
}

I believe it's nice and clear what is going on when written as a single 
line and so the braces are not needed. However I'm very much against:

function someFunction(argument){
  if (!argument.somePrecondition())
    return null;
  // do stuff
}

As it's very easy to miss the fact that there are no braces and 
accidentally add an extra line (which won't be part of the if control). 


On Friday, 28 June 2013 at 4:15 PM, Jared Wyles wrote:

> Going to be whatever is most prevalent in your code base. Things that 
are not normal will stand out and force you to think :)
> 
> On 28/06/2013, at 4:10 PM, James Hiscock <james@small.mu 
(mailto:james@small.mu)> wrote:
> 
> > Personally I'm a fan of
> > 
> > if foo then bar()
> > 
> > in coffeescript, which you'd think would translate to me preferring
> > 
> > if (foo) bar();
> > 
> > in JavaScript but I don't, I prefer the {} if I'm writing JS. If I 
were to guess why I'd say it seems less ambiguous when you're scanning the
code as a newcomer but I don't know that that is why (or true). 
> > 
> > On Friday, 28 June 2013 at 4:08 PM, Jared Wyles wrote:
> > 
> > > How about foo && bar() 
> > > 
> > > :D
> > > 
> > > I fall into the second camp. Usually prefer to add the braces and 
new line for the next person that needs to add a second expression.
> > > 
> > > I also don't like making white space significant in a language that 
normally ignores it. 
> > > 
> > > On 28/06/2013, at 4:03 PM, "Swain, Simon" <simon@modus.com.au 
(mailto:simon@modus.com.au)> wrote:
> > > 
> > > > Me, I code like this:
> > > > 
> > > > if (foo) {
> > > > bar();
> > > > }
> > > > 
> > > > But some folks do it like this
> > > > 
> > > > if (foo) bar();
> > > > 
> > > > Although the second way is a one-liner, I see it as kind of dirty,
> > > > potentially error prone if you need to add another statement to the
> > > > second way, and you're introducing two different styles in one 
> > > > codebase -- but I see it a heap in JS.
> > > > 
> > > > I've never liked C done that way either.
> > > > 
> > > > Can anyone convince me why it should be done one way or the other? 
> > 

Re: [sydjs] Friday afternoon styles

From:
Geoffrey Dunn
Date:
2013-06-28 @ 06:08
There is no point trying to figure out what the perfect way is.
Because there's no way (that I've found) to prove it to someone who
disagrees. Just decide what coding standard rules are important to
your team/company. Just don't get in the habit of doing this in
javascript:

if (foo)
{
  bar();
}

because you can seriously mess yourself up

return
{
  'key' : 'value'
};

Geoff

On 28 June 2013 16:03, Swain, Simon <simon@modus.com.au> wrote:
> Me, I code like this:
>
> if (foo) {
>    bar();
> }
>
> But some folks do it like this
>
> if (foo) bar();
>
> Although the second way is a one-liner, I see it as kind of dirty,
> potentially error prone if you need to add another statement to the
> second way, and you're introducing two different styles in one
> codebase -- but I see it a heap in JS.
>
> I've never liked C done that way either.
>
> Can anyone convince me why it should be done one way or the other?

Re: [sydjs] Friday afternoon styles

From:
Andrew Dodson
Date:
2013-06-28 @ 06:05
foo && bar()

Andrew Dodson | Freelance Web Developer | +61 (0) 452 411 861 |
http://adodson.com


On 28 June 2013 16:03, Swain, Simon <simon@modus.com.au> wrote:

> Me, I code like this:
>
> if (foo) {
>    bar();
> }
>
> But some folks do it like this
>
> if (foo) bar();
>
> Although the second way is a one-liner, I see it as kind of dirty,
> potentially error prone if you need to add another statement to the
> second way, and you're introducing two different styles in one
> codebase -- but I see it a heap in JS.
>
> I've never liked C done that way either.
>
> Can anyone convince me why it should be done one way or the other?
>