Tag Archives: code

7 Rules on Code Readability

Posted on by

What makes good code? Many things but whatever the qualities are, readability is a cornerstone.  If you can’t read the code, you can’t fix it. So how do you write readable code? I’ll give you my view but it’s like books, what I find enjoyable may be different from you.

read more »

Another builder pattern for Java

Posted on by

Whenever you have a domain object, data transfer object, parameter object or any other object that can’t be instantiated with constructor parameters only, you need to create a builder for its class.

The great thing about Java is that it is statically typed so a builder can give you compiler errors if you forget to set the compulsory parameters. If you use the builder in this pattern, you also get a very fluent interface that imposes order on the parameters. When using the builder, your IDE will suggest the next parameter to set.

read more »

Software Development and Tragedy of the Commons

Posted on by

My lightning talk about Software Development and Tragedy of the commons (“Kollektivt kodägarskap och allmänningens tragedi – eller hur blir min kodbas inte utfiskad) from Agila Sverige 2013

And here is the video recording from the event. My presentation begins 12 minutes and 10 seconds into the film.

Here’s the (almost word by word) english transcript of the speech:
read more »

Scala Code Kata Roman Numerals

Posted on by

There’s a Scala User Group in Gothenburg that had several meetings during this summer.  In one of the meetings the group solved a Kata named KataRomanNumerals (A Kata is a small problem that you do over and over again to learn)

The KataRomanNumerals says you should write a function to convert from normal numbers to Roman Numerals: e.g.

1 –> I

10 –> X

7 –> VII

Unfortunately I could not attend at this meeting, so I had to do it on my own during a few summer nights when my family finally was asleep :-).

Here’s my solution:

object Program extends Application {

class RomanInt(number: Int) {

def toRomanString() = {

val ones = List(“”, “I”, “II”, “III”, “IV”, “V”, “VI”, “VII”, “VIII”, “IX”);

val tens = List(“”, “X”, “XX”, “XXX”, “XL”, “L”, “LX”, “LXX”, “LXXX”, “XC”);

val hundreds = List(“”, “C”, “CC”, “CCC”, “CD”, “D”, “DC”, “DCC”, “DCCC”, “CM”);

val thousands = List(“”, “M”, “MM”, “MMM”);

thousands(part(1000)) + hundreds(part(100)) + tens(part(10)) + ones(part(1))

}

def part(digit: Int) = {

number % (digit * 10) / digit

}

}

implicit def Int2RomanInt(number: Int) = new RomanInt(number)

println(154.toRomanString()) // prints ‘CLVIII’

}

Here’s the code with better formatting.

Please comment and correct if I didn’t write idomatic Scala or if you have suggestions for improvements.

/Hans