Generic functions: JavaScript or PHP? [closed] - 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 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.

Related

Why forEach return void? [closed]

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.

Which way is better to call a jquery function twice? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have a javascript jquery function which I need to use twice, in 2 different files.
Now, the question is, which is the better way to implement it, performance-wise. Should I declare function global using 'window' variable, or should I declare the function in each of the files?
Please keep in mind that traffic is not an issue, the script will be used from the hard-drive.
Thanks for your help.
My answer follows the DRY Principle (Don't Repeat Yourself). If you put the function in both files, and later find a bug on Page #1, someone has to remember that the function is duplicated on Page #2 as well (in this case, assume the bug report says "Page #1 doesn't work properly"). From that bug report, would the developer know to also modify Page #2? To avoid the human error piece, I'd always recommend you don't copy/paste functions into multiple locations.
Performance wise, if you're not concerned about traffic, the difference is nanoseconds slower wrapping it in a shared function as you do have to create an additional stack frame for the shared function call that in turn calls jQuery, but we're really talking nanoseconds. For a few nanoseconds lost, I'd say DRY is the way to go.

Any reason for using JQuery over JS for simple tasks? [closed]

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.

When to use continue [closed]

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
I've been reading JavaScript: The good parts. Theres a section: The Bad Parts. This is straight from the book:
continue Statement
The continue statement jumps to the top of the loop. I have never seen a piece of code that was not improved by refactoring it to remove the continue statement.
So my question is, is there ever a situation continue is required?
There are strong opinions on both sides, wether continue is useful or not. But it is never required. You can always find a way around it, if you want.
There are some believe that if there is a continue in your loops, that it should probably be refactored or rewritten. There are legitimate uses of continue, but they can usually be avoided. To give an example:
for (var i = 0; i < particles.count(); ++i)
{
if (OutOfBounds(particles[i])) {
RemoveFromArray(particles[i]));
++i;
continue;
}
}
In this case, the continue is unnecessary because there is nothing else in the loop, but it would be if you were to do something else after the initial check.
Also, it helps with readability, if you have a complicated loop with nested conditionals. For example, you may have a bunch of extraneous variables like quitLoop1 and quitLoop2 and so on.
In my experience, the continue statement is useful if a condition in a loop is met, and you don't want to execute the rest of the code in the loop on that iteration. The reason why you'll get mixed opinions on its usefulness is that many will argue there are better ways to rewrite your code so you don't have to use it. In my opinion that argument is strictly for readability purposes.
I have never used continue in javascript because most of the javascript I write is simple DOM manipulation. However, I have used continue in various Python programs, and it can be useful (as mentioned above) to skip over the loop under certain conditions.

javascript squared [closed]

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.

Categories

Resources