开发者

How to avoid duplicating a formula in both Javascript and PHP?

I'm currently writing a web application in HTML5/Javascript and PHP.

Some employees of the company will need to use it to enter their work schedule. The application will calculate in real time (using Javascript) some complicated legal infos and display the result at the bottom of the page. When he's done, the user clicks the "save" button and everything is sent to the database.

The problem is that I need both the user to see the output in real time and the managers to get the same output from what was saved in the database. I also need to support the case where the user has Javascript disabled. In other words, I need to do the same calculus in both Javascript and PHP.

To make things more complicated, the formula is very complex and will change often (every month or so), so I'd like to avoid maintaining two different versions. Testing one will already be hard enough.

Also I'd like to avoid AJAX to ask the server for the output, because:

  • the user will often build its schedule according to t开发者_开发问答he result of the real-time calculation, so even a 1-2 seconds lag could really be annoying for him
  • if possible I have to support HTML5's offline features, so the user can load the app on his mobile phone, fill its schedule while offline, and then upload it when online

For the moment the only solution I found is to write the formula in a language-agnostic way, then use some way to turn it into PHP code and JavaScript code, but that's not simple.


It sounds like you have essentially two approaches:

  1. You can write a language-independent formula (in a database, or config file, since it changes so frequently) and two code generators (Javascript, PHP). This is actually less scary than it sounds, as long as you pick your input format sanely and you actually mean a formula, not some arbitrary computation.

  2. You can use server-side JavaScript, and write the formula once in JavaScript. Wikipedia has a comparrison of server-side JavaScript implementations. There are even JavaScript interpreters written in PHP, including at least phpjs and J4P5.


Persisting data with language agnostic storage techniques and interpretative conversions are not the first thoughts that come to mind when the term "real-time" is mentioned. It is very possible, but usually much more challenging and time consuming to design infinitely scalable and easily maintainable software.

Personally, I weight the cost versus benefit associated with every known solution and decide on an approach that, first and foremost, benefits the end-user and meets or exceeds key objective deliverables. Once I've made a decision that satisfies major stakeholder's expectations, it is then my job to design and develop the most scalable and maintainable solution as possible within the confines of these expectations.

Based on the details you've provided I would absolutely write the equations using native JavaScript and PHP for optimal performance. If the software specifications literally (not figuratively) denote the necessity for real-time communications, I would then provide this by implementing WebSockets, otherwise long pulling would suffice.

Please note: Not all Web browsers support WebSockets, which may require end-users to conform to a supported Web browser. It is possible to avoid this conundrum by employing a seamless client-side failover (or altogether) by using something like Socket.IO.


This morning I got the idea to try XSLT for the calculation. This is really interesting, because it is supported by both Javascript and PHP, it's something reliable (contrary to a code generator I would have to write myself), and appropriate because the input data was already in XML.

Example input data :

<schedule>
  <element start="2011-19-09 08:00:00" end="2011-19-09 17:00:00" type="work" />
</schedule>

Example XSLT (written by heart, may not work) :

<xsl:stylesheet>
  <xsl:template match="/">
     <output>
       <out name="totalWorkHour"><xsl:value-of select="sum(/element[@type='work'](@end-@start))" /></out>
     </output>
  </xsl:template>
</xsl:stylesheet>

I'll try to code everything with XSLT, and I'll keep this solution if I manage to do that.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜