Friday, 13 June 2008

One missing language construct

A while ago, when reading about a JavaScript template engine, I encountered a language construct called

forelse

The construct was placed to accomodate for the case where there was zero iterations in a for loop. This is quite handy in web scenarios, for instance when you need to present a list, and the list is empty. You would then do something like this:

for (SearchItem item : items) {
... (do something with item)
} else {
.. (do something when there are no items)
}
It's not by accidence that I show it like java code. I sometimes come across this scenario, but usually see it inside an if:

if (!items.isEmpty()) {
for (SearchItem item : items) {
... (Do something with item)
}
} else {
... (Do something when there are no items)
}
And I have actually begun to find it too verbose :-)

So, +1 for a forelse construction..

0 comments:

Post a Comment