Reconstant: Share constant definitions between programming languages
github.com
github.com
const char X[] = "This string cannot be reassigned.";
instead of const char *X = "This string CAN be reassigned";
In the second case, you can do X = "Another value";
X wouldn't be constant, but only the value it points to. Also, it costs sizeof(char *) more memory than the actual string contents.I can find data showing that adding the second const is uncommon. Here are the number of code results for different github searches:
"const char " - 310 million results
"const char" - 385 million results
"const char * const" - 30 million results
"const char* const" - 37 million results
"const char *const" - 36 million results
Unfortunately I'm not sure how to search for the array style syntax.
One way or another, my point is that I'm more interested in optimizing for readablity and the way people expect things to be defined rather than the extra bytes.
I would rather see the change suggested as an opportunity to educate the developers.
In other words, the danger of "now you have two problems" should be considered. http://regex.info/blog/2006-09-15/247
https://www.typescriptlang.org/docs/handbook/enums.html
Edit: from the image it looks like you're using some non standard formatting for the JS output, using Prettier may be more standard - https://prettier.io
I haven’t run into a scenario where I would use something like this before
The short version is that this is for cases when you write frontend and backend code in different languages and they need to share constants. E.g. you might have an API in your backend called ExportData() which takes a parameter called ExportType that can be JSON_OUTPUT, TEXT_OUTPUT, CSV_OUTPUT, etc and you want to re-use those constants between the frontend and backend because they are often transmitted between the two.
The long version is that I developed an application that runs on Kubernetes and can profile any Python application running in your cluster with no prior setup. (Useful if you want to debug a performance issue in an already running application.) My application has a Javascript frontend written in Vue.js and a Python backend written using the FastAPI framework. A user clicks a button in the frontend and chooses which process they want to profile and what type of profile it should be. (E.g. a regular flamegraph profile, a text file containing all the stack dumps, etc.) Then Vue initiates an API call to the backend. The backend checks what type of profile was requested and performs it for the client. I wanted to share the ProfileType enum between the frontend and backend. At first I just duplicated the constants by hand in Javascript and Python but I kept changing them as I added new features and got tired of syncing them by hand.
Hope this helps explain the use.
But a common, well-defined source language (here: yaml) should make it easy to add new backends and to adhere to language quirks (e.g., number literal formats)