20

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.

BIBD
  • 15,107
  • 25
  • 85
  • 137
eKek0
  • 23,005
  • 25
  • 91
  • 119
  • 6
    Probably ought to be community wiki. – chaos Apr 22 '09 at 15:08
  • In regards to Delphi, all caps for constants is generally forbidden [according to the official style guide](http://edn.embarcadero.com/article/10280#3.3). – DBedrenko Sep 24 '14 at 09:27

18 Answers18

58

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.

David Thornley
  • 56,304
  • 9
  • 91
  • 158
  • 6
    +1 In addition, if you use macros for calculations (say the canonical bad MAX() and MIN() bit), it helps to know that arguments to these objects have pre-processor semantics not compiler semantics. – dmckee --- ex-moderator kitten Apr 22 '09 at 15:49
30

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.

Trisped
  • 5,705
  • 2
  • 45
  • 58
ojrac
  • 13,231
  • 6
  • 37
  • 39
20

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.

9

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.

Adam Wright
  • 48,938
  • 12
  • 131
  • 152
  • _you compiler doesn't exactly care how rude you are_ — unless you're writing in [INTERCAL](https://en.wikipedia.org/wiki/INTERCAL). – Ruslan Dec 02 '17 at 08:46
7

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

Jeff
  • 21,744
  • 6
  • 51
  • 55
5

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?

Community
  • 1
  • 1
Scott Wegner
  • 7,263
  • 2
  • 39
  • 55
4

ALL CAPS IS LIKE SHOUTING AT SOMEONE.

S.Lott
  • 384,516
  • 81
  • 508
  • 779
3

IT ALSO MAKES THE CODE HARDER TO READ

Sergio
  • 8,125
  • 10
  • 46
  • 77
3

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.

schnaader
  • 49,103
  • 10
  • 104
  • 136
3

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 :)

mikl
  • 23,749
  • 20
  • 68
  • 89
2

FLIP THE QUESTION, WHY USE ALL CAPS?

cjk
  • 45,739
  • 9
  • 81
  • 112
2

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Robert S.
  • 25,266
  • 14
  • 84
  • 116
  • 1
    -1: Not "old school", but language/platform dependent. In .NET it is discourages, but other cases it is encouraged. – Richard Apr 22 '09 at 15:09
  • 2
    I guess you didn't read Jeff's post, in which he specifically called it "old school." – Robert S. Apr 22 '09 at 15:56
2

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.

Muad'Dib
  • 28,542
  • 5
  • 55
  • 68
2

Just like writing everything in bold is not a good idea

RN.
  • 997
  • 4
  • 14
  • 31
2

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

Alex
  • 4,316
  • 2
  • 24
  • 28
  • 3
    For whatever reason, I practically never use the caps-lock key, at least intentionally. I type quickly, but not using the "official" touch-typing methodology. I simply hit keys with whatever finger feels appropriate at the time, and have no trouble typing any of the left-hand characters while holding down the left-shift. – supercat Apr 17 '11 at 23:46
0

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...).

Hitokage
  • 733
  • 8
  • 19
0

@ck,

BECAUSE IT'S CONVENTION.

lfaraone
  • 49,562
  • 17
  • 52
  • 70
0

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.

jfclavette
  • 3,457
  • 2
  • 20
  • 17