Beautiful Code

by Dion Almaer

Cameron and Cedric have been talking about beautiful code.
Cedric talked about how people don't tend to say "wow" a lot with Java code, due to the syntax. If someone pulls of an amazing line of Perl, fellows can see how cool it is that they did that. Other people think "why" and "i am glad I am not maintaining that!".




Regarding some of the code they talk about:



Cedrics:


List users = dao.getUsers();
for (int i=0; i < users.size(); i++) {
User user = (User) users.get(i);
// do something with user
}




Camerons:


List listUsers = dao.getUsers();
for (int i = 0, c = listUsers.size(); i < c; ++i)
{
User user = (User) users.get(i);
// do something with user
}



Neither of those look that cool do they. I personally would much prefer to see:


foreach (user in dao.getUsers()) {
// do something with user
}


I am looking forward to the foreach statement joining the Java language. Casting and futzing around all the time can be such a pain... sometime you just want something simple that works :) But, at the end of the day, it's the same stuff, just a different day.




Now, on large projects, the "wow" factor comes from great elegant DESIGNS (like Cedric mentioned), not a great few lines of code. I will use your interface... as long as it does what is stated, I don't care what you did :) [within reason]




Don't even get my started on the code indentation that Cameron uses ;) Each to their own.