User defined Code Generate... will be needed!

In my current project, I need to write following code:

The idNo and principalName are both the variables defined in PersonCompanyItem class. Although I defined some live template to implement it one by one, but when the number is large, it'll still very boring. So I hope we can defined our own generate template and that can make a batch job for selected variables. Hope this will avaiable in the following version. Thanks for your hard working. :)

3 comments
Comment actions Permalink

Of course, you could just stuff them all into a HashMap, and iterate
over that. Like:

Map map = new HashMap();
map.put("idNo", personCompanyItem.getIdNo());
map.put("principalName", personCompanyItem.getPrincipalName());
// put rest of items here...

for (Iterator aIterator = map.keySet().iterator();
aIterator.hasNext();)
{
String key = (String) aIterator.next();
String value = (String) map.get(key);
if (value != null)
pageContext.setAttribute(key, value.trim());
else
pageContext.setAttribute(key, "");
}


--
Alex Dommasch


shenyuan wrote:

In my current project, I need to write following code:

             String idNo = personCompanyItem.getIdNo();
>             if (idNo != null)
>                 pageContext.setAttribute("idNo", idNo.trim());
>             else
>                 pageContext.setAttribute("idNo", "");
> 
>             String principalName = personCompanyItem.getPrincipalName();
>             if (principalName != null)
>                 pageContext.setAttribute("principalName", principalName.trim());
>             else
>                 pageContext.setAttribute("principalName", "");
> ]]>

The idNo and principalName are both the variables defined in PersonCompanyItem class. Although I defined some live template to implement it one by one, but when the number is large, it'll still very boring. So I hope we can defined our own generate template and that can make a batch job for selected variables. Hope this will avaiable in the following version. Thanks for your hard working. :)



0
Comment actions Permalink

before you try any solution you should first refactor your code with idea :)

by applying "extract method" and "inline / local var", you can have something like:


personally i think that's enough. if you are considering the hashtable solution, please think it over. in alex's example, you still can not avoid a whole bunch of map.put() operations, unless you convert your PersonCompanyItem class to use all string-based name-value pairs:


thus later you might use personCompanyItem.getAttributeKeys() with a loop to populate page context.

personally i don't think it's good solution because that is throwing away java's strong-typed syntax and going back to weak-typed languages like VB...

in my current working project, there are lots of "hashtable"s and they caused lots of problems. programmers try to save time by implementing all their object attributes as string-based key-value pairs instead of getXXX()/setXXX(), losing advantages of java's type/syntax checking. and it did cause lots of weird problems. mis-spelt key name is one of them, since string property names are not checked during compile. can you quickly tell what's wrong from the following code? it successfully passes compilation but fails to work correctly:


i believe most of us will be quick to figure out such an error. but how about if such an error is mixed among lots of similar things?

someone will say "define string constants". so we can have:


sure it's better because we are taking advantages of java syntax checking again. but wait, what's the big difference between writing 50 "public static final String KEY_XXX = "xxx";" and writing 50 "setPageContextValue("xxx", personCompanyItem.getXXX(), pageContext);" ? in the former, lazy programmers can still use new attribute names directly as a string instead of defining a constant in Attributes class first. in the latter, they are forced to explicitly define their own getYYY() method in PersonCompanyItem.

thus, if i have not too much values to assign, i would rather write them one by one and give each of the attributes its own get/set method. if number of values increases to 100 or more, i might consider writing a code-generation tool to generate that code for me (the work is much easier than we might expect). but i will try my best effort to avoid duplicated code. in most time, you'll surprisingly find that the code becomes dramatically clear by applying "extract method" appropriately.

just my personal opinion. sorry i might have brought the topic too far from the original point... anyway, even if the "code-generator" i mentioned above is easy to write, it will definetely be a great idea to have idea to do it for us. also, hashtable and string are useful tools for data-struct such as attributes. but not for predictable key set.

0
Comment actions Permalink

Hi Samson,
So thanks for your good ideas. Yes, during my waiting time, I do write some code-generation programme to 'write' code for me (with data type checking). Now I feel that sometimes creating some tools by ourself is more convenient than just waiting.

0

Please sign in to leave a comment.