Chapter 12 Writing Tests and Code Quality
Genetic Algorithms in Elixir — by Sean Moriarity (87 / 101)
👈 What You Learned | TOC | Understanding Randomness 👉
In the last chapter, you learned how to optimize your framework in three different ways to get the most performance out of Elixir and the BEAM. So far, you’ve created a framework for writing genetic algorithms capable of solving a wide variety of problems. One thing you haven’t done, however, is test and analyze this framework to ensure your code is correct and clean.
Testing is a crucial part of any development process. Typically, you’d want to write tests that mirror a specification or some behavior first, before writing any code. This process, called test driven development, calls for writing small unit tests first and then improving code to mirror a specification. Elixir emphasizes the importance of tests and makes the process of writing unit tests a breeze.
In addition to testing, another key aspect of the development process is ensuring your code is concise and understandable for yourself and any other developers that may be contributing to your project. Fortunately, Elixir has a few packages that make analyzing your code easy, such as credo[22] and dialyxir.[23]
In this chapter, you’ll learn how to use ExUnit[24] and StreamData[25] to write property-based tests for your code. You’ll also learn how to use credo and dialyxir to improve the code in your framework with some code analysis. Normally, you’d want to use these tools much earlier in the development process; however, I’ve omitted them until this point to place more emphasis on the core concepts of genetic algorithms. This chapter will get you on track with using these tools to improve upon your existing code base.
👈 What You Learned | TOC | Understanding Randomness 👉
Genetic Algorithms in Elixir by Sean Moriarity can be purchased in other book formats directly from the Pragmatic Programmers. If you notice a code error or formatting mistake, please let us know here so that we can fix it.

