views:

50

answers:

3

Say you have a web application that has a lot of business rules built around if-then comparison of String constants. These are used on both client-side and backend code, and at the moment are hard-coded badly throughout both places with lots of duplication and sometimes errors. What's a sensible way to refactor them out? Unfortunately, in most cases they must exist as using polymorphism would be much more hideous. Anything specific for flex/java? Is there an easy way to keep all those constants in one place and compile it into both client and server code?

Edit: I'm really looking for a way to share string constants between flex and java with compile-time checking of everything. The only idea I can think of is a code generator.

A: 

Keep them into a project that is being distributed as a jar. You can also place there all the util classes to avoid code duplication.

thelost
Interesting, but doesn't help me to reuse them flex-side, right?
Gary
Sure it does! Simply use that library (containing your strings) within your flex project. Similar example: http://flexme.wordpress.com/2007/07/11/internationalization-in-flex/
thelost
ah, you're suggesting resource files again. Unfortunately, key value pairs doesn't help me, because I still have to know the name of the keys on flex and java :-). I just have a bunch of strings scattered around, they're not really values of something that can be called a key. My point is, at the moment I have hard-coded strings. If I do key-value pairs, I have hard-coded keys and twice as many strings as a result, added more fuel to the fire, so to speak. Indirection isn't really what I'm looking for here.
Gary
+1  A: 

Don't both Flex and Java both have support for simple .properties files where the contents of those files are of this form

key1=value 1
key2=value 2

and so on...

So why not define a common .properties file and share it between your two projects? Your version control system could help here as well. For instance, if you were using svn you could just put your .properties files in another repository and use svn:externals to share them between both your flex and java repository.

As for loading and using the properties, the following pages show how to do it in each language/platform:

For Java: http://www.mkyong.com/java/java-properties-file-examples/

For Flex: http://vatsalad.wordpress.com/2010/03/12/how-to-use-properties-fileresourcebundle-in-flex/

whaley
it creates kinda the same problem, though, right? This solves the syncing problem, but now I have to define constants in another class and load them up from the properties file. I'd still have to know the names of the properties as hard-coded string literals :-).
Gary
Yes, it solves the syncing problem and at least gets the *values* in one location. On the java side, you could deal with the compile time part of it by avoiding it completely with something like Spring's PropertyPlaceholderConfigurer and using IoC with the classes that need those values. I think Guice has something similar for properties. This pretty much gets all references to the property name/values out of your code and in to configuration and your code becomes ignorant that they exist. As for Flex, you'll have to rely on someone else as I claim Flex ignorance :D
whaley
+1  A: 

Code generation is easier than you think. If you really believe that's what you need to do, I could possibly provide an example (a custom Ant task although it can be run as a plain java 'main' app) that you could modify for your purposes (specifically for string constants) if you want.

If I understand correctly, you're looking for something which provides you with compile-time "key" checking (which would also give you auto-complete support)? So yes, a properties file wouldn't help much here.

Crusader
yes, that's exactly what I need.
Gary
Actually it turns out this code doesn't need modification at all, you just need to set up your Java constants as "public static final String" (and put the class in the right place). So your main challenge will be understanding how it works. It's commented but you may still need to take some time to understand it: http://www.megaupload.com/?d=8PEPWVZM Maybe I'll do a more 'formal' release of that some day if anyone finds it useful, I always figured there was better stuff out there already, but nobody is suggesting anything. Let me know if it helps at all...
Crusader