mapsapi-codestyle icon indicating copy to clipboard operation
mapsapi-codestyle copied to clipboard

Enums code style

Open ghost opened this issue 11 years ago • 12 comments

example:

var UpperCamelCase = {
    UPPER_CASE: 0,
    DELETED: 1,
    REED: 2
};

Let's discuss.

upd: add semi-colon.

ghost avatar Oct 15 '15 19:10 ghost

Looks nice, but missed closing semi-colon.

GREENpoint avatar Oct 15 '15 20:10 GREENpoint

UpperCamelCase for enums also using in Google

ghost avatar Oct 15 '15 20:10 ghost

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

dodev avatar Oct 16 '15 09:10 dodev

+1 for this style:

  • enum introduces "new type", like class. So UpperCamelCase style is consistent.
  • Values of enum are immutable. It's consistent with style for constants.

ikokostya avatar Oct 16 '15 12:10 ikokostya

@dodev Do not use Object.freeze use new language features like const. It provides more clear code without runtime.

+1 for meaningful strings.

GREENpoint avatar Oct 16 '15 17:10 GREENpoint

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?

ikokostya avatar Oct 16 '15 21:10 ikokostya

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

  1. avoiding typos (you'll get undefined if you miss-typed the name of the property and it would be easier to catch).
  2. 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.

dodev avatar Oct 17 '15 15:10 dodev

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.

dmikis avatar Oct 19 '15 11:10 dmikis

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

ikokostya avatar Oct 19 '15 14:10 ikokostya

@dodev UPPER_CASE_CONST_CONVENTION protects you from const object fields overloading.

GREENpoint avatar Oct 19 '15 15:10 GREENpoint

@zloylos Can you make pull request?

ikokostya avatar Oct 29 '15 17:10 ikokostya

Thank u, Kostya. #36

ghost avatar Nov 02 '15 09:11 ghost