Microsoft MakeCode

Constants and JS/TS Style

While I have a specific question, I also am looking for some guidance. I trust this group, so I’m willing to open this can of worms here. :slight_smile:

I’m looking at different JavaScript style guides to implement in the second half of the course that I am writing. I’ve settled on the JavaScript Standard Style (https://github.com/standard/standard). It seems reasonable, widely-used, and fits the style that I’m accustomed to using as a C# programmer, aside from the typical Java quirks inherited from the Sun’s implementation of K&R style. It also specifically supports TypeScript code.

One thing that’s missing from JavaScript Standard Style is a convention for naming constants. I can draw from other style guides, of course, but I’m curious as to what y’all use. Do you follow a style guide for your JavaScript / TypeScript code and, if so, which? What convention do you follow when naming constants?

Since no one else responded; for TypeScript I typically use UPPER_CASE_WITH_UNDERSCORES for constants that are intended to be used like class constants in java / macros in c (e.g. here in the particle effects). If it is just a local variable that I’ve made a constant because there is no current reason for it to be mutable, I camelCase it to match other local variables (e.g. here).

1 Like

Awesome response; thanks, @jwunderl! I like the distinction between global/class constants and local constants. Thanks, too, for the examples! Much appreciated!

We have these naming conventions, mostly for extension writers: https://makecode.com/extensions/naming-conventions . Probably should add a section on constants.

1 Like

Thanks, @peli! I’ve looked at the main PXT docs many times as I write my own extensions, and somehow I missed that one.