How to instantiate an object with given arguments.

I write a lot of CoffeeScripts apart from the other languages like Ruby or Scala. I’m always forage among possible solutions for one specific task to be sure that I’m doing it in the best way it can be done. Sometimes it leads me to stupefying places, which I would never find during my regular programming.

Case study

The recent problem I’ve encountered was constructing CoffeeScript’s objects. I came across the following code:

I was wondering how one may build this object and I found the snippet like this:

Well, it’s still quite readable. Instead of passing merely values, there are named variables, which is fair enough in that case.

However, to be honest, you don’t have to be an expert to know that something is not right. My gut told me there’s a better approach out there.

Constructing an object

So what is wrong with objects’ constructors? Aren’t they a natural way to instantiate an object? That’s how we’ve been taught after all.

Nevertheless someone sometimes may wonder “How many constructor arguments is too many?”. Exactly, but why wonder at all? Because it’s almost certain that at some time, you’ll need to jump to the class definition to see what exactly is being passed there.

Can you tell me what the following excerpt does?

new MyObject('foo', 1, false, ['bar'])

Yes, it creates an MyObject’s instance, but what do the arguments mean there?

Solutions

So except the aforementioned plain approach, there are a bunch of others that may be used in CoffeeScript as well.

1. Ruby-like approach

In Ruby we often see something like that:

def initialize(params = {})

which means that a constructor accepts any number of named arguments or rather a hash with options:

Foo.new(foo: 1, bar: false)

We can do the similar thing in CoffeeScript as well:

Instead of just fetching properties, we are also assigning default values in case they don’t exists in a given parameters’ list.

That’s how we can instantiate a particular object then:

2. Destructuring Assignment

This is an approach, which can be used in idiomatic way in CoffeeScript:

Huh? How does that work?

Awesome, isn’t it?

3. Builder pattern

Last but not least is the builder pattern known from the other languages, especially Java, where it is used extensively. In this approach you create an object without any arguments and then using some auxilary methods you fill the rests of the arguments.

It may be used like that:

That is simple and pretty readable to understand very quickly what exactly we are assigning for a particular object.

Patterns

Here are some resources you may find useful. They are describing common patterns in a more formal way than I did and are good basics for better understanding the aforementioned solutions:

Other languages

Some other languages like Ruby or Scala provide something like keyword arguments or named parameters. They are a better way to indicate, which argument means what and they make construction of an object more neat and concise.

In Ruby we can do for example:

In Scala similarly:

Summary

As you can see, there are many elegant solutions for naming arguments in constructors or functions. Remember to be reasonablee, because sometimes it may be overwhelming to apply a particular pattern for simple use cases. Take a look for a plain, but self-explanatory example:

new FullName('Kamil', 'Lelonek')

I hope that everything is clear now and you’ll use those solutions in your own code.