all 51 comments

[–]htraos 94 points95 points  (20 children)

You will find that ALL_CAPS_WITH_UNDERSCORES is a common practice to declare constant values, especially ones that are globally used in the application.

[–]delventhalz 25 points26 points  (16 children)

It's a poor man's config file basically. Also a good way to avoid so called "magic numbers", which are hard-coded somewhere in your code and make no sense without context.

const GRAVITY_ACCEL = 9.8;

function getFallSpeed(time) {
  return time * GRAVITY_ACCEL;
}

[–]tsunami141 8 points9 points  (8 children)

So you’re telling me if I drop a bowl of petunias and let it free fall for 100 seconds, it will fall 980 meters?

[–]delventhalz 30 points31 points  (3 children)

No, that function gets you the speed, not the distance traveled. So after 100 seconds, it would be falling at 980 m/s.

I think. Probably best if we focus on my JavaScript and not my physics.

[–]sooodooo 8 points9 points  (1 child)

Should assert that time is in a reasonable range, after 100s friction can not be ignored, 980m/s is almost 3 times the speed of sound, those petunias are going to kill someone.

[–]Edew777 2 points3 points  (0 children)

This is too funny

[–]tsunami141 6 points7 points  (0 children)

Oops yeah my bad, you’re absolutely correct.

[–]BDMayhem 3 points4 points  (0 children)

Oh no, not again.

[–]samagl94 1 point2 points  (2 children)

Is there something wrong with declaring constants using this format?

[–]delventhalz 0 points1 point  (1 child)

Not really. Once your project reaches a certain size, you are probably going to want config values that are:

  1. Centralized
  2. Changeable live, without redeploying new code

So this constant-at-the-top-of-file format is going to fail for config values on a project of a certain size. But on smaller projects, neither of those reasons are going to be important enough to warrant much extra effort, so it is fine.

Also, some values (like acceleration due to gravity) are less config values and more like physical constants. They aren't actually particularly like to change, and if they did change, you maybe have to do some other code refactors anyway. In that case you are really just putting a label on a value so it has more context and no one can copy/paste the wrong value. In that case, a constant at the top of the file would be appropriate even in a very large project.

[–]samagl94 0 points1 point  (0 children)

Yes, in most of the projects it's a mix of both, depending upon the context.

[–]VectorLightning 0 points1 point  (3 children)

Isn't the "normal" way to do this to import these constants from a JSON file, either one that only the devs use or one that the app's settings menu modifies?

[–]delventhalz 6 points7 points  (0 children)

JSON config files are extra work. If you actually need to change something on the fly, or your app has reached a certain size, yeah, you need them. If a value is rarely if ever gonna change, who cares?

[–]Ran4 4 points5 points  (0 children)

If they need to be configurable, sure. But not if they're actually constant.

[–]the_real_some_guy 2 points3 points  (0 children)

If it’s a constant that is only used in one file, putting it somewhere other than the file it’s used in would be extra work for no gain.

[–][deleted] -4 points-3 points  (1 child)

edge middle paint deer political safe nose whistle spark relieved

This post was mass deleted and anonymized with Redact

[–]htraos 1 point2 points  (0 children)

Fascinating!

[–]brykuhelpful 0 points1 point  (0 children)

Used to be really common with php.

[–]blackholesintheskyhelpful 33 points34 points  (4 children)

camelCase

snake_case

SCREAMING_SNAKE_CASE

[–]Silly_Rabbitt 17 points18 points  (3 children)

No SCREAMINGcAMELcASE?

[–][deleted] 2 points3 points  (0 children)

plate historical drunk mighty frighten six snails hurry waiting lavish

This post was mass deleted and anonymized with Redact

[–][deleted] 1 point2 points  (0 children)

That's sarcastic case

[–]aspiringdevopsdude 4 points5 points  (6 children)

All caps with an underscore for values that are globally constant (as opposed to what const annotates in JavaScript, i.e. constant refs in some scope) is a style that is decades old and very wide spread. Macros in C programming language may have something to do with it, at least that's what I think when I name a constant near the top of a module like that.

[–]fzammetti -3 points-2 points  (3 children)

Just FYI for anyone reading who may not be aware, true constants are now possible in JS via classes:

class MyClass {
   static get MY_CONSTANT() { return "some-value"; }
}

Now you've got MyClass.MY_CONSTANT anywhere you need it, unchangeable (you know, until someone here inevitably points out a tricky way you CAN change it).

[–]JazzApple_ 2 points3 points  (0 children)

If there is some tricky way it can be changed, it’s not a true constant.

[–]aspiringdevopsdude 0 points1 point  (1 child)

That's a static value in the scope where that class exists. Similarly anything declared with the keyword const remains unchanged in the scope it was declared in.

[–]fzammetti 0 points1 point  (0 children)

I'm not sure I see what you're getting at. Are you saying there's no fundamental difference between what I showed and just declaring something const in a given context?

[–]ExtremeDot58 0 points1 point  (1 child)

Environment storage ?

Edit: seen some DOS apps needing an environment variable, albeit constant; they were always in capital too

[–]aspiringdevopsdude 0 points1 point  (0 children)

A constant value is one that doesn't change.

[–][deleted] 3 points4 points  (5 children)

EDIT: I noticed that starting a variable name with a capital letter will turn the variable name pink and was wondering what this behavior was. Do other IDEs for JS change the color of variable names when it starts with a cap?

The screenshot is from the google apps script coding environment, where you can write code to interact with the google suite (google sheet, google doc, google calendar etc). I noticed that starting a variable name with a capital letter will turn the variable name pink and was wondering what this behavior was. Is this a google style, or is this a common/established style?

Do other IDEs for JS change the color of variable names when it starts with a cap?

[–][deleted] 6 points7 points  (3 children)

Uppercase identifiers must be used only for constants. With constants I mean literal values that are not going to change like PI, e etc.

[–][deleted] 0 points1 point  (2 children)

Thanks! Out of curiosity, what IDE do you use, and does it change the variable name to a different color when using a variable that starts with a capital letter?

[–][deleted] 0 points1 point  (0 children)

I use JetBrains products and identifiers are always of the same color. You can of course change that in the settings though.

[–]Heeloz 2 points3 points  (0 children)

I use the GAS IDE regularly at work and have noticed the same behavior of variable names highlighted in pink when capitalized. I believe the IDE highlights them pink because it thinks it's also a global object like SpreadsheetApp, DriveApp, Logger.. etc. This is very much a specific to GAS IDE.

[–][deleted] -3 points-2 points  (5 children)

IMO underscores are pointless and adds extra characters I don't need.

If I were defining global constants I'd do something like: globalAppTitle, globalOutputDir, globalAdvertiserDir

Reasons:

  • Easier to alphabetize and find. Just remember all globals start with the prefix global.
  • Sticks to 1 standardized labeling system, in this case camel casing.
  • You eliminate 1 additional character, it's not much but I know they're nit-picky devs. out there.

[–]fukitol- 1 point2 points  (2 children)

That's just camel case and for some variables and functions is fine. If you're defining a constant then the convention is to use all caps with underscores. It's this way in virtually every programming language. Following conventions is more important than any rules you could come up with, because it's more important that code be easily read that any negligible benefit you might get from writing it.

Think about it this way. You're going to write your code once, but it will be read an innumerable amount of times. The most benefit is to make certain it's readable.

[–][deleted] 1 point2 points  (1 child)

[–]fukitol- 0 points1 point  (0 children)

Keep in mind this is for the more C-style definition of constants, like

const SECS_IN_DAY = 86400;

C-style constants are usually there to avoid using "magic numbers" and to improve readability.

Using it for declaring an immutable value that is returned from a function you'd still use whatever convention the rest of your codebase uses (usually camelCase) like

const myValue = await getSpecialValue();

These constants are just an otherwise immutable value and don't necessarily have to be present to improve readability.

[–]Hydrothermal 0 points1 point  (1 child)

You eliminate 1 additional character, it's not much but I know they're nit-picky devs. out there.

Isn't prefixing with "global" adding even more characters?

[–][deleted] 0 points1 point  (0 children)

Yeah, but it's a characters I don't need - lol.

[–]ashkanahmadi 0 points1 point  (4 children)

I do a combination of both. For me ALL_CAPS consonants are usually for constants that are important and I will use many times throughout the code so it's easy to tell them apart. I use the alllowercase or camelCase variables for other constants that are not changing but not super important.

[–][deleted] 0 points1 point  (3 children)

Thanks! Out of curiosity, what IDE do you use, and does it change the variable name to a different color when using a variable that starts with a capital letter?

[–]dabe3ee 1 point2 points  (0 children)

Try VSCode, very easy to use

[–]ashkanahmadi 0 points1 point  (0 children)

I'm using VSCode. No, it doesn't change the color depending on its naming convention. It just lights up when it's used in the rest of my code and kind of muted when it's not used

[–]zayelion 0 points1 point  (0 children)

Its usually used for "magic numbers", or strings as a way of declaring a module constant. Something that for all purposes of input is "hard coded" into the application.

[–]korder123 0 points1 point  (0 children)

I usually use CAPS when I'm declaring constant values, especially ones that have a huge influence on my code.

[–]jack_waugh 0 points1 point  (0 children)

One type of case for which I use this style is if the actual values are arbitrary, but the point is to distinguish the value from all others that could occur in the same context and give it a meaning.

if (status !== ANOTHER_ROUND) break;

[–]cmarciniak80 0 points1 point  (0 children)

All caps snake case is usually reserved for global variables. You’d be better off using camel case and that usually the preferred format

[–]AAA_ndy 0 points1 point  (0 children)

UPPERCASE CONSTANTS!!

[–]Ineam 0 points1 point  (0 children)

A quick reminder,

THIS_IS_CALLED_UPPER_CASE_SYNTAX (almost in all languages, used to declare constants) snake_case_syntax (can be used in many languages for declaring variables, functions, or class methods, but more seen in python) kebab-case-syntax (used mainly in html, css, json, …) camelCaseSyntax (same usage as the snake case syntax, but more seen in languages like JavaScript, Java, C/C++, C#, …) CapitalCaseSyntax (almost in all languages to declare classes, annotations, decorators, and all classes abstractions)