1

I recently started learning and using JavaScript with Node.js. One of the things I noted, in different tutorials and also in production code, is that there are lots of hardcoded strings. For example: https://www.tutorialspoint.com/nodejs/nodejs_event_emitter.htm

Same applies for production code - using strings for eventNames, property name, different status values, etc. It makes it harder to follow through existing code. Is there any good reason not to use patterns like this for example?

var Events = {GoodEvent: 'goodEvent', BadEvent: 'badEvent'};
myObject.on(Events.GoodEvent, func)
Anorflame
  • 376
  • 3
  • 12
  • You can do that, but you've added extra code. This would be beneficial if you were using it in many places, or that it needs updating, and that update needs to be reflected across the app. – evolutionxbox Nov 27 '17 at 10:23
  • 1
    JavaScript doesn't perform static type checking, so you don't benefit from using enums as much as you would e.g. in TypeScript. It helps with automatic code completion though. – le_m Nov 27 '17 at 10:29
  • The problem I'm talking about is code's readability. In this way its a lot easier to see all existing events, avoid typos in names, make changes to those strings. All the things that are taught in intro courses in Computer Science, – Anorflame Nov 27 '17 at 10:35

0 Answers0