Enums code style
example:
var UpperCamelCase = {
UPPER_CASE: 0,
DELETED: 1,
REED: 2
};
Let's discuss.
upd: add semi-colon.
Looks nice, but missed closing semi-colon.
UpperCamelCase for enums also using in Google
+1 for the UpperCamelCase and the ALL_CAPS for the property names.
I would like to throw in some ideas:
- Use Object.freeze as stated in the comments of this SO answer. It will enforce read-only on JS-machine level.
- Use meaningful strings instead of numbers for the values. It will be a bit easier to debug.
+1 for this style:
enumintroduces "new type", likeclass. SoUpperCamelCasestyle is consistent.- Values of
enumare immutable. It's consistent with style for constants.
@dodev Do not use Object.freeze use new language features like const. It provides more clear code without runtime.
+1 for meaningful strings.
Use meaningful strings instead of numbers for the values. It will be a bit easier to debug.
In some cases I want use number:
var RegionTypes = {
CITY: 4,
COUNTRY: 7
};
where values are identifiers.
Also enumeration has overhead (if optimization tool isn't used): you can bloat your code with constructions like this:
var MapType = {
HYBRID: 'hybrid',
ROADMAP: 'roadmap',
SATELLITE: 'satellite'
TERRAIN: 'terrain'
};
Why not use string instead enum in this case?
@GREENpoint I also like the idea of using ES2015 constants, but const protects only the variable's value from overwriting. In the case of an Object, that's a reference, and the object itself can be altered by client code:
const obj = {a:1}; // => {a:1}
obj = {b:2};
obj; // => {a:1}
obj.a = 2;
obj; // => {a:2}
On the other hand, if you're experienced enough js developer you already know that you shouldn't alter API instances from your client code. So adding just the semantic sugar const will be enough just to make to code more readable.
@ikokostya the only two reasons I can think of are:
- avoiding typos (you'll get undefined if you miss-typed the name of the property and it would be easier to catch).
- using property names instead of strings can be aided by developer tools - like your text editor or you IDE ca enable auto-completion for this enum, or the static code-analyzer can warn you if you miss-typed the name of the enum.
Never the less you have a valid point there about the name repetition. Maybe we should leave this part up to the user.
There is also a case with "intrusive" enumerations, when enum constants are declared as static fields of a class in which desired enumeration is used. Example: consts GPU_TIME and CPU_TIME are meant as members of one enumeration, GPU_CPU_ORDER and CPU_GPU_ORDER – another one. But they all available as static members. For such a case some JSDoc convention is needed.
There is also a case with "intrusive" enumerations, when enum constants are declared as static fields of a class in which desired enumeration is used. Example: consts GPU_TIME and CPU_TIME are meant as members of one enumeration, GPU_CPU_ORDER and CPU_GPU_ORDER – another one. But they all available as static members. For such a case some JSDoc convention is needed.
In this case constants are part of existed type:
class GpuCpuTimeBar {}
/**
* @static
* @const
*/
GpuCpuTimeBar.GPU_TIME = 'gpu';
If you want use this constants in jsdoc in another place as separate type I think you can declare enumeration in namespace:
timebar::GpuCpuTimeBar
timebar::BarType
or even in class
GpuCpuTimeBar.BarType
@dodev UPPER_CASE_CONST_CONVENTION protects you from const object fields overloading.
@zloylos Can you make pull request?
Thank u, Kostya. #36