I don’t know what it is about programmers that gives us crack withdrawal itches any time we see “unnecessary” lines of code, but our need to get that fix sometimes takes things way too far. I see way too much code where the programmer has sacrificed readability and understandability in the name of saving a line or two of code. Sometimes it’s okay:
Collection c = new ArrayList();
someObject.setCollection(c);
(becomes)
someObject.setCollection(new ArrayList());
That’s just damn good refactoring right there. And while I encourage you to save lines where it makes sense, sometimes we take it way too damn far. For the love of God it’s not a sin to have an extra line of code or two. At some point someone is going to need to read that code and, I’m sorry but if I have to read this line of code that you wrote I’m going to show up at your house in the middle of the night and go fucking monkey nuts crazy on your ass:
for(Iterator i=CollectionUtil.getIterator(list);list.hasNext(); ) callSomething((String)list.next(), (x > 5 ? foo() : bar())); (see doesn't even fit)
Less lines doesn’t automatically make your code cleaner. Code is meant to be read top-down. Write it that way.
Iterator i = CollectionUtil.getIterator(list);
while( i.hasNext() )
{
String s = (String)i.next();
callSomething(s, (x > 5 ? foo() : bar()));
}
Yeah, I used a few more lines but you’re not going to get confused and get the urge to punch a baby reading the second version. Seacrest out.