CrewLink icon indicating copy to clipboard operation
CrewLink copied to clipboard

Replace hard-coded type strings with constant Enums for memoryjs

Open ewpratten opened this issue 4 years ago • 1 comments

This PR makes no functional change, but replaces hard-coded strings with enums when specifying a data type to be read through the memory.js library.

Example: 'pointer' -> DataType.POINTER

This both keeps the code (in my opinion) more reader-friendly, and more closely matches the intended usage of memory.js.

The DataType enum is defined in memoryjs.d.ts, and is a typed copy of the constants defined here.

I also switched all references to the ptr datatype to pointer, since they are functionally the same

ewpratten avatar Dec 01 '20 18:12 ewpratten

LGTM, good improvement for future maintenance.

For what it's worth, the definition file ALREADY had restraints on what 'magic strings' would be accepted by readMemory, so on the surface it's a small readability change (with possible side effects I havn't tested) Yes using enums would be more familiar to people who don't use TypeScript on the regular, but I'm not 100% sure on the mechanics of defining an enum inside the definition file, as noted in my review.

Also be aware that Memory.JS already exports a series of constants, they are just unfortunately not grouped nicely into a datatype.

https://github.com/Rob--/memoryjs/blob/master/index.js#L6

So if you really wanted to use hard-coded constants instead of raw strings, even though the type definitions had those strings already in the type, I'd suggest using MemoryJS.PTR etc, and leaving the type definition as-is.

ryantheleach avatar Dec 08 '20 06:12 ryantheleach