Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why nodejs use magic strings in its core modules willy-nilly?

I’m learning nodejs and having previous experience with other {strongly typed} languages I’m suffering a bit finding the reason why there is so much abuse of magic values (mainly strings) in core modules of nodejs.

Just to give an example, let’s take the implementation of the http module which has classes that extends EventEmitter and relies on strings to identify events, such as the http.Server class with its 'connection', 'connect' and 'request' events identifiers.

Is there a reason for that? Isn’t there any better way to achieve that (like just using an object for instance)?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Is there a reason for that?

ECMAScript does not have enumerations. To model them, one would have to use one of the available types, namely numbers, strings or objects (or symbols, though they’re younger than NodeJS). Now "magic strings" are far better than "magic numbers" or "magic empty objects".

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading