Why are all-caps constant considered bad coding st

2020-02-21 01:38发布

问题:

Jeff finished his post talking about this, but I don't catch the idea.

So, why do you think this is a bad coding style?

EDIT:

I, as a lot of you, don't think that it is a bad coding style. But Jeff is far better programmer than me, and his point of view turned on lights on my head to get answering if I was wrong. I was a Delphi developer for some few years and am becoming a C# developer now, and in Delphi it was a common practice.

回答1:

All caps is the traditional C designation of a preprocessor macro constant. It's very useful to have these in a different namespace from anything else, since the preprocessor will substitute wherever it finds the name, regardless of things like scope.

A constant in the sense that Jeff was using is, semantically, a variable that can't be changed. It obeys all scoping principles and everything, and is semantically identical to a non-const variable with the same value.

To put this another way,

#define max_length 5

is a problem because somebody might use max_length as a variable in a different context, where it would normally be safe, while

const int max_length = 5;

is simply a variable declaration. Therefore, there's an advantage in using

#define MAX_LENGTH 5

because the convention is that only preprocessor constants are all-caps, so it will not interfere with any other use.



回答2:

You'll find that a lot of Jeff's statements are controversial first, with accuracy being a secondary concern. His blog wouldn't be that popular if he weren't occasionally inflammatory. (Consider the last line of Death to the Space Infidels, "That said, only a moron would use tabs to format their code.") It's honestly subjective. Don't take everything he says as True and Good -- it's not meant to be. If you disagree, go kick his ass in the comments, and see if he writes back. :)

I think ALL_CAPS_CONSTANTS are perfect: they're instantly recognizable and familiar. Part of my workplace's style guidelines (and we don't have that many) is to write all static constants in caps. Don't sweat it; just use whatever the rest of your team uses. Deciding on StudlyCaps vs. camelCase vs SCREAMING_CAPS is worth maybe 90 seconds discussion.



回答3:

Screaming is fine. In the case of the constant it tells the reader

DONT THINK ABOUT CHANGING ME LATER IN CODE

But I understand if soft programmers get offended.



回答4:

Frankly, I don't think it is bad coding style. Indeed, even the official Java code style makes constants all capitals (http://java.sun.com/docs/codeconv/html/CodeConventions.doc8.html), as do other language conventions.

In theory, it's harder to read - we humans like to use the variable height of letters to increase reading speed (we can infer a lot of information from just rough word shape and the first/last letters). However, I don't have a problem with short, all capital phrases. It's hardly "shouting" (you compiler doesn't exactly care how rude you are), and clearly shows the names are potentially mutable, and those that are not.



回答5:

I don't think it's bad coding style. Maybe old-fashioned, but not bad. It does make constants stand out from other variables.



回答6:

The only compelling reason to me is consistency in style. For non-.NET languages like Java / C++, all-caps constants are certainly acceptable.

For C#, the standard is to use PascalCase, as noted here: C# naming convention for constants?



回答7:

ALL CAPS IS LIKE SHOUTING AT SOMEONE.



回答8:

IT ALSO MAKES THE CODE HARDER TO READ



回答9:

Jeff has some additional things to say on this in the comments:

However on the other side and the reason I do still use the all caps and underscore

Less code? That's a worthwhile cause deserving of serious discussion.

But whether you call something "foo", "Foo", "_foo", or "FOO"? Meh.

Naming conventions are highly controversial and religious. Developers should pick something they like, something that's hopefully not too much at odds with the "local conventions", and just go with it. A lot of discussion and hand-wringing over naming isn't worthwhile.

That said, I think ALL CAPS IS REALLY HARD TO READ!

I also use this for constants, but I also can understand why some people don't like it. It's a bit like writing everything in lowercase in german or other languages.



回答10:

Well, there's the old readability issue. Normally-cased test is just easier to read.

There are exceptions, though. Languages like SQL are usually all upper-case (although case-insensitive).

Also, there are other uses, like to distinguish between "constants" and regular variables, which you'll see in a lot of languages like PHP, Python, etc., even though Jeff for some reason doesn't like that, and it is apparently against the C# code style guidelines.

So generally, I don't think it's wrong anywhere, but I do think that one should always try and follow the general best practises. When in Rome, do as the Romans – when coding Python, follow PEP 8 :)



回答11:

FLIP THE QUESTION, WHY USE ALL CAPS?



回答12:

I don't think that it is wrong. Mostly, it's a personal choice. For your personal coding, do what you want. For you professional coding, follow company policy.



回答13:

Just like writing everything in bold is not a good idea



回答14:

I remapped my capslock to a ctrl key (woo emacs!). So I think its bad style when I have to type 30-character names while holding down shift



回答15:

Some people consider ALL CAPS to be "old school". Consider the difference between:

const string ErrorMessage = "Some error message.";

and

const string ERROR_MESSAGE = "Some error message.";

Both are completely usable, but the ALL CAPS version is less used by newer developers, such as the ones that started out with .NET.

I consider it a bad coding style if your team is using a different style. Other than that, I don't really care. At least when I see ALL CAPS in some shared code, I can guess that it's a constant.



回答16:

@ck,

BECAUSE IT'S CONVENTION.



回答17:

I use ALL_CAPS for macros and preprocessor symbols. So my pre-C99 C constants are ALL_CAPS, but not in any other language I know of.



回答18:

Constants and CONSTANTS might differ. Use all-caps only when dealing with preprocessor, that being said for example in C++ it's recommended to avoid that and use const variable or constexpr which would be named just like any other variables (maybe with some prefix or something to make it clear that it's a constant but...).