What does software composability mean?


Googling software composability leads to many different definitions. This is due to the fact that there are many different ways of writing composable software.  I’ll cover two simple examples that show composability.

The command line is a good first simple example of composability. There are three main reasons that the command line is composable. The first is that commands output content in a uniform way via text. Second commands also receive input via a uniform way (again via text). Lastly but still just as important, is the pipe operator. You see, the pipe operator acts as the glue between commands. It transfers output from command into input in the following command.

For example `$>ls` will simply print out the current directory contents.  Output is in a uniform format (simply text).  The uniform format makes it usable as input for another command. If we use the pipe operator we can pass the output from ls into another command. For example `$>ls | grep “src”` now we can filter the output for only files/folders that contain the text src.

The builder design pattern is another great example.  The builder pattern has a uniform interface.  All methods return the builder.  Calling different methods and arguments changes the final built object.  The methods and arguments make the builder pattern composable.

An excellent concrete example is a networking request builder.  That’s because it illustrates those two points.

class Builder(){

val headers = mutableMapOf<String, String>()
lateinit var url: String

fun addHeader(key: String, value: String): Builder{
    headers.add(key, value)
}

fun setURL(url: String): Builder{
    this.url = url
}

fun build() : NetworkingRequest{
   return NetworkingRequest(url, headers)
}

}

Calling different Builder methods creates different NetworkingRequests.  Think of all the different urls and arbitrary headers (or even no headers at all) that this builder class supports.  Different Builder urls create different NetworkingRequest objects.

Leave a Reply

Your email address will not be published. Required fields are marked *