25 October, 2026 / color theme: ef-spring
Duck typing results is a more open than static typing approach.
It lets us use the function quack in more contexts (what
can robo-duck say?), and it leads to systems with less code and more reliable, which is strange to say for dynamically typed systems.
There are other features that help programs be more flexible:
This architecture benefits from a dynamically typed language.
It’s a sequence of stages that are executed step by step.
Imagine a client calls a GET /cart endpoint,
and it returns all the products of the client's cart.
It does a sequence of steps:
Generally, it might have any number of steps.
In statically typed languages this is implemented as follows: we create types for the results of each step, and each step transforms data from one type to another.
Now, imagine something went wrong. How can we debug the pipeline? Let's say logging doesn't cover the bug. The only thing left is to start analyzing the code.
In dynamically typed languages though, we can choose another implementation: instead of transforming data with losses, we can create a "big bag of data" object with the results of each step.
When the bug happens, we log the whole object (without sensitive or very big data) or the client sends it, and now we have all the info we need to debug it.
Debugging such code is easy (we just read the log) and enjoyable.
It's hard to express the move in types: until the final step happens, we have only a partially filled "big bag of data" structure. It is a pain to make every field optional and work with that structure.
More about the pipeline styled programs can be found in a Sportify series of Functional Design in Clojure podcast.
With dynamically typed languages, it’s easier to work with data from other languages.
Languages shape how people think and what they see as
best practice. For example, TypeScript developers like to use a type
discriminator field. It requires a lot of code to
deserialize such JSON into a Go struct:
type Animal = {
type: 'dog';
energyLevel: 'high' | 'veryHigh';
} | {
type: 'cat';
sleepDurationHours: number;
};type Dog struct {
Type string `json:"type"`
EnergyLevel string `json:"energyLevel"`
}
type Cat struct {
Type string `json:"type"`
SleepDurationHours int `json:"sleepDurationHours"`
}
func UnmarshalAnimal(data []byte) (any, error) {
a := struct{
Type string `json:"type"`
}{}
err := json.Unmarshal(data, &a)
if err != nil {
return nil, err
}
switch a.Type {
case "dog":
dog := &Dog{}
return dog, json.Unmarshal(data, dog)
case "cat":
cat := &Cat{}
return cat, json.Unmarshal(data, cat)
default:
return nil, fmt.Errorf("unknown type %q", a.Type)
}
}A better approach would be to program in a language that doesn’t impose such things on the user and lets them handle arbitrary JSON:
(json/parse-string animal)
;; we got: {:type "cat" :sleepDurationHours 20}Conceptually, a field of a product type is a function: we give it a
structure, and the field turns the structure into a result, which is the
field’s value. So higher-order functions such as filter,
map, sort-by, etc, should take field names as
function arguments. Compare this code in Go:
func sortProducts(products []Product, attr string) []Product {
result := copy(products)
slices.SortFunc(results, func(p1, p2 Product) int {
switch attr {
case "cost":
return cmp.Compare(p1.Cost, p2.Cost)
case "weight":
return cmp.Compare(p1.Weight, p2.Weight)
case "id":
return cmp.Compare(p1.ID, p2.ID)
}
})
return result
}
// example
sortProducts(products, "cost")
with this code in Clojure:
(defn sort-products [attr products]
(sort-by attr products))
;; example
(sort-products :cost products)
Much better, no? And if we want to sort by the new attribute, we don’t need to rewrite the function, so it’ll be twice as fast to develop new code.
I know that Go has dynamically typed features. Anonymous functions help a lot in such situations, and the actual Go I’d write is this:
func sortProducts[T cmp.Ordered](products []Product, keyFn func(p Product) T) []Products {
result := copy(products)
slices.SortFunc(result, func(p1, p2 Product) int {
return cmp.Compare(keyFn(p1), keyFn(p2))
})
return result
}
// example
sortProducts(products, func(p []Product) int { return p.Cost })
Notice how lambda functions help. They are a feature that was introduced in dynamically typed languages (first in Lisp).
Overall, I found that being able to work with types as hash maps helps a lot for building interfaces of functions, and results in less code that’s easier to read.
By the way, TypeScript is also able to get a field of the object dynamically, which is a feature that comes from dynamically typed JavaScript.