Skip to content

Commit a9f5d51

Browse files
shanemilehamljharb
authored andcommitted
Added naming--uppercase section from comment
1 parent 5fd7c6f commit a9f5d51

1 file changed

Lines changed: 24 additions & 13 deletions

File tree

README.md

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3140,32 +3140,43 @@ Other Style Guides
31403140
```
31413141
31423142
<a name="naming--uppercase"></a>
3143-
- [23.10](#naming--uppercase) Use UPPERCASE for nested object namespacing, global variables, and constants.
3143+
- [23.10](#naming--uppercase) You may optionally uppercase a constant only if it (1) is exported, (2) is a `const` (it can not be reassigned), and (3) the programmer can trust it (and its nested properties) to never change.
31443144

3145+
> Why? This is an additional tool to assist in situations where the programmer would be unsure if a variable might ever change. UPPERCASE_VARIABLES are letting the programmer know that they can trust the variable (and its properties) not to change.
3146+
- What about all `const` variables? - This is unnecessary, so uppercasing should not be used for constants within a file. It should be used for exported constants however.
3147+
- What about exported objects? - Uppercase at the top level of export (e.g. `EXPORTED_OBJECT.key`) and maintain that all nested properties do not change.
31453148

31463149
```javascript
31473150
// bad
3148-
const namespace = namespace || {};
3151+
const PRIVATE_VARIABLE = 'should not be unnecessarily uppercased within a file';
31493152
3150-
namespace.util.Widget = {
3151-
// ...stuff...
3152-
}
3153+
// bad
3154+
export const THING_TO_BE_CHANGED = 'should obviously not be uppercased';
31533155
31543156
// bad
3155-
const apiKey = '44b345234534t455245njkl523452-vbb9';
3157+
export let REASSIGNABLE_VARIABLE = 'do not use let with uppercase variables';
31563158
3157-
// good
3158-
const NAMESPACE = NAMESPACE || {};
3159+
// ---
31593160
3160-
NAMESPACE.util.Widget = {
3161-
// ...stuff...
3162-
}
3161+
// allowed but does not supply semantic value
3162+
export const apiKey = 'SOMEKEY';
3163+
3164+
// better in most cases
3165+
export const API_KEY = 'SOMEKEY';
3166+
3167+
// ---
3168+
3169+
// bad - unnecessarily uppercases key while adding no semantic value
3170+
export const MAPPING = {
3171+
KEY: 'value'
3172+
};
31633173
31643174
// good
3165-
const API_KEY = '44b345234534t455245njkl523452-vbb9';
3175+
export const MAPPING = {
3176+
key: 'value'
3177+
};
31663178
```
31673179

3168-
31693180
**[⬆ back to top](#table-of-contents)**
31703181

31713182
## Accessors

0 commit comments

Comments
 (0)