Is there any real reason to use "const"?
H. S. Teoh
hsteoh at quickfur.ath.cx
Mon Jan 24 17:55:39 UTC 2022
On Mon, Jan 24, 2022 at 09:30:26AM -0800, Ali Çehreli via Digitalmars-d wrote:
> On 1/24/22 04:31, Dennis wrote:
[...]
> > One day I had a single-line for-loop body, and to fix an error I
> > quickly added an `import` statement above it, pushing the for-loop
> > body outside the for-loop scope. Oops.
>
> I've done its counterpart just two days ago by commenting out one line
> (in somebody else's code):
>
> foreach(i; 0..2)
> // foo();
>
> bar();
>
> Oops! Now bar() is executed multiple times.
Huh, for some reason I was under the impression that D does not allow
un-braced blocks in a looping construct? Or maybe it's just
self-imposed restriction that became subconscious, probably precisely
because of bugs like these.
If-statements are another trap waiting to happen... but so far I've
found it very hard to resist the conciseness of:
if (x == y)
doSomething();
else if (y == z)
doSomethingElse();
else
doYetAnotherThing();
as opposed to the verbosity of:
if (x == y)
{
doSomething();
}
else if (y == z)
{
doSomethingElse();
}
else
{
doYetAnotherThing();
}
But in the former, it's too easy to e.g. add another line to the else
block and forget the add the braces as well.
For some reason I was under the impression that D didn't allow unbraced
loop bodies because in the case of loops such errors could be a lot
worse than in if-statements.
T
--
What do you get if you drop a piano down a mineshaft? A flat minor.
More information about the Digitalmars-d
mailing list