All Questions
Tagged with javascript coding-style
44
questions
0
votes
2
answers
481
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
1k
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
199
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
715
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
358
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
180
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
1k
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
828
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
8
answers
17k
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
291
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
162
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 ...
5
votes
1
answer
639
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
358
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 ...
1
vote
5
answers
3k
views
Where to declare a variable and define a function in Javascript?
I'm reading the book JavaScript: The Good Parts.
On page 113 it recommends function expressions instead of function statements, because statements are subject to hoisting:
The statement:
function ...
3
votes
1
answer
210
views
Are there any scenarios where a JS function expressions would need to refer to its "name"?
Anonymous function expressions in JS bug me as they make stack traces and Chrome Dev Tools profiler output harder to use, so I've decided to name ALL my function expressions from here on in. The ...
2
votes
2
answers
139
views
It it a better to have the expected return at the beginning or at the end of a function?
This is a function that searches child nodes recursively until it finds one that is a text:
_.findPrevText = (node) => {
if (node.nodeType === 3) return node
return _.findPrevText(node....
2
votes
2
answers
1k
views
TypeScript/ES2015: Prefer `const` instead of `let` reduces readability?
ES2015 introduced the let and const keywords, which essentially have the same semantics apart from reassignment. (const can be seen as a final variable in languages like Java.)
I see the point of ...
6
votes
3
answers
3k
views
Is break a code smell?
I'm asking in terms of a loop, obviously break is important in switch statements. Whether or not switch statements themselves are code smells is a separate issue.
So consider the following use cases ...
2
votes
1
answer
488
views
Javascript Closure Style Similar to Java Class Structure
PROBLEM: There is a coding imperative (S. McConnel, Code Complete) that one shouldn't code on language, but by means of it, e.g. doing right style things even if language doesn't have some ...
2
votes
3
answers
230
views
Angular JS style guide when declaring variables
I'm new to Angular JS and working on a project with "legacy" Angular JS code. The code is about a year or half a year old. I often see stuff like this:
var _link;
_link = "http://localhost";
Or:
...
0
votes
3
answers
172
views
Eliminating the bad cases in if to get a nicer code
I have read this link Should I return from a function early or use an if statement? and it triggered a conflict in my head. I agree that it looks nicer and cleaner and I guess that would be the way I ...
1
vote
3
answers
267
views
What is the most readable way of passing arguments to the function? [closed]
I'm using JavaScript but the question can be generalized to all the languages.
In a nutshell - I'm checking if a browser connecting to my site is a microwave and cater for that accordingly.
What ...
29
votes
1
answer
1k
views
What is the term for an anonymous JavaScript function that's called immediately?
I'm writing a JavaScript style-guide for my team, so that we can organise and contribute our documents easier. But I've hit a small bump which is where my question applies...
What am I supposed to ...
1
vote
1
answer
504
views
Is writing code chronologically the best way for readability? [closed]
So I've been writing a lot of JavaScript lately. Despite what many people say I think JavaScript can be a simple and beautiful language.
What I've pondered of late is how to structure the code so it ...
1
vote
1
answer
73
views
Best place to declare functions in this example
Suppose I have code that accepts html GET requests:
server.get('path/to/some/endpoint', function(req,res) {
function a() {
//function goes here
}
}
Is it better to declare functions (...
0
votes
3
answers
388
views
What arguments are there to use a coding style for each distinct language? [duplicate]
I recently had a discussion about our coding style for C# projects. Two things in particular were very hard to agree upon.
Method Naming
C# has the de-facto standard of naming (at least public, not ...
8
votes
1
answer
324
views
Why create a Global-ish Object.create function?
I'm a fairly experienced programmer in the .NET and Java realms, and I've started reading up on JavaScript. I bought Douglas Crockford's "The Good Parts" book, and I'm immediately put off by a few ...
2
votes
1
answer
1k
views
Checking for valid state inside function or outside [closed]
This is a common occurrence in programming and is language agnostic, you have a function that needs to do something but in only in some cases. Maybe it's a feature-toggle, maybe it's a function that ...
5
votes
1
answer
2k
views
Why is the use of JavaScript in HREF attributes discouraged?
Disclaimer: I came to Programmers.SE to ask this question because I understand this is the place to ask this type of question, and not necessarily stackoverflow. If I am wrong, please close the ...
-3
votes
1
answer
450
views
Why is the JavaScript-language different in different programs/sites? [closed]
I'm kind of new to programming and i have a question that's been bothering me for awhile.
Why is the JavaScript-language different in different programs/sites.
I've used Codecademy to practice and i'...
1
vote
1
answer
175
views
Object Chain through an Interface
Say I have 3+ types of objects:
function Sea() {
var logs = [Logs];
this.getLog = function(ind){return logs[ind]}
}
function Log() {
var bumps = [Bumps];
this.getBump = function(ind){...
4
votes
4
answers
2k
views
Should our team order Javascript class methods/properties consistently? If so, how?
As our team is growing I've noticed that different developers put their class methods in different orders. For instance:
var Foo = Backbone.Model.extend({
someVar: {},
initialize: function()...
3
votes
1
answer
153
views
equivalence in callback and non-callback javascript
I'm pretty sure that the following two snippets are equivalent but I wanted to double check:
CALLBACK STYLE
function foo(input, callback) {
// do stuff with input
callback();
}
function ...
17
votes
2
answers
2k
views
Creating nested functions for purely aesthetic reasons?
I've always wondered what other programmers think about the idea of creating pure aesthetic functions.
Say I have a function that processes a chunk of data: Function ProcessBigData. Say I need ...
0
votes
3
answers
319
views
Javascript Naming Convention [closed]
I've been using Javascript for quite a while, but I've always ignored the common naming conventions that I see online (camelCase). I know it all depends on the developer's preference, but why camel ...
1
vote
4
answers
4k
views
Switching from ActionScript to JavaScript, tips for writing code?
I am quite comfortable with using actionscript3 and flash. I also have some experience with Java. But recently, I started learning JavaScript and node.js. I ultimately want to make a 3d game in ...
37
votes
15
answers
5k
views
Should I continue my self-taught coding practice or learn how to do coding professionally? [closed]
Lately I've been getting professional work, hanging out with other programmers, and making friends in the industry. The only thing is I'm 100% self-taught. It's caused my style to extremely deviate ...
11
votes
4
answers
3k
views
How do you keep code with continuations/callbacks readable?
Summary: Are there some well-established best-practice patterns that I can follow to keep my code readable in spite of using asynchronous code and callbacks?
I'm using a JavaScript library that does ...
112
votes
13
answers
37k
views
Why the recent shift to removing/omitting semicolons from Javascript?
It seems to be fashionable recently to omit semicolons from Javascript. There was a blog post a few years ago emphasising that in Javascript, semicolons are optional and the gist of the post seemed to ...
12
votes
3
answers
2k
views
Should I nest functions in languages that allow me to do that or should I rather avoid it?
In JavaScript, PL/SQL and some other languages, functions can be nested, i.e. declared within another function. This could be used to break a large function into smaller pieces, but keep those pieces ...
6
votes
6
answers
2k
views
Returning an object or false in dynamic languages
Consider the following JavaScript function:
function Foo() {
function getPreferences() {
if ([there is an existing preferences object]) {
return preferences;
}
return false;
}
}
...
0
votes
6
answers
8k
views
Mixing jQuery with JavaScript
I just asked this question at SO about some bugginess I am experiencing with jQuery and IE8, and someone commented that he was appalled that I would mix JavaScript with jQuery (he has since deleted ...
55
votes
10
answers
54k
views
Why Should I Avoid Inline Scripting?
A knowledgeable friend recently looked at a website I helped launch, and commented something like "very cool site, shame about the inline scripting in the source code".
I'm definitely in a position ...