Dangling if
foobar
foo at bar.com
Mon Oct 1 08:32:12 PDT 2012
On Monday, 1 October 2012 at 12:56:05 UTC, Nick Sabalausky wrote:
> 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.
Modern cars do have a warning system - The car makes a beep noise
when opening the door while the headlights are on to remind the
driver to turn them off.
Regarding indent-based languages - It has more cognitive costs
for the programmer:
1. Tab vs. spaces holly-wars, how many spaces to use, etc, now
become language enforced instead of a human style choice.
2. Some "redundant" syntax noise is necessary in a language, to
make it easier for humans to "see" the code, even if it's
redundant from the compiler's perspective.
A good programming language should resemble more a natural
language than a mathematically rigorous language that is easily
machine parsed. After all, we are _not_ computers. Programming
language design is after all in large part a UX/UI question.
More information about the Digitalmars-d
mailing list