Giter Club home page Giter Club logo

Comments (13)

ryanmcdermott avatar ryanmcdermott commented on May 2, 2024 18

Keyboard keycodes are another example you see in the front-end JavaScript world, where you have something like:

const KEYCODES = {
  ENTER: 13
};

document.addEventListener('keydown', function(event) {
  if (event.which === KEYCODES.ENTER) {
    //...
  }
}

from clean-code-javascript.

ProLoser avatar ProLoser commented on May 2, 2024 13

This is typically how I do things. I'll also go a step further and add enums statically to my classes for more organized namespacing (rather than just having randomly floating constant objects).

export default class Person { /* ... */ }

// Static Property:
Person.GENDERS = {
  MALE: 'm',
  FEMALE: 'f',
  OTHER: 'na'
};

// Const + Static Property:
const GENDERS = { ... };
Person.GENDERS = GENDERS;

// New Syntax Static Property:
class Person {
  static const GENDERS = { ... };
}

// Static Property Usage:
import Person from './Person';
let bob = new Person();
bob.setGender( Person.GENDERS.MALE );

// Or you can do a Named Export Variation:
import Person, { GENDERS } from './Person'; // or { GENDERS as PERSON_GENDERS }
let bob = new Person();
bob.setGender( GENDERS.MALE );

This allows me to keep the enums very close to their intended usage. And it's easier to expose since I don't have to go around creating some unrelated global / flat constants container object. person.gender can be one of Person.GENDERS in essence.

from clean-code-javascript.

Tarabass avatar Tarabass commented on May 2, 2024 3

Sounds good. The example you wrote isn't that good imo. In most cases categories will be dynamic, just like posts. Not changing is a characteristic of magic strings I believe. Maybe the example will be more clear when a enum with months is used and the function is called getPostsByMonths(months.jan) or something :)

from clean-code-javascript.

ryanmcdermott avatar ryanmcdermott commented on May 2, 2024 1

Hey there Quique! It sort of feels like you're referring to an Enum. JavaScript of course doesn't have them! It also feels like you're referring to named constants as in the case of Searchable Names. https://github.com/ryanmcdermott/clean-code-javascript#use-searchable-names

It's sort of an extension of that too, where you're hiding the textual implementation details of the sports category key, and you're simply namespacing them under a constant categories object. All in all, it's an interesting idea to extend the subsection on Searchable Names to have namespaced constants. What does everyone else think?

from clean-code-javascript.

jacktuck avatar jacktuck commented on May 2, 2024 1
const KEYCODES = {
  ENTER: 13
};

document.addEventListener('keydown', function(event) {
  if (event.which === KEYCODES.ENTER) {
    //...
  }
}

When u wish actual enums existed xD

from clean-code-javascript.

sowrovsarkar63 avatar sowrovsarkar63 commented on May 2, 2024 1

Clean code doesn't mean less code. It's about being more readable, manageable, and efficient.

from clean-code-javascript.

kaneruan avatar kaneruan commented on May 2, 2024

feel good

from clean-code-javascript.

alexshinego avatar alexshinego commented on May 2, 2024

Good. It should be easy to maintain when you add more params.

from clean-code-javascript.

CKGrafico avatar CKGrafico commented on May 2, 2024

I think that all of you are right, maybe not the best example xD
Totally useful to use as Enums and some examples could be:

  • Days of week (days.monday)
  • Months of year (months.january)
  • Event names (events.mouseover)

etc.

from clean-code-javascript.

toddmantell avatar toddmantell commented on May 2, 2024

@jacktuck Just out of curiosity, what advantage do enums give over constants? I used to do C# and we had enums and I never really understood why they were that great.

from clean-code-javascript.

jacktuck avatar jacktuck commented on May 2, 2024

@toddmantell Enums can not be changed. Objects can be, unless you freeze them. That's the only behavioural difference I can think of. Other than that, aesthetics, I guess.

from clean-code-javascript.

 avatar commented on May 2, 2024

@jacktuck Object.seal.

const enum = Object.seal;

const KEYCODES = enum({
  ENTER: 13
});

KEYCODES.ENTER = 14;
KEYCODES.NEW_KEY = 15;
delete KEYCODES.ENTER;

KEYCODES // => { ENTER: 13 }

from clean-code-javascript.

AdiRaushan avatar AdiRaushan commented on May 2, 2024

i think it's good more attractive to eyes

from clean-code-javascript.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.