patternjavascriptMinor
Cross-language constants
Viewed 0 times
crossconstantslanguage
Problem
I'm developing a web application that leverages multiple programming languages. The data flow resembles:
Using different languages makes it tempting to hard-code various constants within the different languages, which encourages inconsistencies. Without a single source, it is highly probable that "Your Name" will sometimes be "Your name" or "your name" or even "Username", depending on what language requires the value.
To avoid duplicating these values, a "configuration" table exists:
The configuration table is then converted to XML, with the references expanded as expected:
Once in XML, files for constants are generated for the appropriate syntax:
Since the constants are now defined in a single table, there is no more duplication.
Problem
I am concerned that the problem of multi-language constants has a technically better solution.
When a project uses multiple programming languages that tightly communicate, how would you prevent constant values from being repeated throughout different language-specific source files?
In other words, is there a better design for eliminating duplicate values across different programming languages?
Generate XML
The code to generate the XML resembles...
`CREATE OR REPLACE FUNCTION generate_configuration_xml()
RETURNS text AS
$BODY$
DECLARE
v_result TEXT DEFAULT '';
BEGIN
SELECT
xmlroot (
xmlelement( name "configuration",
xmlagg(
x
- Browser » PHP » PL/SQL » XML » XSLT » XHTML + JavaScript » Browser
Using different languages makes it tempting to hard-code various constants within the different languages, which encourages inconsistencies. Without a single source, it is highly probable that "Your Name" will sometimes be "Your name" or "your name" or even "Username", depending on what language requires the value.
To avoid duplicating these values, a "configuration" table exists:
| code | label | languages | data_type |
+-------------------+---------------------+------------+-----------+
| DEFAULT_USER_NAME | Your Name | xsl,js,php | string |
| BASE_IMAGE | ${BASE_APP}images/ | xsl,js | string |
| BASE_ICON | ${BASE_IMAGE}icons/ | xsl,js | string |
| BASE_APP | /app/ | xsl,js,php | string |
The configuration table is then converted to XML, with the references expanded as expected:
BASE_ICON = /app/images/icons/
Once in XML, files for constants are generated for the appropriate syntax:
- constants.php
- constants.xsl
- constants.js
Since the constants are now defined in a single table, there is no more duplication.
Problem
I am concerned that the problem of multi-language constants has a technically better solution.
When a project uses multiple programming languages that tightly communicate, how would you prevent constant values from being repeated throughout different language-specific source files?
In other words, is there a better design for eliminating duplicate values across different programming languages?
Generate XML
The code to generate the XML resembles...
`CREATE OR REPLACE FUNCTION generate_configuration_xml()
RETURNS text AS
$BODY$
DECLARE
v_result TEXT DEFAULT '';
BEGIN
SELECT
xmlroot (
xmlelement( name "configuration",
xmlagg(
x
Solution
I'm not familiar with PL/SQL so I'm unsure what exactly is being done with it. However, the commonly accepted solution for sharing between PHP and JavaScript is to use a JSON file. JSON is native to JS, its even in the name. PHP also supports JSON via its
You can import a JSON string with
And of course, if you wish to use them as actual constants, you can loop over the array and define them.
I believe this accomplishes the same thing you are currently doing, only without the extra files. The only downfall is that you wont be able to use those XML entities to declare shared elements, unless of course you end up generating the JSON from the XML. However, I think that might be just a bit much. At that point you should then just turn to some JS library to read the XML directly as you can already do with PHP. Hope this helps.
json_encode() and json_decode() functions. Here's an example of what the JSON file might look like:{
"BASE_IMAGE" : "/app/images/",
"BASE_ICON" : "/app/images/icons/",
"DEFAULT_USER_NAME" : "Your Name"
}You can import a JSON string with
json_decode(), which means you need to read the file into a string first, in order to get either a JSON object or a multidimensional array.//as an object
$constants = json_decode( $json );
echo $constants->BASE_IMAGE;// echos /app/images/
//as an array
$constants = json_decode( $json, TRUE );
echo $constants[ 'BASE_IMAGE' ];// echos /app/images/And of course, if you wish to use them as actual constants, you can loop over the array and define them.
foreach( $constants AS $constant => $value ) {
define( $constant, $value );
}I believe this accomplishes the same thing you are currently doing, only without the extra files. The only downfall is that you wont be able to use those XML entities to declare shared elements, unless of course you end up generating the JSON from the XML. However, I think that might be just a bit much. At that point you should then just turn to some JS library to read the XML directly as you can already do with PHP. Hope this helps.
Code Snippets
{
"BASE_IMAGE" : "/app/images/",
"BASE_ICON" : "/app/images/icons/",
"DEFAULT_USER_NAME" : "Your Name"
}//as an object
$constants = json_decode( $json );
echo $constants->BASE_IMAGE;// echos /app/images/
//as an array
$constants = json_decode( $json, TRUE );
echo $constants[ 'BASE_IMAGE' ];// echos /app/images/foreach( $constants AS $constant => $value ) {
define( $constant, $value );
}Context
StackExchange Code Review Q#19953, answer score: 6
Revisions (0)
No revisions yet.