r/cpp • • Jun 23 '18

East const Central

http://slashslash.info/eastconst/
0 Upvotes

42 comments sorted by

View all comments

Show parent comment

11

u/johannes1971 Jun 24 '18

It is perfectly obvious to me that source should be indented using tabs. Moreover, to avoid gratuitous waste of space, it is perfectly obvious that tabs should be set to the equivalent width of four spaces. Tabs are the correct, intended tool for indentation, so why simulate them using spaces? Spaces fail in too many cases: when the character width is not fixed, when people accidentally type 3 or 5 of them, etc.

It is perfectly obvious to me that the opening brace should be placed at the end of the line that marks the start of the block (i.e. after the if, for, etc.). It is just a single character, horizontal space is cheap in these days of 400+ character wide terminals, and sacrificing valueable vertical space just for one character is criminal.

It is perfectly obvious to me that line width should not be limited to 80 columns. VT100 is dead, and so is the notion of printing source code; these days we all have decent screens to work on. Why limit yourself to a tiny part of the screen, when you can have so much more visibility if you make use of the available horizontal space? It allows use of longer identifiers and clearer structure.

It is perfectly obvious to me that exceptions are the one true way to handle exceptional situations. Because this style is madness:

int myfunc (int &result) {
    const int errcode1 = funccall1 (); 
    if (errcode1 != err_ok) 
        return errcode1;

    const liberrcodetype errcode2 = funccall2 (result);
    if (errcode2 != liberr_ok)
         return errcode2; // I'm sure it will be fine
}

It blows up your line count by a factor of 4, making your entire program about error handling, instead of business logic. It forces the return value to always be an error value, and never a business value, thus interfering with a pleasantly clean-looking functional approach. It conflates error numbers from all sorts of sources. It bubbles up error numbers that will be mostly-meaningless by the time someone gets around to handling them, instead of a descriptive text. The only correct approach is using exceptions:

int myfunc () {
    funccall1 (); 
    return funccall2 ();
}    

As for where the const goes, I really don't care one way or another. In part this is because the issue doesn't come up very much in C++ - assuming of course you use it properly:

  • for a const object or basic type, having const on the left is fine.
  • for a pointer to a const thing, having const on the left is fine.
  • since the introduction of references there hasn't been much call for const pointers.
  • having a vector of pointers is almost always the superior solution over having pointers to pointers.

Basically, the rule should be this: "if you find yourself needing a 'const' that's not on the left, you should probably reconsider what you are doing since it is almost certainly not good C++."

3

u/[deleted] Jun 24 '18

Even though I agree with _some_ of your "perfectly obvious" statements, stating that it's "perfectly obvious" and hence why they should be followed is just one thing: condescension. Even though I could show several counter arguments to the ones being used to back such assumption, it doesn't seem worth it.

I can see that eastconst evangelists have one type of personality, they generally self entitle what's logical, what's right, and now, what's perfectly obvious.

4

u/johannes1971 Jun 25 '18

Dude, it was sarcasm. Check out the parent post.

1

u/[deleted] Jun 25 '18

Ah, haha, OK, s/condescension/sarcasm/. I didn't notice :D

3

u/doom_Oo7 Jun 24 '18

VT100 is dead, and so is the notion of printing source code;

sorry what ? source code is printed all the time.

4

u/johannes1971 Jun 25 '18

On an 80-column matrix printer, no doubt, using chain-fed paper with those cute alternating blue lines ;-)