Monday, August 1, 2011

Grails, and seemingly dirty command objects

I love Grails, but sometimes, the magic is a little too much.

I was trying to debug a particularly strange oddity, and as far as I could tell, Grails was reusing a dirty command object.  That's not possible, right?  Further investigation showed that no, it's not using a dirty command object, well, not really.  Its only doing what it's supposed to: dependency injection and convention over configuration.
Here is the simplest form of the problem I have been able to put together.

The controller:
def debug = { DebugCommand d ->
render new JSON(d)
}

The command objects: I have nested commands, with DebugCommand being the outer command (used by the controller) and DebugMapCommand, a map holding some values.  I'm using a LazyMap since that's why I used in my real-life problem.

public class DebugCommand {
    int someNum
    DebugMapCommand debugMapCommand = new DebugMapCommand()
}

@Validateable
public class DebugMapCommand {
  Map things = MapUtils.lazyMap([:], FactoryUtils.instantiateFactory(String))
}

What happens here is that multiple calls to the controller/action result data being accumulated in the LazyMap between calls:

nancyd $ curl 'http://localhost:8080/library/rpc/debug?someNum=5\&debugMapCommand.things\[2\]=5'
{"class":"com.example.DebugCommand",
 "debugMapCommand":{"class":"com.example.DebugMapCommand",
                    "things":{"2":"5"}},
 "someNum":5}

nancyd $ curl 'http://localhost:8080/library/rpc/debug?someNum=5\&debugMapCommand.things\[1\]=5'
{"class":"com.example.DebugCommand",
 "debugMapCommand":{"class":"com.example.DebugMapCommand",
                    "things":{"2":"5","1":"5"}},
 "someNum":5}

nancyd $ curl 'http://localhost:8080/library/rpc/debug?someNum=5\&debugMapCommand.things\[elephants\]=5'
{"class":"com.example.DebugCommand",
 "debugMapCommand":{"class":"com.example.DebugMapCommand",
                    "things":{"2":"5","1":"5","elephants":"5"}},
 "someNum":5}

If I gave a new value for a map entry, the new value was used.

So, what's happening?  The DebugCommand's reference to the DebugMapCommand is called debugMapCommand, and Grails thinks I want a DebugMapCommand injected, so it created a singleton, and passed it to all my DebugCommand instances. Oops.

Trying to prove this wasn't too easy.  It would seem that a number of factors are necessary for this particular issue to manifest:

  1. The field name must be the same as the class name with the first letter lowercased
  2. The inner/sub command, DebugMapCommand, must be annotated with @Validateable
  3. The inner/sub command must be in a package that is searched for "validateable" classes (in Config.groovy, you have to have grails.validateable.packages = ['com.example.yourpackage', ...])

So, what's the lesson here?

Don't name your fields after their class.

Sunday, June 12, 2011

Why is reading code so hard?

We've all been there.  Faced with lines and lines of code, written by an intern, a programmer long gone, or a vendor with whom you do not have a support contract.

Code can be quite hard to read.  Why is that?

Without looking at the language or frameworks used, some of which are easier to read than others, there are other, more human factors that make it had to puzzle out what code does.  I'm not talking about how the code was written, but how we attempt to read it.

1. Reading code usually involves two actions: identifying what the code does, and what it was actually meant to do.

When we approach code, it's rarely because we're just curious; we usually want to fix bugs or improve it.  We must know what the code does, but for the structure and implementation of the code to make sense, we have to figure out what the programmer intended to do.  It's usually easier to figure out the intent first, then work out the discrepancies - where we think the code doesn't do what it was meant to.

This can be particularly hard when we have to look at code that was edited multiple times, by multiple people with different approaches.  Then, it's more of an archeological expedition.  That's the situation that can lead to some pretty ugly or silly code.  I remember looking at code once that, I can only imagine, was the product of too many edits:

if (someCondition == true) {
  myVar = false;
} else {
  myVar = false;
}

2. We're talking too loud, and not listening enough

When we see code like the one above, we laugh, we point, and our opinion of the code (and the programmer(s)) goes down.  We start generalizing and thinking the worse.  How can code with that in it be any good?  And what were they thinking?  who would wrote such poor code?

And that's when we start losing faith in the code.  Our disdain takes over, and our ears close.  We see some odd code, and instead of genuinely asking "what were they trying to do here?", we throw our hands in the air and sigh, "what were they trying to do here!"

If you want to understand the code, you must listen to what the previous programmers were trying to say.  Withhold judgement, open your eyes, your mind, and assume they were trying to accomplish something.  They may even have something to teach you about how to solve problems, about the business, or about the history of the code.

3. It's not written how we think

If you've ever written in perl, (and even if you haven't) you know that there's more than one way to accomplish a task.  Sometimes, it's a simple syntax tweak, but sometimes it's a whole approach.  For example, if I were to put an apple and an orange on the table and ask you to describe what you see, would you start with "I see two fruits" or "there's an apple and an orange"?  Depending on your answer, chances are you approach problems differently.

The code may use programming techniques we're unfamiliar with (using functions as parameters, patterns, functional programming).  Unfortunately, if we're unfamiliar with the technique, we're unlikely to recognize it it the wild, unless the programmer was kind enough to leave comments about it.  This is where experience and breadth of knowledge will come handy.  Keep up to date with developments in the field.

4. We lose track

Not all code is nicely packaged, modular and cohesive.  More often than not, we start reading code in one spot, only to realize we need to find out what a function does in another file or module.  We leave the code here, investigate the code there, which takes us to another module,... and by the time we get back to the original spot, we've totally forgotten what we were reading or trying to do.å

Take notes.  Draw pictures.  Use your IDE to find relations between code, where functions are called from.  I find printing code is, unfortunately for the trees, helpful, as I then remember not only the code, but where the code is on the page; and I scribble all over the paper in various colours.


What can we do about it?
  • Try to find out what the programmer wanted to do
  • Then see if the code does it
  • Keep an open mind - don't let your prejudice get in the way
  • Learn new (to you) techniques and patterns
  • When you code, try to see more than one way to solve the problem; maybe next time you try to read someone's code, they'll have used your second or third choice, and you'll recognize it more easily
  • Try to remember code you've written, before you were perfectly fluent in a new language, or when you were too rushed to go back and fix some readability issue.  Maybe there's someone reading that code now; treat the code in front of you as you would have others treat yours.

Why do you think reading code is so hard? what helps you?

Monday, January 10, 2011

Scala building blocks part 2: imports and packages

If you're used to Java, Scala imports and packages look both familiar and all wrong and weird.

Imports

The first thing that you may notice in some code you're reading is the use of _ in imports. The best way I've found to deal with _ is to replace it in my head with "whatever". It works in imports, but it also works in other cases where you encounter _.

So, when you see

import com.nancydeschenes.mosaique.model._

it means "import whatever you find under com.nancydeschenes.mosaique.model"

You can lump together multiple imports from the same package:

import scala.xml.{ NodeSeq, Group }


Imports are relative:

import net.liftweb._
import http._

imports whatever's needed in net.liftweb, and whatever's needed from net.liftweb.http.

But what if I have a package called http, and don't want net.liftweb.http? use the _root_ package:

import net.liftweb._
import _root_.http._

Packages

Like Java, code goes in packages. Unlike Java, packages aren't dictated by the directory structure. You can put multiple public classes in one file, and the location of the file doesn't imply what package it will be in. Do keep things simple, tho, if you ever want to be able to re-read yourself.

You can use the same package statement you would with Java:

package com.nancydeschenes.mosaique.snippet

Or, you can use the package block:

package com.nancydeschenes.mosaique {
  // Some stuff

  package snippet {
    // stuff that ends up in com.nancydeschenes.mosaique.snippet
  }
}

Sunday, January 9, 2011

Scala building blocks

When I first started reading about Scala, I saw a lot of very interesting looking code, but I felt lost.  It looked as if I was trying to learn a foreign language with a phrasebook.  I saw a number of examples, but I felt I was missing the building blocks of the language.

I've been using Scala for a little while now, and this is the introduction I wish I'd had then.  This post is not meant as a detailed "how things are" but just to provide a newcomer with the right frame of mind to understand the rest.  There are some over-simplifications, but I find they help me understand, so I'm sharing them.


Classes, traits, objects, companions

Everything in Scala is an object, and every object is of a particular class.  Just like Java (if you ignore the pesky primitives).  So, generally speaking, classes work the way you expect them.  Except that there's no "static".  No static method, no static values.

Traits are like interfaces, but they can include fields and method implementations.

Objects can be declared and are then known throughout the program, in a way that's reminiscent of dependency injection, particularly the Grails flavour.  Object declarations can even include implementation, and new fields and methods.

Companion objects are objects named the same as a class.  This is how you handle details that you would normally want to do as static methods/fields/etc.  This is very elegant: it makes it look as if you had access to static elements, while preserving a true object-oriented approach.

So you may encounter code such as

class Car extends Vehicle with PassengerAble {
   val numWheels = 4
   val numPassengerSeats = 6
}

object Car {
  def findByLicensePlate(String plate, String emitter) : Car = {
    Authorities.findEmittingAuthority(emitter).findByLicensePlate(plate);
  }
}

object MyOwnCar extends Car with PassengerSideAirbags, RemoteStarter {
  val numChildSeats = 2;
  def honk : Unit = {
    HonkSounds.dixie.play;
  }
}

In that example, the first Car is a class.  The second Car is an object.  MyOwnCar is an object that can be addressed anywhere (same package rules apply as java), but MyOwnCar has extra stuff in it: PassengerSideAirbags and RemoteStarter are trait (you can guess that because of the with keyword).  It even defines a new method so that honking it MyOwnCar should remind you of the Dukes of Hazzard.


Types

Unlike Java, in Scala, everything is an object.  There is no such thing as a primitive.


Basic types

At the top of the object hierarchy, we have Any.  Every object, either what you think of an object in a Java sense, or the types that are primitives in Java, every thing inherits from Any.

The hierarchy then splits into two: AnyVal and AnyRef.  Primitive-like types are under AnyVal and AnyRef is essentially the equivalent of java.lang.Object.  All Java and Scala classes that you define will be under AnyRef.

Strings are just like Java Strings.  Double-quoted.  But Scala defines a few additional methods on them, and treats them as collections, too, so your String is also a list of characters.  A particularly convenient method, I've found, is toInt.  There's toDouble and toBoolean too.

Unit is what a method returns when it doesn't return anything.  You can think of it as "void".

Null as you know it is a value, but in Scala, every value has type, so it's of type Null.  And Null extends every single class, so that null (the one and only instance of Null) can be assigned to any AnyRef object.  It sounds crazy, but if you let it sink in, it will make sense.  Null is actually a trait, not a class.

Nothing is the absolute bottom of the hierarchy.  Nothing doesn't have any instances.


Numeric Types

Integers are of type Int.

Doubles are Doubles, floats are Float.

A litteral in the code is treated an object of the appropriate type.  Things just work, without "autoboxing" or other convolutions.

Strings and numeric types are immutable.


Collections

Collections come in mutable and immutable variations.

The operation you can perform on collections are probably easier to understand and get used to when you remember lisp/scheme.  Or perl.  Try to view collections are a whole, and not so much as an object with a few methods, the way we tend to in Java.  The think of the methods as operations on the whole.  It may also help to dust out set theory concepts.

A special kind of collection is the Tuple.   It's an ordered group of N items that can be of the same or of different types.  They are defined for N=2 to N=22.  You can access the j-th element of the tuple with ._j   (ex: the first is myTuple._1, the third is myTuple._3)

Options

The first thing you have to realize about options is that they're everywhere, so you better get used to the idea.  Lift uses Box instead, but it serves the same purpose.

The second thing you have to realize is that Options (or Box) is the right way to handle multiple scenarios, but you've spent your programming life working around that fact.

Let's take a simple example.  You want to implement a method that computes the square root of the parameter it receives.  What should that method return?  a Double.  So you start:

def sqrt (x: Double) : Double = {
 // ...
}

You start implementing, maybe by writing some tests first... Hm... what happens if x is a negative number?  you may think "I'll just return null" or "I'll throw an exception".  If you return null, chances are the caller wont bother checking, and bad things will happen.  If you throw an exception, you slow things down a bit (exceptions should be for exceptional condition).  If you throw an unchecked exception, who knows where in the caller's code that will be handled.  If you throw a checked exception, the caller's probably already sick of catching every exception every where and has a standard, no-thought solution (ignore, wrap in a RuntimeException, etc)

Options tell you that there are multiple outcomes possible.  The case where a regular value is returned, the Option is actually a Some (subclass of Option), with the value in it.  In cases when no value can be returned, the Option is actually None (an object).  Of course, Options can be used in contexts other than returning a value from a method, but that's an easy way to see their usefulness.

Once you have an Option, you can test it for Some or None, or you can try to find what's in it: myOption.get will return the content of the Some (or throw an exception if it's None, so don't do that unless you know).  If you're not sure, you can use myOption.getOrElse(defaultVal)  which will return the contents of the Some, or gracefully (and elegantly) use the default you provided.

Or, you can use matching to decide what to do:

MathHelper.sqrt(x) match {
  case Some(y) => "The square root of " + x + " is " + y
  case None => "The value " + x + " does not have a square root"
}

Monday, January 3, 2011

Ruminations on Rejection

A few months ago, I attended a Montreal Geek Girl Dinner about "Women in technology".  I was hoping to meet like-minded women and maybe some men, but I was surprised that the majority of attendant were what I consider technology users, rather than technology producers.  These are women who use technology to provide content, who mange technology production, who sell it, who bring it to all of us, making our lives easier, more interesting, or more fun.  I was glad I came, even tho it wasn't what I had expected.

At one point, the issue of the few women speakers at conferences came up.  One of the the organizers of ConFoo (one of the brave men in attendance) asked for ways to get more speakers, because very few women step up.  I thought about it, and I think maybe I'm one of those women who just do not step up to be heard. I want to do it, but..  surely, there's someone better than me out there, surely there's nothing special I can bring to a crowd... I'm just a developer - a good one, but why me?  What would make me step up?

I'm not exactly shy.  Well, okay, maybe a little, but when I see a need, I step up.  I  usually let others lead, but when I know I can do better, I speak up, and guide the situation the way I think will work best.  So what would make me volunteer as a speaker? a void; a need.  I went to the organizer of ConFoo, and told him that maybe, instead of sending out vague requests for speakers, they should suggest specific topic to specific women, showing them the need.  Which he promptly turned around on me, and suggested I do a Grails talk at ConFoo.

I considered it for some time, trying to convince myself I could do it.  I know Groovy and Grails well, I like it, and I think I can communicate my enthusiasm to other developers.  I took a deep breath, and proposed the talk.  And felt all giddy.  And started thinking about how I'd do the presentation.  Then I realized I'm also pretty good at SQL, from a programmer's perspective.  How you can't tune a database on it's own, but you have to look at it in the context of the application; sometimes you have to change the logic in your program instead of tossing another index at the problem.  How sometimes, it's better if a process takes longer, if it's less intrusive on the other users of the system.  And how to write and rewrite queries until they do what you want, and how you want.  After the rush of the first proposal (and a few words of encouragement) I proposed a session on SQL for programmers.

Finally, I had most recently started working with Scala and Lift, and I could see the potential.  I submitted a session for this too, because I thought it should be presented, but I was hoping I wouldn't get selected - I have so little experience with it, and none in a proper "work" context, that I thought it was pushing it a little.  Still, no better way to learn than when thinking "and how do I explain that to someone else?"

So I submitted three talks, and waited.  I started writing notes about how I wanted to design each presentation, what aspects I particularly didn't want to forget, etc.  Just in case I was selected.  They did say they wanted a session on Grails, and they did say they wanted more women.  I wasn't expecting to get a session due to my gender alone, but I thought my chances were pretty good.  But I also knew very well I had no experience to show for, no visible accomplishment.  And I was worried that I would get selected! Would I have enough time to prepare as well as I'd like?

And then, the decision came.  The list of speakers was published, and I couldn't find my name on it.  I looked and looked and.. nope! nothing.  Not for the Scala talk, not for the Grails talk, not for the SQL talk.

I was surprised at how disappointed I was.  It's not like I was promised anything.  There were quite a number of proposals, many by professional speakers, or by people directly involved in the making of the technology discussed.  I was able to confirm later that there will be a talk on Grails, and at least a talk on SQL (by someone whose blog I've consulted on many occasions, so he's a lot more of an authority than I am, let's face it).  I didn't see anything about Scala/Lift, but I may have missed it.  And it's very new, and definitely a serious paradigm shift for anyone doing Java or PHP, and even Python.

So, why was I so disappointed?

Probably because in order to even dare proposing a session, I had to convince myself that it was needed, and that I was the only one who could do it.  I wasn't.  I don't like taking chances, I don't like selling myself.  People should just realize how awesome I am, without me ever having to brag.  Yeah, that's not how it works..  I have done very well for myself with that philosophy, but I have also not done anything particularly visible, cool, amazing...  Oh, I've done some really impressive stuff.  I wrote one of the first CMS (but you wouldn't know it) and I worked one some pretty cool sites (like Heat.net - but you probably don't remember it).  I wrote one of the first applets that kept track of who was on-line - well before anyone spoke of "Web 2.0".  Well, if I want to be seen, be recognized by my peers, I'll have to do a lot more than wait for someone to offer me a session at a conference.

What now?

Now that I've tasted the thrill of proposing sessions at a conference, and now that I know that rejection sucks, but you survive, dust yourself off, put a band-aid on the bruised ego and move on... now, I've decided that working alone in my office and gathering knowledge and skills is not enough.  I want more.  I will be more.  I will take risks, and put myself out there.  I will not stop at "what I'm supposed to do".  I will also listen to what I want to do.  And I will try to share what I know, as best I can.  And there are plenty of other conferences.  And there's next year.

I know that whenever we do something, we "practice" doing it - we get better at it, it becomes easier.  So it's time for me to start practicing risk taking.  At least a little.

Isn't it convenient that this is happening at the turning of the year?

Here's to 2011!