Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
What should I use for configuration ?
Some module like KrakenJS highly supports configuration through JSON and the blog https://blog.risingstack.com/node-js-best-practices-part-2/ say JS should be preferred over JSON.
Can you tell me how they differ and what's the best way to manage them ?
You should accept both.
JavaScript configuration files have many advantages:
programmable of course, which allows for interesting extensions and for generated parts
lighter and less awkward to type (less quotes, trailing commas, etc.)
lets you have comments (a big limitation of JSON, here's why they're needed)
more value types (NaN & infinites, regular expressions for example)
In this case the JS file exports a plain JS object, similar to what you would have had as a result of a parsed JSON file.
JSON is more socially accepted, because JS isn't often thought as a configuration format. Letting people use JSON will prevent a "JS is for logic" sterile debate and there's no problem if people are happy with it.
Accepting both formats:
Here's how reading the JS/JSON configuration can be done:
try {
config = require('./config.js');
} catch(err) {
if (err.code==='MODULE_NOT_FOUND') {
config = require('./config.json');
} else {
console.error('config.js loading failed');
throw err;
}
}
In any case, the JSON configuration file can be turned to a JS one by just prefixing it with
module.exports =
so nobody's locked in that format.
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 months ago.
Improve this question
The situation comes up when converting a Javascript project to Typescript that there is an enormous amount of Type errors in the project. Looking for techniques to handle this enormous tech debt on a mature project where going back and writing all the types is an impossible task to do all at once.
To be clear, this is not a question about how to migrate a project technically, but how to handle the enormous load of the migration while being able to use the type checking on new features. For example, one idea I had was to take a sort of snapshot of the errors at the beginning, and then suppress those errors, but show new errors from new features.
You can use the #ts-check and #ts-nocheck directives to control whether or not the compiler will typecheck individual modules.
You can skip checking some files by adding a // #ts-nocheck comment to files.
There are also directives for suppressing compiler errors on individual lines:
TypeScript may offer you errors which you disagree with, in those cases you can ignore errors on specific lines by adding // #ts-ignore or // #ts-expect-error on the preceding line.
For more information, there's an entire section of the TypeScript handbook dedicated to this topic: Migrating from JavaScript.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
This sounds really stupid to me, but I don't really have an answer for this.
What's the point of using ES6 modules or CommonJS (in browsers using browserifiy), if you can just connect multiple js files to the html via the script tag so they act as modules (sharing the same scope)?
In decent sized web applications, you have to take in count that more than one developer is working on the project, so separation of concerns is one of the key elements to develop a maintainable application, lets pains the next fake scenario:
You have a file within your web application called library.js.
ES2015 module syntax
export function calculatesquareArea(object) {
// code that calculates area
}
export function calculateVolume(object){
// code that calculates volume
}
And now we import this module into our code by doing something like this:
import { calculatesquareArea } from 'library'
// We log the output of the execution of calculatesquareArea
console.log( calculateSquareArea(object))
You can notice right away that in my module I have 2 functions but I've decided to only import one because at that time I've only needed to use that. Maybe this is a silly example but you can picture code reuse with the modularity nature of CommonJS or ES6 modules.
There is a more elaborate article on the key differences between CommonJS and ES2015 modules here
Hope this helped a little bit.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
This is more of a question regarding the architecture of an application, let's say you have a module something.js with some kind of special configuration constants mind you that these constants are hardcoded they are not imported from env or some file.
Would it be better to put everything related to this module as a global constant or is it better to import all configuration constants from config.js for example.
I'm interested in knowing the pros and cons from an architecture point of view.
If it's something high-level or broad, like a debug option that turns on debug output across your app, I would be inclined to put it in a common config file. On the other hand, if it's something specific or narrowly scoped, like how many characters a particular piece of text can be before it's truncated, I would be inclined to keep it in the only file that references it.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
In Python underscores are used for variables:
some_long_var, my_name, first_name, etc.
In JavaScript camelcase is preferred:
someLongVar, myName, firstName, etc.
All is fine and dandy until the two technologies start interacting:
JS Ajax Call:
$.get('url', {foo_bar: fooBar, bar_baz: barBaz}).done(function (data) {
console.log(data.computed_result);
};
Python view:
def url():
foo_bar = request.GET.get('foo_bar')
bar_baz = request.GET.get('bar_baz')
return jsonify(computed_result=foo_bar + bar_baz)
Do I use the Python or the JS style within the data JSON object sent to the server?
What about the JSON response?
Pick one and be consistent. I write Python a lot, therefore I would choose the Python style, which is concerned with readability for non-native-English speakers. However, reasonable people could disagree. Build concensus on your team, and take what you can best agree to be the best course of action.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
Is there any such thing as the "standard" convention for JSON indentation level? Should it be 2 spaces, 3 spaces, 4 spaces, tabs delimited, or something else?
I tried to come across the official JSON site, but it is not stated there.
JSON is a serialization format, not a presentation format.
As such, there is no "standard" indentation - JSON is typically sent as compactly as possible.
(That said, there is an option to JSON.stringify() to request "pretty printed" JSON - look at the space parameter at the MDN documentation)
There is no standard. The JSON specification permits any number of whitespaces.
However, when you are pretty-printing JSON to make it readable (e.g. in config files) it is good practise to be consistent with the coding conventions of your project and use the same indendation level as you would for an JS object literal - which is often 4 (Crockford) or 2 spaces (Node.js).