Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

That's how it feels, but the syntax for declaring multiple variables breaks it:

    int* a, b;
is equivalent to

    int *a;
    int b;
not

    int *a;
    int *b;
So while I think it makes sense to think of the pointer nature as part of the type I stopped using it as the risk of writing incorrect code was too great.

Though there's also the option of forbidding shorthand declaration. Then you can write

    int* a;
    int* b;
especially if you have a linter letting you add such a syntax rule.


The "don't declare multiple variables on one line" is a more important rules IMO, that makes the thing that's relatively unimportant (where the * is next to) go away.


Given the numerous advantages of int* a; rather than int a;, and the general laziness of declaring variables together anyway, wouldn't it be more reasonable, as a house style, to require int and to disapprove the declaration of multiple variables in the same statement?


Note: using % instead of * below because I can't for the life of me figure out how to type an asterisk without it being used as an italics markup directive.

>> Given the numerous advantages of int% a; rather than int %a;

Care to elaborate what 'numerous advantages' there are to use int% a instead of int %a, because I can't think of a single one besides 'it feels more natural', which IMO is irrelevant in the context of a coding style.

Enforcing a single line for each variable declaration does not sound like a good idea, in mathematical code this can quickly grow unwieldy due when lots of variable are involved. It also often makes sense to group declarations of related variables on the same line to indicate they are used in the same way, for example 'int v0i, v1i' to indicate two vertex indices.


Sure. And as I noted, you may even be able to configure your linter of choice to handle that automatically. There's also the macro option, I guess.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: