Published in


Typesafety 101: Knowing your Types

Note: I will try to keep the articles conceptual, language agnostic* and unbiased, but as Code Quality is subjective, there will not be any clean conclusions or clear recommendations. The goal is to help a beginner (or a less experienced type-safe programmer) to have a better understanding of Types and Typesafety

*snippets will be in Scala but that the points I wish to convey can be applied to any language. I will provide a brief explanation of the langauge specifc types which should be sufficient to understand the concept.


Before we dive into the grey area and nested types, let’s get on the same page to achieve a better understanding of the problem. I’m going to go slow so please be patient (side note: during the start, you will need a lot of patience while writing type-safe programs).

val MyMap: Map[String, String] = Map("key" -> "value")
MyMap.get("key") // returns Some("value")
MyMap.get("key2") // returns None
MyMap.get(key) match {
case Some(value) => value.toJson
case None => JsNull
MyMap.get(key).map(value => value.toJson).getOrElse(JsNull)
MyMap.get(key)                     // Option(value)
MyMap.get(key).map(...).map(...) // Option(value), still contained
MyMap.get(key).get.toJson // Value if good, else NPE
* Returns the value to which the specified key is mapped,
* or {@code null} if this map contains no mapping for the key.
/** Optionally returns the value associated with a key.
* @param key the key value
* @return an option value containing the value
* associated with `key` in this map,
* or `None` if none exists.
def get(key: K): Option[V]
Photo by Markus Spiske on Unsplash



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Saheb Motiani

A writer in progress | A programmer for a living | And an amateur poker player