Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoiding writing the same algorithm in multiple different languages

I am a web developer, and I have observed that many times I need the same function on both client and server. So I write it in JS as well as in PHP or whichever server side language. I am fed up with this. If I have to change it then I need to change it in both places. If I want to use it for some hand held devices, then I will have to rewrite that code yet again using objective-C or Java etc. Then if I need to change that function then I will need to change it everywhere.

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

It is also possible that some time we need such functions output from some paramters as input using db or without db.

I know there would be some pretty simpler solution but I am not aware of that. Please tell some language independent solution as I don't have VPS always.

I am not sure if my question actually belongs to stackoverflow.com or programmers.stackexchange.com so please transfer it to programmers.stackexchange.com instead of closing this question if it belongs to there.

like image 471
Hafiz Avatar asked Mar 08 '12 02:03

Hafiz


1 Answers

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

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

Node.js is a platform built on Chrome's JavaScript runtime for easily building fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.

You can also use JavaScript to write HTML5 apps for mobile devices.

"Building iPhone Apps with HTML, CSS, and JavaScript"

Now web designers and developers can join the iPhone app party without having to learn Cocoa's Objective-C programming language. It's true: You can write iPhone apps quickly and efficiently using your existing skills with HTML, CSS, and JavaScript. This book shows you how with lots of detailed examples, step-by-step instructions, and hands-on exercises.


If you don't want to try to write large complex applications in JavaScript, GWT provides a way to write Java and via-translation, run it on the client.

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


If you develop in .Net languages: C# -> JavaScript ScriptSharp

Script# is a free tool that enables developers to author C# source code and subsequently compile it into regular script that works across all modern browsers

like image 73
Mike Samuel Avatar answered Oct 20 '22 15:10

Mike Samuel