Avoiding writing the same algorithm in several different languages

I am a web developer, and I noticed that many times I need the same function on both the client and the server. Therefore, I write it in JS as well as in PHP or any other server-side language. I'm tired of it. If I need to change it, I need to change it in both places. If I want to use it for some hand-held devices, I will have to rewrite this code again using objective-C or Java, etc. Then, if I need to change this function, then I will need to change it everywhere.

Is there a solution for this? If I call some web service through ajax, the client will have a delay. If it will be in JS, then it cannot be accessed from PHP or Java, etc. If I use some service in PHP from another language, this can also become a performance issue.

It is also possible that for some time we need functions derived from some parameters as input using db or without db.

I know that it would be a fairly simple solution, but I do not know about it. Please tell me some kind of language-independent solution, since I do not have VPS.

I am not sure if my question really belongs to stackoverflow.com or programers.stackexchange.com, so please transfer it to programers.stackexchange.com, and do not close this question if it belongs.

+7
source share
2 answers

Typically, the solution to this problem is to write common code in one language and use translators or a library to allow access from other languages.

Node.js allows you to write server-side code in JavaScript.

Node.js is a JavaScript-based runtime platform for quickly creating fast, scalable network applications. Node.js uses an event-driven, non-blocking I / O model that makes it lightweight and efficient, ideal for real-time data intensive applications that work across distributed devices.

You can also use JavaScript to write HTML5 mobile apps.

Building iPhone apps using HTML, CSS, and JavaScript

Web designers and developers can now join the iPhone application group without learning the Cocoa Objective-C programming language. It’s true: you can quickly and efficiently create iPhone applications using your existing skills with HTML, CSS and JavaScript. This book shows how with the help of many detailed examples, step-by-step instructions, and practical exercises.


If you do not want to try to write large complex applications in JavaScript, GWT provides a way to write Java and end-to-end translation, run it on the client.

The GWT SDK contains Java API libraries, a compiler, and a development server. It allows you to write client-side applications in Java and deploy them as JavaScript.


If you are developing .NET languages: C # -> JavaScript ScriptSharp

Script # is a free tool that allows developers to create C # source code and then compile it into a regular script that works in all modern browsers

+3
source

you can use spidermonkey extension to translate php to javascript. that way you can write your functions in php and then just convert them to javascript and reuse them in the browser.

here is a good tutorial to show you how to do it

+1
source

All Articles