All Questions
Tagged with javascript coding-style
45 questions
1
vote
1
answer
259
views
Is module scoped initialisation considered a bad practice?
A module app.js includes another module service.js. The service.js module/file contains some functions but also does some module scoped initialisations(registry variable).
// service.js
const ...
0
votes
2
answers
759
views
Should I use unnecessary function for readability sake
I am implementing if/else statement, by using function "inCase" to make it more readable:
const size = 'small'
const equals = str1 => str2 => str2 === str1
const inCase = (obj) => ...
0
votes
2
answers
7k
views
Defining default values for Boolean arguments in JavaScript
Is it usually recommended to define default values for Boolean arguments?
I mean, is it usually recommended to define a function like this
someFunction(a, b, x) {
// a and b are strings, x is true ...
0
votes
2
answers
257
views
Code style to keep track of nested objects and data types?
In untyped languages (Python, Javascript specifically) find myself making a lot of bugs / wasting a lot of time because I forget what's in the objects I'm passing around. For example, I forget things ...
2
votes
1
answer
2k
views
How should I define hardcoded strings with some variable parts? Reuse more characters? Or keep the whole sentence?
for example, sometimes I need to define a hardcoded string with some variable parts, I often have trouble to choose the style:
style 1 : reuse every characters when possible
showMessage(num){
let ...
2
votes
2
answers
509
views
Should I use Array or Set if both can be used to finish my task?
for example, Suppose I have a 2d array:
let filterArr=[
[1,1,0,1,1],
[0,1,1,1,0],
[1,1,0,1,0]
];
I want to find and store the index of column that all are 1, i.e.:position 1 and 3, and the ...
1
vote
2
answers
186
views
HTML and JS code structure
I am relatively new to HTML/JS and am very much an amateur programmer. I have created a web app that works but I can't help but feel that the code is a spaghetti mess! I have been looking for JS/HTML ...
0
votes
3
answers
4k
views
Which is more readable: early returns, or conditionals? [duplicate]
I’m writing an asynchronous, Promise-returning function. In the processing I test some conditions, and if the conditions pass, the promise should be fulfilled (resolved), but if one fails, then the ...
0
votes
1
answer
891
views
Is it okay to use var on purpose in ES6, as opposed to let?
I was really glad that ES6 introduced the let keyword for defining variables. var scoping rules can lead to all kinds of issues, especially when working with loops and event handlers. Even when ...
31
votes
9
answers
18k
views
Does comparing equality of float numbers mislead junior developers even if no rounding error occurs in my case?
For example, I want to show a list of buttons from 0,0.5,... 5, which jumps for each 0.5. I use a for loop to do that, and have different color at button STANDARD_LINE:
var MAX=5.0;
var DIFF=0.5
var ...
1
vote
2
answers
463
views
Declaring a function without named parameters that accepts at least 1 argument
Say I have a function whose first parameter is used differently depending on how many arguments are passed. In this case, it is easier to just process the arguments object as a whole within the ...
3
votes
1
answer
4k
views
Why tabs are evil in ES6? [closed]
As I have recently started using ES6 in production, I was going through an ES6 style guide (having more than 350 stars on GitHub). This guide mentions at least three times that "Tabs are evil. Don't ...
1
vote
3
answers
188
views
Should conditionals be embedded in the function whose execution is contingent on them?
I have a large data structure that is about to be persisted to the database. Before that can happen I have to validate it and then update a bunch of it's properties based on various specific ...
6
votes
1
answer
886
views
What are the benefits of encapsulating conditionals (in functions)?
According to this clean-code guide you should encapsulate conditionals:
function shouldShowSpinner() {
return fsm.state === 'fetching' && isEmpty(listNode);
}
if (shouldShowSpinner()) {
/...
5
votes
3
answers
503
views
Functions that contain single statement?
Is it OK to have functions that have single statements?
I usually make functions that have single statements. I believe these increases code readability and i am able to write code faster as they ...