Is there any real reason to use "const"?
H. S. Teoh
hsteoh at quickfur.ath.cx
Mon Jan 24 19:00:45 UTC 2022
On Mon, Jan 24, 2022 at 10:22:47AM -0800, Ali Çehreli via Digitalmars-d wrote:
> On 1/24/22 09:55, H. S. Teoh wrote:
>
> > as opposed to the verbosity of:
> >
> > if (x == y)
> > {
> > doSomething();
> > }
> > else if (y == z)
> > {
> > doSomethingElse();
> > }
> > else
> > {
> > doYetAnotherThing();
> > }
>
> Luckily, humans are very adaptive and can be happy with the following
> style. ;)
>
> if (x == y) {
> doSomething();
>
> } else if (y == z) {
> doSomethingElse();
>
> } else {
> doYetAnotherThing();
> }
I used to be a big fan of this style, in fact. But these days, I write
in Phobos style -- mainly because at one point I was actively
contributing to Phobos and it was just too much of a hassle to have to
keep switching mental gears between two divergent styles. After a while,
I grew to like the better clarity of the extra whitespace around my
code, and stuck to Phobos style ever since.
There are still cases where this whitespace becomes excessive, though,
and the above code is an example. But to mix the two styles in the same
code would be even worse, so for now I'm just gritting my teeth over
unbraced if-statements. :-D
> Especially note TABs are only for Makefiles. :p
I used to be a big fan of tabs. Developed a whole philosophy around why
tabs were superior (mainly based around the 80's now-outdated philosophy
of saving every last byte because you only had 64K of RAM so every
little bit counts). These days, I set expandtab in Vim for all code,
and am a lot happier for it. :-P
Two-space indentation though... I used to be a big fan of that, in my
Perl phase. Needless to say, in retrospect, 2-space indentation + Perl
kookiness = completely unreadable code. These days I prefer 4 spaces.
(Well OK, that's also the influence of Phobos style, but it's much
easier to tell what level you're at with 4-space indentation than with
2-space. And like Torvalds would say, if you need to indent so deep that
it's pushing against the right edge of the screen, you're doing
something wrong and should refactor your code to avoid that many levels
of nesting in the first place. (He was talking about 8-space tab
indentation BTW. Though for D code 8-space indentation is a bit too
excessive, given the typical nesting level of idiomatic D code.))
> But I can like the following no-brace formatting as well:
>
> if (x == y) doSomething();
> else if (y == z) doSomethingElse();
> else doYetAnotherThing();
[...]
This really scares me, because it's also extremely easy to make
mistakes (e.g., need to add another line to the else block, so wrap the
existing line to a new line and append another line -- while forgetting
to add braces).
T
--
One Word to write them all, One Access to find them, One Excel to count them all, And thus to Windows bind them. -- Mike Champion
More information about the Digitalmars-d
mailing list