7

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.

Mike Samuel
  • 109,453
  • 27
  • 204
  • 234
Hafiz
  • 3,847
  • 11
  • 54
  • 106
  • You could always create a web service to provide you this common functionality. The web service invocation code would differ, but the implementation of whatever you wanted common will remain common. Obviously this depends heavily on what you're doing with it, but it's possible. – doogle Mar 08 '12 at 03:05
  • It is unclear why you are writing the same functions on the client and server. How does that work, exactly, and why would you need to do that? – kennebec Mar 08 '12 at 04:41
  • @doogle I know it is common thing so it must be possible but in given scenario how can it work in such way that one can use in browser clientsite as well as in handheld devices, whether using java or c etc.,mean any suitable format? I think json is nearer? but still js will need to load it on runtime. – Hafiz Mar 08 '12 at 05:57
  • @kennebec There are some time some calculations that are some time require via JS (runtime calculations) and from server site require same things when getting data on refresh without js execution. This is sort of normal scenario in many projects. Not always calculation but different things some time but always this replication. – Hafiz Mar 08 '12 at 05:59
  • I think what I am looking for is a basic calculation and basic operation language, that should have encoder and decoder written in different languages and that transfers basic algorithms just like JSON transmits data but I think I will need to do this by myself :| – Hafiz Mar 08 '12 at 22:20
  • wow language/format transmitting algorithms instead of data, I think that would be good for my semester project :) – Hafiz Mar 08 '12 at 22:23

2 Answers2

3

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

Alexei Levenkov
  • 94,391
  • 12
  • 114
  • 159
Mike Samuel
  • 109,453
  • 27
  • 204
  • 234
  • but there are places when we need to write native apps, instead of using webview etc as some time apps have to use some features not provided in libraries which are donig HTML/CSS/JS to app. And there can be symbian or other handheld OS where JS don't work. Also node.js on server will need to be installed there and I don't think that shared hostings allow that. So isn't there some really cross language sort of solution like some webservice or format or combination of both, that can work at both side? – Hafiz Mar 08 '12 at 02:49
  • my clients normally have shared hosting where PHP is only available – Hafiz Mar 08 '12 at 02:51
  • is there some webservice using JSON that can do so? As JSON is independent format, that most of languages can encode/decode – Hafiz Mar 08 '12 at 02:52
  • 1
    @Hafiz, no, there is no programming language inter-lingua that runs on all platforms. – Mike Samuel Mar 08 '12 at 14:41
  • I understand, then maximum possible solution is to transmitting objects using JSON like thing having data but can't transmit function – Hafiz Mar 08 '12 at 22:12
  • So I think there should be basic calculating and basic operation language, that should have encoder and decoder written in different languages and that transfers basic algorithms just like JSON transmits data – Hafiz Mar 08 '12 at 22:18
1

you could use the spidermonkey extension to translate php into javascript. this way you can write your functions in php then simply convert them to javascript and re-use them at the browser.

here is a good tutorial to show you how this is done

mulllhausen
  • 3,712
  • 7
  • 43
  • 63