Home » Software Development » TDD Vs. math formalism: friend or foe?

# TDD Vs. math formalism: friend or foe?

It is not uncommon to oppose the empirical process of TDD, together with its heavy use of unit tests, to the more mathematically based techniques, with the »formal methods » and formal verification at the other end of the spectrum. However I experienced again recently that the process of TDD can indeed help discover and draw upon math formalisms well-suited to the problem considered. We then benefit from the math formalism for an easier implementation and correctness.

It is quite frequent that maths structures, or more generally « established formalisms » as Eric Evans would say, are hidden everywhere in the business concepts we need to model in software.

Dates and how we take liberties with them for trading of financial instruments offer a good example of a business concept with an underlying math structure: traders of futures often use a notation like ‘U8? to describe an expiry date like September 2018; ‘U’ means September, and the ’8? digit refers to 2018, but also to 2028, and 2038 etc. Notice that this notation only works for 10 years, and each code is recycled every decade.

The IMM trading floor in the early 70′s (photo CME Group)

In the case of IMM contract codes, we only care about quarterly dates on:

• March (H)
• June (M)
• September (U)
• December (Z)

This yields only 4 possibilities for the month, combined with the 10 possible year digits, hence 40 different codes in total, over the range of 10 years.

## How does that translate into source code?

As a software developer we are asked all the time to manage such IMM expiry codes:

• Sort a given set of IMM contract codes
• Find the next contract from the current « leading month » contract
• Enumerate the next 11 codes from the current « leading month » contract, etc.

This is often done ad hoc with a gazillion of functions for each use-case, leading to thousands of lines of code hard to maintain because they involve parsing of the ‘U8? format everytime we want to calculate something.

With TDD, we can now tackle this topic with more rigor, starting with tests to define what we want to achieve.

The funny thing is that in the process of doing TDD, the cyclic logic of the IMM codes struck me and strongly reminded me of the cyclic group Z/nZ. I had met this strange maths creature at school many years ago, I had a hard time with it by the way. But now on a real example it was definitely more interesting!

The source code (Java) for this post is on Github.

## Draw on established formalisms

Thanks to Google it is easy to find something even with just a vague idea of how it’s named, and thanks to Wikipedia, it is easy to find out more about any established formalism like Cyclic Groups. In particular we find that:

“Every finite cyclic group is isomorphic to the group { [0], [1], [2], …, [n ? 1] } of integers modulo n under addition”

The Wikipedia page also mentions a concept of the product of cyclic groups in relation with their order (here the number of elements). Looks like this is the math-ish way to say that 4 possibilities for quarterly months combined with 10 possible year digits give 40 different codes in total.

So what? Sounds like we could identify the set of the 4 months to a cyclic group, the set of the 10 year digits to another, and that even the combination (product) of both also looks like a cyclic group of order 10 * 4 = 40 (even though the addition operation will not be called like that). So what?

Because we’ve just seen that there is an isomorphism between any finite cyclic group and the cyclic group of integer of the same order, we can just switch to the integer cyclic group logic (plain integers and the modulo operator) to simplify the implementation big time.

Basically the idea is to convert from the IMM code « Z3? to the corresponding ‘ordinal’ integer in the range 0..39, then do every operation on this ‘ordinal’ integer instead of the actual code. Then we can format back to a code « Z3? whenever we really need it.

## Do I still need TDD when I have a complete formal solution?

I must insist that I did not came to this conclusion as easily. The process of TDD was indeed very helpful not to get lost in every possible direction along the way. Even when you have found a formal structure that could solve your problem in one go, even in a « formal proof-ish fashion », then perhaps you need less tests to verify the correctness, but you sure still need tests to think on the specification part of your problem. This is your gentle reminder that TDD is not about unit tests.

## Partial order in a cyclic group

Given a list of IMM codes we often need to sort them for display. The problem is that a cyclic group has no total order, the ordering depends on where you are in time.

Let’s take the example of the days of the week that also forms a cycle: MONDAY, TUESDAY, WEDNESDAY…SUNDAY, MONDAY etc.

If we only care about the future, is MONDAY before WEDNESDAY? Yes, except if we’re on TUESDAY. If we’re on TUESDAY, MONDAY means next MONDAY hence comes after WEDNESDAY, not before.

This is why we cannot unfortunately just implement Comparable to take care of the ordering. Because we need to consider a reference IMM code-aware partial order, we need to resort to a Comparator that takes the reference IMM code in its constructor.

Once we identify that situation to the cyclic group of integers, it becomes easy to shift both operands of the comparison to 0 before comparing them in a safe (total order-ish) way. Again, this trick is made possible by the freedom to experiment given by the TDD tests. As long as we’re still green, we can go ahead and try any funky approach.

## Try it as a kata

This example is also a good coding kata that we’ve tried at work not long ago. Given a simple presentation of the format of an IMM contract code, you can choose to code the sort, find the next and previous code, and perhaps even optimize for memory (cache the instances, e.g. lazily) and speed (cache the toString() value, e.g. in the constructor) if you still have some time.

## In closing

Maths structures are hidden behind many common business concepts. I developed an habit to look for them whenever I can, because they always help make us think, they help question our understanding of the domain problem (« is my domain problem really similar in some way to this structure? »), and of course because they often offer wonderful ready-made implementation hints!

Reference: TDD Vs. math formalism: friend or foe? from our JCG partner Cyrille Martraire at the Cyrille Martraire’s blog blog.

Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....

Be the First to Comment!

Subscribe
Notify of
Want to take your Java skills to the next level?
Here are some of the eBooks you will get:
• Spring Interview QnA
• JPA Minibook
• JVM Troubleshooting Guide
• Java Interview QnA
• Java Design Patterns
The Spring Framework Cookbook
• Learn the best practices of the Spring Framework
• Build simple, portable, fast and flexible JVM-based systems and applications
• Explore specific projects like Boot and Batch
The Spring Data Programming Cookbook
• Learn how to use data access technologies and cloud-based data services
• Set up the environment and create a basic project
• Learn how to handle the various modules (e.g. JPA, MongoDB, Redis etc.)
The Selenium Programming Cookbook
• Kick-start your own projects using this testing framework for web applications
• Learn JUnit integration and Standalone Server functionality
• Find out the most popular Interview Questions about the Selenium Framework
The Mockito Programming Cookbook
• Kick-start your own web projects using this open source testing framework
• Write simple test cases using the Mockito Framework
• Learn how to integrate with JUnit, Maven and other frameworks
The JUnit Programming Cookbook
• Learn basic usage and configuration of JUnit
• Learn how to integrate with other testing frameworks
The JSF 2.0 Programming Cookbook
• Build component-based user interfaces for web applications
• Set up the environment and create a basic project
• Learn Internationalization and Facelets Templates
The Amazon S3 Tutorial
• Develop your own Amazon S3 based applications
• Learn API usage and pricing
• Get your own projects up and running in minimum time
Java Design Patterns
• Learn how Design Patterns are implemented and utilized in Java
• Understand the reasons why patterns are so important
• Learn when and how to apply each one of them
Java Concurrency Essentials
• Dive into the magic of concurrency
• Learn about testing concurrent applications
The IntelliJ IDEA Handbook
• Kick-start your own programming projects using IntelliJ IDEA
• Learn how to setup and install plugins
• Create UIs with this Java integrated development environment
The Git Tutorial
• Learn why Git differs from other version control systems
• Explore Git's usage and best practises
• Learn branching strategies
The Eclipse IDE Handbook
• Explore the most widely used Java IDE
• Learn how to setup and install plugins
• Built your own projects up and running in minimum time
The Docker Containerization Cookbook
• Explore the world’s leading software containerization platform
• Learn how to wrap a piece of software in a complete filesystem
• Learn how to use DNS and various commands
Developing Modern Applications With Scala
• Develop modern Scala applications
• Build SBT and reactive applications
• Learn about testing and database access
The Apache Tomcat Cookbook
• Explore Apache Tomcat open-source web server
• Learn about installation, configuration, logging and clustering
• Kick-start your own web projects using Apache Tomcat
The Apache Maven Cookbook
• Explore the Apache Maven build automation tool
• Learn about Maven's project structure and configuration
• Learn about Maven's dependency management and plug-ins
• Explore the Apache Hadoop open-source software framework
• Learn distributed caching and streaming
The Android Programming Cookbook
• Explore the Android mobile operating system
• Learn about services and page views
The Elasticsearch Tutorial
• Explore the Elasticsearch search engine
• Develop your own Elasticsearch based applications
• Learn operations, Java API Integration and reporting
Amazon DynamoDB Tutorial
• Develop your own Amazon DynamoDB based applications
• Learn DynamoDB Concepts and Best Practices
• Get your own projects up and running in minimum time
Java NIO Programming Cookbook
• Learn features for intensive I/O operations
• Follow a series of tutorials on Java NIO examples
• Get knowledge on Java Nio Socket and Asynchronous Channels
JBoss Drools Cookbook
• Explore Drools business rule management system
• Follow a series of tutorials on Drools examples
• Get knowledge on business rules for a shopping domain model
• Explore Vaadin web framework for rich Internet applications
• Learn the Architecture and Best Practices
• Get knowledge on Data Binding and Custom Components
Groovy Programming Cookbook
• Explore Apache Groovy object-oriented programming language
• Create sample applications and explore interview questions
• Get knowledge on Callback functionality and various widgets
GWT Programming Cookbook
• Explore the open source Google Web Toolkit
• Create sample applications and explore interview questions
• Create and maintain complex JavaScript front-end applications in Java
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
and many more ....
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
and many more ....
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
Want to be a DynamoDB Master ?
In order to help you master this Amazon NoSQL database service, we have compiled a kick-ass guide with all the major DynamoDB features and use cases! Besides studying them online you may download the eBook in PDF format!
Programming Interview Coming Up?
In order to get you prepared for your next Programming Interview, we have compiled a huge list of relevant Questions and their respective Answers. Besides studying them online you may download the eBook in PDF format!
Java Interview Coming Up?
In order to get you prepared for your next Java Interview, we have compiled a huge list of relevant Questions and their respective Answers. Besides studying them online you may download the eBook in PDF format!
Java Interview Coming Up?
In order to get you prepared for your next Java Interview, we have compiled a huge list of relevant Questions and their respective Answers. Besides studying them online you may download the eBook in PDF format!
Want to be a Java NIO Master ?
In order to help you master Java NIO Library, we have compiled a kick-ass guide with all the major Java NIO features and use cases! Besides studying them online you may download the eBook in PDF format!
Want to be a Drools Master ?
In order to help you master Drools Business Rule Management System, we have compiled a kick-ass guide with all the major Drools features and use cases! Besides studying them online you may download the eBook in PDF format!
Want to be an iText Master ?
In order to help you master iText Library, we have compiled a kick-ass guide with all the major iText features and use cases! Besides studying them online you may download the eBook in PDF format!
Want to be an Elasticsearch Master ?
In order to help you master Elasticsearch search engine, we have compiled a kick-ass guide with all the major Elasticsearch features and use cases! Besides studying them online you may download the eBook in PDF format!
Want to be a Scala Master ?
In order to help you master Scala, we have compiled a kick-ass guide with all the basic concepts! Besides studying them online you may download the eBook in PDF format!
Want to be a JUnit Master ?
In order to help you master unit testing with JUnit, we have compiled a kick-ass guide with all the major JUnit features and use cases! Besides studying them online you may download the eBook in PDF format!
Want to master Amazon Web Services ?
In order to help you master the leading Web Services platform, we have compiled a kick-ass guide with all its major features and use cases! Besides studying them online you may download the eBook in PDF format!
Want to master Spring Framework ?
In order to help you master the leading and innovative Java framework, we have compiled a kick-ass guide with all its major features and use cases! Besides studying them online you may download the eBook in PDF format!
Want to master Eclipse IDE ?
In order to help you master Eclipse, we have compiled a kick-ass guide with all the basic features of the popular IDE! Besides studying them online you may download the eBook in PDF format!
Want to master IntelliJ IDEA ?
In order to help you master IntelliJ IDEA, we have compiled a kick-ass guide with all the basic features of the popular IDE! Besides studying them online you may download the eBook in PDF format!
Want to master Docker ?
In order to help you master Docker, we have compiled a kick-ass guide with all the basic concepts of the Docker container system! Besides studying them online you may download the eBook in PDF format!
Want to create a kick-ass Android App ?
With this book, you will delve into the fundamentals of Android programming. You will understand user input, views and layouts. Furthermore, you will learn how to communicate over Bluetooth and also leverage Google Maps into your application!
Want to be a GIT Master ?
In order to help you master GIT, we have compiled a kick-ass guide with all the basic concepts of the GIT version control system! Besides studying them online you may download the eBook in PDF format!
Want to be a Hadoop Master ?
In order to help you master Apache Hadoop, we have compiled a kick-ass guide with all the basic concepts of a Hadoop cluster! Besides studying them online you may download the eBook in PDF format!
Want to master Spring Data ?
In order to help you master Spring Data, we have compiled a kick-ass guide with all the major features and use cases! Besides studying them online you may download the eBook in PDF format!
Want to create a kick-ass Android App ?
With this book, you will delve into the fundamentals of Android UI design. You will understand user input, views and layouts, as well as adapters and fragments. Furthermore, you will learn how to add multimedia to an app and also leverage themes and styles!
Want to be a Java 8 Ninja ?
In order to get you up to speed with the major Java 8 release, we have compiled a kick-ass guide with all the new features and goodies! Besides studying them online you may download the eBook in PDF format!
Want to master Java Annotations ?
In order to help you master the topic of Annotations, we have compiled a kick-ass guide with all the major features and use cases! Besides studying them online you may download the eBook in PDF format!
Want to be a JUnit Master ?
In order to help you master unit testing with JUnit, we have compiled a kick-ass guide with all the major JUnit features and use cases! Besides studying them online you may download the eBook in PDF format!
Want to master Java Abstraction ?
In order to help you master the topic of Abstraction, we have compiled a kick-ass guide with all the major features and use cases! Besides studying them online you may download the eBook in PDF format!
Want to master Java Reflection ?
In order to help you master the topic of Reflection, we have compiled a kick-ass guide with all the major features and use cases! Besides studying them online you may download the eBook in PDF format!
Want to be a JMeter Master ?
In order to help you master load testing with JMeter, we have compiled a kick-ass guide with all the major JMeter features and use cases! Besides studying them online you may download the eBook in PDF format!
Want to be a Servlets Master ?
In order to help you master programming with Java Servlets, we have compiled a kick-ass guide with all the major servlet API uses and showcases! Besides studying them online you may download the eBook in PDF format!
Want to be a JAXB Master ?
In order to help you master XML Binding with JAXB, we have compiled a kick-ass guide with all the major JAXB features and use cases! Besides studying them online you may download the eBook in PDF format!
Want to be a JDBC Master ?
In order to help you master database programming with JDBC, we have compiled a kick-ass guide with all the major JDBC features and use cases! Besides studying them online you may download the eBook in PDF format!
Want to be a JPA Master ?
In order to help you master programming with JPA, we have compiled a kick-ass guide with all the major JPA features and use cases! Besides studying them online you may download the eBook in PDF format!
Want to be a Hibernate Master ?
In order to help you master JPA and database programming with Hibernate, we have compiled a kick-ass guide with all the major Hibernate features and use cases! Besides studying them online you may download the eBook in PDF format!
Want to be a JSF Master ?
Want to be a Java Master ?
In order to help you master the Java programming language, we have compiled a kick-ass guide with all the must-know advanced Java features! Besides studying them online you may download the eBook in PDF format!
Want to be a Java Master ?
In order to help you master the Java programming language, we have compiled a kick-ass guide with all the must-know Design Patterns for Java! Besides studying them online you may download the eBook in PDF format!
Want to be a Hadoop Master ?
In order to help you master Apache Hadoop, we have compiled a kick-ass guide with all the basic concepts of a Hadoop cluster! Besides studying them online you may download the eBook in PDF format!
Want to be an Elastic Beanstalk Master ?
In order to help you master AWS Elastic Beanstalk, we have compiled a kick-ass guide with all the major Elastic Beanstalk features and use cases! Besides studying them online you may download the eBook in PDF format!
Amazon Elastic Beanstalk Tutorial
• Develop your own Amazon Elastic Beanstalk based applications
• Learn Java Integration and Command Line Interfacing
• Get your own projects up and running in minimum time
Want to take your Java skills to the next level?
Here are some of the eBooks you will get:
• Spring Interview QnA
• JPA Minibook
• JVM Troubleshooting Guide