Dangling if
Nick Sabalausky
SeeWebsiteToContactMe at semitwist.com
Mon Oct 1 05:56:25 PDT 2012
On Sun, 30 Sep 2012 11:42:40 +0200
"monarch_dodra" <monarchdodra at gmail.com> wrote:
> On Friday, 28 September 2012 at 17:40:17 UTC, Andrej Mitrovic
> wrote:
> > On 9/28/12, Bernard Helyer <b.helyer at gmail.com> wrote:
> >> By the time the compiler even has a concept of an 'if
> >> statement'
> >> or a 'block' the whitespace is long gone. Not to say you
> >> couldn't change the lexing model to detect such things,
> >> but it's not a simple as you make it sound.
> >
> > I see, so it's an implementation limitation. I guess we'll have
> > to
> > resort to that dlint tool which will have to be built.
>
> Personally, EVEN when I'm doing a 1 line if, I *still* wrap it in
> a block. EG:
>
> if(a == 0)
> a = 1;
> or
> if(a == 0) a = 1;
>
> Becomes:
> if(a == 0)
> {a = 1;}
> or
> if(a == 0) {a = 1;}
>
> It might look iffy at first, but very quickly feels natural. It
> may look like it requires (god forbid) "useless" typing, but when
> that 1 liner becomes a 2 liner, it saves your life.
>
> It has saved mine more than once actually!
>
> I've done the dangling if bug often. One day I said "no-more!".
> I've addopted the above format, and it has not happened to me
> since.
>
> Further more, thanks to D's ban on "if();", you can litterally
> never fail with this format. I warmly recommend it to every one.
I don't know if maybe this is somehow related to my inability to accept
or feel comfortable with indent-based languages, but I don't think
there's ever been a time I've forgotten to add curly braces when adding
another statement to a one-statement 'if' or 'else' clause. It's just
automatically the first thing I do, kinda like automatically turning
the headlights off when I park the car (although the "headlights" thing
is admittedly much more subconscious than the curly braces). I'll
forget a semicolon pretty often, but the {} I haven't had a problem
with.
I might just be weird, though.
More information about the Digitalmars-d
mailing list