SlideShare a Scribd company logo
Understanding our code
with tests, schemas, and
types
Mark Godfrey
@msgodf
The problem
(ns please-understand-me
…)
(defn my-function [its-complicated]
…)
๏ Why do I need to understand the code?
๏ Why the problem exists
๏ Examples of things that might help
๏ Some other ideas
๏ Conclusion
Why do I need to
understand the code?
To use it
To change it
To review it
Why does this problem -
understanding the function
interface - exist?
What things might we find
in the codebase that could
help?
The code
(defn my-function
“I take a map like this:
{:name ‘Sebastian’
:age ‘27’}
and return the year of birth.”
[its-complicated]
…))
(ns please-understand-me
“You might understand me better with a docstring”
…)
(defn my-function
“I take a map like this:
{:name ‘Sebastian’
:age ‘27’}
and return the year of birth.”
[its-complicated]
(:age its-complicated))
(ns please-understand-me
“You might understand me better with a docstring”
…)
(defn my-function
[{:keys [name age] :as its-
complicated}]
…)
(ns please-understand-me
…)
(defrecord Person [name age])
(defn my-function
[its-complicated]
…)
(ns please-understand-me
…)
(defrecord Person [name age])
(defn my-function
[its-complicated]
(->YearOfBirth (:age its-complicated))
(ns please-understand-me
…)
(defprotocol Summarizable
(my-function [its-complicated))
(defrecord Person [name age]
Summarizable
(my-function [its-complicated]
… ))
(ns please-understand-me
…)
(defn my-function
[^Person its-complicated]
…)
(ns please-understand-me
…)
Tests
(deftest verify-me
(is (= (my-function {:name “Derek”
:age 28})
28)))
(ns please-understand-me-test
(:require [clojure.test :refer [deftest is]])
…)
And less likely…
Documentation
Schemas
(def Person {:name s/Str
:age s/Int})
(s/defn my-function :- s/Int
[its-complicated :- Person]
…))
(ns please-understand-me
(:require [schema.core :as s])
…)
(s/defrecord Person
[name :- s/Str
age :- s/Int])
(s/defn my-function :- s/Int
[its-complicated :- Person]
…))
(ns please-understand-me
(:require [schema.core :as s])
…)
(def Person
{:name (s/maybe schema/Str)
:age (s/either s/Int
(s/eq :dont-ask))
:occupation (s/protocol Job})
(s/defn my-function :- s/Int
[its-complicated :- Person]
…))
(ns please-understand-me
(:require [schema.core :as s])
…)
Types
(t/defalias Person {:name String
:age Integer})
(t/defn my-function
[its-complicated :- Person] :- Integer
…))
(ns please-understand-me
(:require [clojure.core.typed :as t])
…)
(t/defalias Person {:name String
:age Integer})
(t/ann my-function
[its-complicated :- Person] :- Integer))
(ns please-understand-me.types
(:require [clojure.core.typed :as t])
…)
Are types really an
option in Clojure?
Type Error (core.clj:66:3) Function my-function could not be applied to arguments:
Domains:
(typed/HMap :mandatory {:name String, :age Integer} :optional {:occupation (typed/U
nil None)})
Arguments:
(typed/HMap :mandatory {:name String, :age Integer} :optional {:occupation (typed/U
nil None)
Ranges:
String
with expected type:
Integer
in: (my-function {:name “Derek”, :age 28})
Type Checker: Found 1 error
Found errors
$ lein typed check
(t/ann ^:no-check
clojure.tools.logging/*logger-factory*
clojure.tools.logging.impl/LoggerFactory)
(t/defalias LogLevel
(t/U ':trace ':debug ':info ':warn ':error ':fatal))
(t/ann ^:no-check clojure.tools.logging/log*
[clojure.tools.logging.impl/Logger LogLevel (t/U nil Throwable) String -> nil])
(t/ann-protocol clojure.tools.logging.impl/Logger
enabled?
(t/IFn [clojure.tools.logging.impl/Logger typed/Kw -> typed/Bool])
write!
(t/IFn [clojure.tools.logging.impl/Logger typed/Kw Throwable String -> nil]))
(t/ann-protocol clojure.tools.logging.impl/LoggerFactory
name
(t/IFn [clojure.tools.logging.impl/LoggerFactory -> String])
get-logger
(t/IFn [clojure.tools.logging.impl/LoggerFactory typed/Any ->
clojure.tools.logging.impl/Logger]))
(ns please-understand-me
(:require [clojure.core.typed :as t]
[clojure.tools.logging :as log])
…)
Other Tools
The REPL, or interactive
evaluation
Tracing/debugging
Schemas and generative
testing
(defspec describe-my-function 1000
(tc/forall [person (sg/generate Person)]
(is (pos? (my-function person)))))
(ns please-understand-me-test
(:require [clojure.test.check :as tc]
[clojure.test.check.clojure-test :refer [defspec]
[schema.experimental.generators :as sg])
…)
(def Birthdate {:year s/Int
:month s/Int
:day s/Int})
(s/defn mock-helper :- Birthdate
[its-complicated :- Person]
(sg/generate Birthdate)
…))
(ns please-understand-me
(:require [schema.core :as s]
[schema.experimental.generators :as sg])
…)
Understanding our code with tests, schemas, and types
Conclusion
“Your ‘reliability’ tools (testing,
refactoring, type systems) don’t
care if program is simple or not”
But they can help you
understand your code
See also
• Jessica Kerr - Contracts asTypes
• Schema for Clojure(Script) Data Shape Declaration and
Validation
• Stuart Sierra -Thinking in Data
• Rich Hickey - Simplicity Matters

More Related Content

PDF
Simple fuzzy name matching in elasticsearch paris meetup
PPTX
PDF
Simple fuzzy Name Matching in Elasticsearch - Graham Morehead
PDF
Kotlin Delegates: Reduce the boilerplate
PPT
Introduction to Ruby, Rails, and Ruby on Rails
PDF
03 introduction to graph databases
PDF
An Introduction to Scala JS
PDF
Learning Rust - experiences from a Python/Javascript developer
Simple fuzzy name matching in elasticsearch paris meetup
Simple fuzzy Name Matching in Elasticsearch - Graham Morehead
Kotlin Delegates: Reduce the boilerplate
Introduction to Ruby, Rails, and Ruby on Rails
03 introduction to graph databases
An Introduction to Scala JS
Learning Rust - experiences from a Python/Javascript developer

What's hot (7)

PDF
2013 lecture-02-syntax shortnewcut
PDF
Ruby presentasjon på NTNU 22 april 2009
PDF
C++ L09-Classes Part2
PDF
(gentle (introduction Clojure))
PDF
Mateusz herych content search problem on android
PPTX
Tries, conversions and field goals
PPT
Groovy presentation
2013 lecture-02-syntax shortnewcut
Ruby presentasjon på NTNU 22 april 2009
C++ L09-Classes Part2
(gentle (introduction Clojure))
Mateusz herych content search problem on android
Tries, conversions and field goals
Groovy presentation
Ad

Similar to Understanding our code with tests, schemas, and types (18)

PDF
Clojure for Java developers - Stockholm
KEY
あたかも自然言語を書くようにコーディングしてみる
PDF
07. haskell Membership
PDF
Type Profiler: Ambitious Type Inference for Ruby 3
PDF
RubyConf Portugal 2014 - Why ruby must go!
PDF
Compiler Construction | Lecture 7 | Type Checking
PDF
Contracts in-clojure-pete
PDF
7li7w devcon5
PDF
Declare Your Language: Name Resolution
PPT
Semantic Analyzer.pptSemantic Analyzerpt
PDF
Contracts in Clojure: Settling Types vs Tests
PDF
Funtional Ruby - Mikhail Bortnyk
PDF
Functional Ruby
PDF
Type Profiler: An Analysis to guess type signatures
PDF
Scala reference
PDF
You are in a maze of deeply nested maps, all alike
PDF
Introduction to clojure
PPTX
Unit 3 Compiler Design Regulation 2021.pptx
Clojure for Java developers - Stockholm
あたかも自然言語を書くようにコーディングしてみる
07. haskell Membership
Type Profiler: Ambitious Type Inference for Ruby 3
RubyConf Portugal 2014 - Why ruby must go!
Compiler Construction | Lecture 7 | Type Checking
Contracts in-clojure-pete
7li7w devcon5
Declare Your Language: Name Resolution
Semantic Analyzer.pptSemantic Analyzerpt
Contracts in Clojure: Settling Types vs Tests
Funtional Ruby - Mikhail Bortnyk
Functional Ruby
Type Profiler: An Analysis to guess type signatures
Scala reference
You are in a maze of deeply nested maps, all alike
Introduction to clojure
Unit 3 Compiler Design Regulation 2021.pptx
Ad

Recently uploaded (20)

PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
top salesforce developer skills in 2025.pdf
PPTX
Essential Infomation Tech presentation.pptx
PPTX
CHAPTER 2 - PM Management and IT Context
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PPTX
L1 - Introduction to python Backend.pptx
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
System and Network Administraation Chapter 3
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
How Creative Agencies Leverage Project Management Software.pdf
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PPTX
Transform Your Business with a Software ERP System
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
How to Choose the Right IT Partner for Your Business in Malaysia
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Which alternative to Crystal Reports is best for small or large businesses.pdf
top salesforce developer skills in 2025.pdf
Essential Infomation Tech presentation.pptx
CHAPTER 2 - PM Management and IT Context
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Odoo POS Development Services by CandidRoot Solutions
Softaken Excel to vCard Converter Software.pdf
Wondershare Filmora 15 Crack With Activation Key [2025
L1 - Introduction to python Backend.pptx
VVF-Customer-Presentation2025-Ver1.9.pptx
System and Network Administraation Chapter 3
Design an Analysis of Algorithms I-SECS-1021-03
How Creative Agencies Leverage Project Management Software.pdf
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Transform Your Business with a Software ERP System

Understanding our code with tests, schemas, and types