Whenever there is dilemma to do a particular task, which can be accomplished either by client side code or by using a server code, which should be preferred?
For example:
I can iterate through a javascript object and construct a string and then send it to server, or should i send the javascript object and process it in the server?
My views: whenever I come across this situation I use the client side code as it reduces the computation load on server.
What has to be done in such a situation? Which is the right approach?
(This answer assumes a web programming context)
I personally put a lot of front-end logic in Javascript running on the client side, because it makes things very responsive, even for people with a slow connection. This also means that some functionality continues working even if the user goes offline. I find some programmers use AJAX for things which don’t reallly require communication with the server at all.
When using this technique, if possible, I find it helpful to embed all the data which the front-end Javascript may need in the page, so it doesn’t have to go back and request more data from the server using an AJAX call. (In general, whenever data is being communicated over a high-latency channel, you want to send it in big chunks so the client doesn’t have to keep constantly coming back for more. The same applies to DB queries: you can often increase performance by pulling back all the data you might need with a single query, rather than making numerous fine-grained queries. Even if you don’t end up using all of that data, performance will still usually be better.)
One caution: unless you are very disciplined to keep things organized, Javascript which updates the UI by dynamically generating HTML in response to user actions can become very hairy. If the Javascript is also dynamically generated using another language (running on the server side), things can get downright frightening.
Testability is also important in any app. If you are using something like Selenium, testing a combination client/server web app may not be a problem, but if you aren’t, testing server-side code may be easier.
As others have noted, you must also make sure that you don’t compromise security. Don’t expect that people will only invoke your server-side code in the way you provide for. View all the types of requests your server handles as forming an “API” which people may invoke with any arguments and in any order.