Comments (13)
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.
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.
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.
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.
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.
Clean code doesn't mean less code. It's about being more readable, manageable, and efficient.
from clean-code-javascript.
feel good
from clean-code-javascript.
Good. It should be easy to maintain when you add more params.
from clean-code-javascript.
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.
@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.
@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.
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.
i think it's good more attractive to eyes
from clean-code-javascript.
Related Issues (20)
- Bonne pratique JS HOT 1
- J HOT 1
- Farsi translation
- The code example of <Don't ignore rejected promises> may be wrong.
- Nice HOT 4
- Contradict HOT 1
- De code HOT 5
- Js two HOT 1
- eslint plugin
- Add OSSF Scorecard security workflow
- Clean code HOT 1
- Close Issues with no description/details attached. HOT 1
- J HOT 1
- J
- I
- Clean HOT 2
- Js HOT 1
- Clean HOT 1
- Adding translations HOT 3
- CleanCode HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from clean-code-javascript.