Skip to main content

Simple code: Functions and methods

What makes a good function or method? I don't think it's a single thing but a combination of things where each is significant. If one the things is flawed it affects to all others and the whole function is flawed. So what are those "things"?

Have a meaningful name

Function should have a name that describes it's purpose or functionality. When a function has a meaningful name it's easy to read and understand what's it's purpose.

Let's take a example.

If function's purpose is to find a customer by it's id a good name could be findCustomerById(id: String) or it could just as well be just findCustomer(id: String) because the function signature implies that the
customer is found by it's id the word find also implies that the customer might be found or it might not be found.
If the function's name would be changed to getCustomer(id: String) it's meaning changes because now it implies that there's no fallback, the customer is either found or the function fails miserably and maybe throws a exception.
Both are valid names for a function but they have a different meaning and therefore their implementations should also be different.

Should have as few parameters as possible

I like to follow the rule of three myself what that means is that a function should have three or less parameters. When the function needs more than three parameters it should be rewritten and the parameters placed inside a data holder e.g. class, data class, JavaScript object etc. This is a easy way to reduce the number of parameters and to organize the data within the application.

Lets take a example of a function that has identical behaviour but differing signatures.

fun createCustomer(
  firstname: String,
  lastname: String,
  streetAddress: String,
  city: String,
  zipCode: String


data class Address(
  val street: String,
  val city: String,
  val zipCode: String,
  val streetNumber: String

data class Customer(
  val firstname: String,
  val lastname: String,
  val address: Address

fun addCustomer(customer: Customer)

Does what's expected

Function should do what's expected of it, nothing more, nothing less. If a function is named as findAddress(latitude, longitude) it should find the address in the given coordinates or if no address can be translated for the coordinates return a None, null, Empty, what ever is the appropriate type for the given language. The function should not do anything else e.g. find  adjacent addresses or building records of the coordinates or address. The function can have side effects like logging or analytics but those
are invisible to the input and to the output.

Is testable

Functions should be designed so that they're testable. In the previous code sample I defined the function addCustomer but I didn't define any return type for it so in that format it's testability is questionable. Sure it could be tested with mocks or spies depending on what the internal implementation is like but by just simply giving it a return type we can easily test it.

fun addCustomer(customer: Customer): Customer

With the given function signature we can return the added customer entity to the callee and with that addition we can test that the function does what it's supposed to do that customer entity e.g. assign it a unique id.

Four conventions for functions

Have a meaningful name, have as few parameters as possible, do what's expected of it and be testable.
Doesn't seem that hard to follow or too restricting conventions that when followed makes the code simple, easy to read, easy to reason, testable and maintainable.

Next part

In the next part I'll be writing about contracts and how they're related to code.


  1. Hey Jyri!
    Makes sense :)

    Any thoughts around length of functions. If all of the conditions you describe are met, but function is still say 200 lines, is it ok or should it be split?

    1. In most situations I'd say the function should be split but that being said I don't think there's a single truth in the length matter :)
      There are at least some situations where it makes sense to have a long function. One natural occurence where longer functions are unavoidable are functions that compose big data structures. They could be split but to my experience that would often weaken the readability and understandability of the function.


Post a Comment

Popular posts from this blog

Simple code: Immutability

Immutability is a special thing that in my mind deserves a short explanation and praise. If you're familiar with functional programming you surely recognice the concept of immutability because it's a key ingredient of the paradigm. In the world of object oriented programming it's not as used and as easy to use approach but there are ways to incorporate immutability to parts of the code and I strongly suggest you to do so. Quick intro to immutablity The basic idea of immutability is unchangeable data.  Lets take a example. We have a need to modify a object's property but because the object is immutable we can't just change value but instead we make a copy of the object and while making the copy we provide the new value for the copy. In code it looks something like this. val pencil = Product(name = "Pencil", category = "Office supply") val blackMarker = pencil.copy(name = "Black marker") The same idea can be applied in functions and metho

Simple code: Contracts

Code works around contracts and contracts should be carefully thought and crafted. What are contracts A High abstraction level of contracts for code are API's. They define a interface that is basically a contract that the producer and consumer of the API agree to use to communicate with each other. Two common forms of API's are libraries that are used in code and external API's  that are used via HTTP, RPC etc. When thinking in a bit deeper contracts consist firstly of functions, methods or external endpoints and secondly of data, more precisely on data models and data types within the models.   Defining contracts Contracts should always be defined with careful thought. I've come accross few times to someone saying that "this is for intenal use only so it doesn't need to defined and/or documented as thoughtfully as a public API would be" but I disagree with that. The same care should be be given to internal and external contracts because the contracts are