velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Andy Lee <ag...@earthlink.net>
Subject RE: discarding method return values
Date Mon, 04 Nov 2002 16:10:25 GMT
At 10:16 AM -0500 11/4/02, Kevin Baynes wrote:
>  > At 9:26 AM -0500 11/4/02, bob mcwhirter wrote:
>>  >Sorry for being pedantic here, but if you're calling a method and
>>  >ignore its return value in a template, then you're just wanting the
>>  >side-effects of the method call?  I'd consider that to probably be a
>>  >Bad Programming Practice.
>>
>>  Arguably so, but IMO it depends on the situation.  In the
>>  examples I gave...
>>
>>       $myArray.add("x")
>>
>>       $myHashMap.put("name", "bob")
>>
>>       $myStringBuffer.append("...")
>>
>  > ...I think it's reasonable to ignore the return values.
>
>I think Bob's point is that the template is the 'View', if you are calling
>methods which you don't want to 'View' then they should be in the
>'Controller'.

If that was Bob's point, I respectfully disagree.  I think there are 
many cases in a Velocity template where it's okay to call a method 
only for its side effects.

>  Why would you ever need to hardcode an array/hashmap in the
>template and then iterate over it?
>

One example of hard-coding an array would be to create a popup menu. 
The labels in the menu are View objects.  I might want the array 
contents to depend on run-time conditions.

Or I might simply want to get around the fact that literals in 
Velocity can't be multi-line.  Instead of one long array assignment, 
I might prefer:


#set( $myLongWords = [] )
$myLongWords.add("rhinoceros")
$myLongWords.add("callipygian")
$myLongWords.add("lactovegetarian")
$myLongWords.add("dodecahedron")
$myLongWords.add("subliminable")    ## political humor
$myLongWords.add("Schwarzenegger")
$myLongWords.add("0xDEADBEEF")      ## geek humor


Easier to read, easier to reorder, easier to comment items out.  And 
notice that you can comment individual items in a readable way.

But I don't mean to focus on hard-coding as the issue.  The issue is 
calling non-getter methods at all.  Regardless of hard-coding, *any* 
call to ArrayList.add(Object) or HashMap.put(Object,Object) has a 
return value, and I almost never want to display it.

One could decide as a matter of policy to only call getter methods in 
templates.  I would find this needlessly restrictive.

--Andy

--
To unsubscribe, e-mail:   <mailto:velocity-user-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:velocity-user-help@jakarta.apache.org>


Mime
View raw message