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 3 years ago.
Improve this question
I'm using Typescript for a Node application and I love to fully use the power of Javascript with all the good stuff of Typescript. Line after line I took up the habit to chain functions, to use arrow functions, to user carrying and partial functions and so on.
What I really cannot understand is why the forEach function return void instead of something useful I can use to perform a chainable, just one-line and elegant piece of code.
My question is about the design considerations behind this choice that at first glance seems to be just annoying and the implications a return value can cause.
Because that's what forEach does. It's like a for loop; it isn't meant to evaluate to anything useful. It's intended to be used to carry out side effects using a function. If you need it for anything other than that purpose, it's the wrong tool for the job.
If you want a looping function that evaluates to something, use map or reduce instead. map will return a new list based on the old list, and reduce will return a reduced value from iterating over the list.
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 2 years ago.
Improve this question
I'm new to asynchronous programming and I would like to know if using await is considered a bad coding practice. I am asking this because it looks like it is possible to let the callback function do the waiting instead of letting the entire program suspend everythng until the rpocess complete.
Thank you and I appreciate your idea on this.
No, it’s not. It’s just an approach (very useful sometimes).
In modern systems complexity is so high, that in many cases having clear code is much better that having super effective code. Just an example: imagine the case when you need to make several async things one by one (maybe, fetch data based one previously fetched results). You can do it making a chain of several .then(). After that you’ll need to also add .catch(). And at this point you’ll find yourself writing spaghetti code which is a bit messy.
The other option is just make this async function with sequential calls and some logic between them. And this will look much better
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 2 years ago.
Improve this question
When removing a property from an object, I've used the delete operator in the past. But when I had my code reviewed by a person with a CS background, they wanted me to find another way of removing the property because it was an "expensive" task (not sure what that means). Since I'm self taught I wanted to ask if deleting in Js is something I should consider using as last resort? I've looked at other responses within SO like this but couldn't find anything that makes reference to this.
No, it's not considered bad practice. It just depends on what you want to do,
Read this thread:
Garbage Collection and JavaScript "delete": Is this overkill/obfuscation, or a good practice?
The book "JavaScript, the Good Parts" by Douglas CrockFord says...
The delete operator can be used to remove a property from an object. It will remove a property from the object if it has one. It will not touch any of the objects in the proto-
type linkage.
I think you can use the Delete operator without worries. There are many other ways to remove a property from an object, but all they are more expensive in my opinion.
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 3 years ago.
Improve this question
I've been looking on stack overflow and searching blog posts for info on this and can't seem to find a definitive answer.
It regards using if condition or a ternary to execute functions. Is there a "best practice" regarding which we should use?
Let's say we have 2 functions and that one of them will execute based on some boolean value.
We can write functionality like this using traditional if/else statements:
if (boolean) {
doThis()
} else {
doThat()
}
Alternatively, we can write something functionally equivalent like this:
boolean ? doThis() : doThat()
Generally, most ternaries I use and see from others are used for assignment, and not to execute functions.
Everything on stack overflow and blogposts that I've seen just state that, "Yes, that's a thing. We can execute functions in ternaries".
Nothing ever speaks to whether we should favor one over the other. Is there a best practice when it comes to which one you should choose?
It seems like it'd be completely fine to use one if the functionality you'd like to execute conditionally is just one function. If you have more then that it seems like if conditions should be favored for readability.
So, with all that context, here's the question:
Does it just depend on the situation? Or should we always favor if conditions for function calls and relegate ternaries strictly to assignment?
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
Although javascript is a weakly typed language, in some scenarios is needed to ensure the data type. Currently many node packages depend on helper functions, e.g, isNumber and isFunction.
It'd be interesting if there were built-in methods for that like Array.isArray. Something like Number.isNumber or Function.isFunction.
What do you guys think about this?
You should not worry much about such. There are already a lot of methods. For your query, you can use Number.isNaN for checking number and use typeof to know if the given variable is a function.
You can check the following answers:
Is there any function like IsNumeric in JavaScript to validate numbers?
How can I check if a javascript variable is function type?
Array is a collection. The collection might refer to [...] or {...} and this is why there's Array.isArray to check if the given collection is array or not. But there's no other way arround for differentiating the function and the number between themselves. And this is perfect reason the javascript should not have such function.
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
While working on a recent project, I began wondering when somebody may use JavaScript vs. PHP for a generic function. Take this basic function (in JS) as an example, which simply returns whether or not a number falls within a particular range:
function range(num, var1, var2) {
if ((num >= var1) && (num <= var2)) {
return true;
}
else {
return false;
}
}
For something that doesn't query a database, nor is it information that should be — or needs to be — indexed for SEO (I know by default JavaScript will not be indexed), then my inference would be that JavaScript would be sufficient. But at the same time, PHP could be as well.
Basically, if the ONLY point of the application were a simple function like above (not that I can see a reason for that, but I digress...), then which langauge would be better to write this in? JavaScript or PHP?
Would love any insight as to which would be the best method to use and why. I recognize there is no right or wrong answer necessarily, but would like to hear arguments for or against one over the other.
Thanks!
As you point out, there is no necessarily right or wrong answer.
I would say that it depends:
-Is it a problem for people to be able to reverse engineer the code?
-Is it a problem if it does not execute because JavaScript might be disabled?
-Is it preferable to have code execute client-side versus server-side from a performance point of view?
-Does the content generated by the output of this function qualify as something you might want indexed by Search Engines?
Depending on the importance of the above criteria/questions, JavaScript might be disqualified.
From the points above, the common thing seems to be that choosing for JavaScript is more likely to lead to potentially undesirable side-effects.
The safest bet, from what I theorize, is therefore the server-side language, PHP.