Naming: Why should named constants be all uppercase in C++/Java?

前端 未结 15 1774
情歌与酒
情歌与酒 2020-12-15 16:13

I know, that for C++ and Java it is a well established naming convention, that constants should be written all uppercase, with underscores to separate words. Like this (Java

15条回答
  •  悲&欢浪女
    2020-12-15 16:26

    In C (and then C++), symbols that were defined with the preprocessor #define directive were written all in caps as a loud signal to developers that the code was not as it seemed and to not treat as a normal symbol. Constants did not initially exist in K&R C (although const was brought back from C++ later) so developers used #define instead, hence the caps.

    For some reason, this got twisted into Java as "constants are caps", hence the current misguided (IMO) convention.

提交回复
热议问题