söndag 15 november 2009

Keep it pretty

One thing i dont like with scala, is that alot of the code i see, is uglier then alot of the java code i see. This is probably partly because im more used to java. But also because scala has some great and powerfull features that not always make the code easier to read.

I have been dooing some refactoring in Scala. Mostly to get used to reading Scala code. I have encounterd some things that are not possible in java, that can make it abit harder to read scala code.


def formatAge(date: Date): String = {
    def today(d1: DateTime) = d1.getDayOfYear == new DateTime().getDayOfYear

    val dateTime = new DateTime(date)
    if (showAsAge_?)
      TimeFormatter(dateToAgeSeconds(date.getTime)).colonSeparated
    else
      if (today(dateTime)) fmtNoDay.print(dateTime) else fmt.print(dateTime)
  }

I totaly see why today is a method. But i dont see why it should be inside formatAge. When i read code, i want to read the usages of a method before i read the implementation.

def formatAge(date: Date): String = {
    val dateTime = new DateTime(date)
    if (showAsAge_?)
      TimeFormatter(dateToAgeSeconds(date.getTime)).colonSeparated
    else
      if (today(dateTime)) fmtNoDay.print(dateTime) else fmt.print(dateTime)
  }

def today(d1: DateTime) = d1.getDayOfYear == new DateTime().getDayOfYear


Isnt it easier to see what formatAge does? Is it realy that horrible that today gets a little bigger scope?

I dont realy like the way TimeFormatter is used either. It looks almost like static methods in java are beeing called. But its actually a new class beein instansiated and a method on that instance beeing called. Im not sure how want it. But im sure scala can make it pretty!
 

Inga kommentarer:

Skicka en kommentar