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
What is better to use in javascript to square some value.
Math.pow
Math.pow((circle2.x - circle1.x), 2)
Or my own function
square(circle2.x - circle1.x);
function square(a){
return a*a;
}
It is nearly always better to use a library then write your own code, unless you have a good reason not to. Reasons why:
It saves you work.
It is well-tested code, while your code might introduce bugs.
Someone else who looks at your code will know what Math.pow is, so it makes your code easier to read.
Obviously this is a very simple case, in which your own function is unlikely to cause a problem. Still, it is better to get in the habit of using libraries whenever possible.
By the way, Math.pow performs a lot of handling of special cases. This illustrates that even a simple function can have more pitfalls than it appears at first. By using a library, you don't have to worry about handling all of the edge cases yourself.
You should always prefer to use library functions - what is the point in reinventing the wheel?
Library functions are optimized and may cater for some corner cases you are not aware of...
Are you going to provide own implementation of multiplication as well?
For a power 2 specifically I would use multiplication because you really don't lose any legibility. Even more if speed is important, multiplying will probably be faster than function calls.
edit: I mean don't even create a function... just a*a.
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 6 years ago.
Improve this question
I see most code-bases using bracketed if-statements in all cases including cases in which only one statement needs to be executed after the if.
Are there any clear benefits to always using bracketed if-statements or has it just become a standard over time?
What is the general consensus on this, would it be a big mistake to allow one-line ifs through our linter?
Largely this comes down to developer preference, although many reasons are given for using the curly braces on every control block.
First, it makes the control block more readable. Consider:
if (some_condition)
runSomeFunction();
runSomeOtherFunction();
Since indentation is not respected in most curly brace languages, this will work, but it really reduces the readability (only runSomeFunction() will happen in the control block). Compare to:
if (some_condition) {
runSomeFunction();
}
runSomeOtherFunction();
Second, when you need to add something to the control block (which almost invariably happens more often than not), adding the curly's can be frustrating or easily forgotten leading to issues like the above.
Still, those largely come down to preference and you can always find exceptions (like if (some_condition) runSomeFunction(); which is much more readable than the first example above while still accomplishing the same goal in a much more concise format that retains readability).
If you have to go back to the code to add something, you might forget that you didn't open brackets, and your code wouldn't work if you exceed one line.
Other than that it's a matter of preference and format.
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
I'll keep my question short. I've read this question about a delay function.
How to wait 5 seconds with jQuery?
and it struck me that people wanted to know about alternative ways to achieve this using JQuery.
In my understanding JQuery is nothing but a JS library. In many cases to introduce new functionality.
Now why would someone use JQuery if an original JS function is available and not even too complex?
Bonus question: Can JS functions be called from within JQuery? (this one, if negative might answer my first one, actually...)
In my understanding JQuery is nothing but a JS library. In many cases to introduce new functionality.
Yes, that is correct. In fact, your astute observation that people want to use jQuery where JS will suffice likely stems from a lack of understanding to this point!
Now why would someone use JQuery if an original JS function is available and not even too complex?
Someone may wish to use jQuery for certain functions if the JS-only equivalent is not well supported in all browsers.
Bonus question: Can JS functions be called from within JQuery? (this one, if negative might answer my first one, actually...)
Certainly! As you stated, jQuery is just a JavaScript library, and can be mixed with bits of plain JS without a problem.
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.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
which one is faster
document.getElementsByName('tempName')[0].value
or
$('[name="tempName"]')[0].value
I want to know because I'm writing js codes were I have to use it for around 10 thousand times on every change of a single field and I want to know its effect on performance.
The first one seems to be faster. Using pure javascript.
Tested with jsperf: http://jsperf.com/performance-fsdfsd
Pure JavaScript should work faster here. So this one is
document.getElementsByName('tempName')[0].value
The first one. No doubt about it. Not a single test needed for that.
However, the questions you should be asking yourself is:
By how much?
Does it really matter?
How big is your app that you worry about these things?
Is it worth sacrificing readability & cross-browser support for such a tiny gain?
If you can make do without jQuery as a dependency, then you can save yourself & your users some precious bytes. But, if you anyhow need jQuery elsewhere on the page, I don't think fiddling like this is gonna make a real-life difference.
Bottom line: you should test your real-world example, not just this one abstract line. The vanilla JavaScript might be faster, but if it makes no difference to your app, then why bother?