Sunday, 14 May 2017
Coffee in the Mine: In Java I wish I could... part 1
However, there are some fantastic features in the language and a few of them I wish I could use in Java. For example, in Ruby, you can put conditional statements after expressions, for example:
return '1' if a == 1
return '2' if a == 2
Whereas in Java you'd have to write:
if (a == 1)
if (a == 2)
which is more verbose and less expressive. Ruby also has the unless keyword, which Java lacks, so in Ruby you can do this:
return @colour unless @colour.nil?
The example shows off another feature in Ruby. To test for nil in Ruby you can call .nil? on any object, whereas the equivalent null check in Java is more verbose:
if (colour != null)
I could go on, but I'll leave that for a later piece in the series. These features of Ruby may only be, in the main, syntactic sugar, but they are the ones I miss most when I'm developing in Java.
Next time we'll look at what Rails taught me about Spring MVC projects.