question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

What about Magic strings?

See original GitHub issue

Do you think that can be good to include something about magic strings?

Bad:

let posts = getPostsFromCategory('sports');

Good:

const categories = {
  sports: 'sports',
  economy: 'economy'
};

let posts = getPostsFromCategory(categories.sports);

Issue Analytics

  • State:open
  • Created 7 years ago
  • Comments:12 (3 by maintainers)

github_iconTop GitHub Comments

18reactions
ryanmcdermottcommented, Jan 17, 2017

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) {
    //...
  }
}
13reactions
ProLosercommented, Feb 19, 2019

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Magic Strings - DevIQ
Magic strings are string values that are specified directly within application code that have an impact on the application's behavior.
Read more >
Magic string - Wikipedia
In computer programming, a magic string is an input that a programmer believes will never come externally and which activates otherwise hidden functionality...
Read more >
What is wrong with magic strings?
If a magic string is being written in multiple places you have to change all of them without any safety (such as compile-time...
Read more >
The Magic Strings Anti-Pattern - Develpreneur
“Magic strings are string values that are specified directly within application code that have an impact on the application's behavior.
Read more >
TypeScript - Taking The Magic Out of Magic Strings
Magic strings are string literals strewn about a code base that apply some kind of limitation to the code. They can be used...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found