Simple TDD in Julia and C++
I started off with python and after running into limitations, I’ve skipped around and worked on projects in Julia, Go, and Rust. Recently…
I started off with python and after running into limitations, I’ve skipped around and worked on projects in Julia, Go, and Rust. Recently, I’ve been taking a C++ course at the Saylor Acadamy. Along with studying all of these different compiled languages, I’ve read several programming books such as “Becoming a Better Programmer”, by Pete Goodliffe and also “The Coding Dojo Handbook”, by Emily Bache. Over the course of this last year, I’ve learned alot, not only about syntax, but structure, mindset, patience, and discipline.
I’d like to start off talking about the things I learned from the two books mentioned above. While these books do not teach you to code, they teach you to think not only about the code you will write in the future, but code you’ve already written. Both of these programmers put a very strong emphasis on TDD (test driven development). The main idea behind this concept:
Before writing a program, write one or two tests. Write only enough code to pass the tests. After passing them, refactor as needed and write another test.
These two authors also put a very strong emphasis on reflection. It is of utmost importance that we look at and refactor our old code so we can learn from our mistakes and gain a greater understanding of ourselves as developers. In the future, I’ll be doing this with my own code and publishing here on medium. Without much further ado, lets begin with an example using the classic “Hello World” program.
The C++ Example
In pretty much any folder you feel like, create a “hello.cpp” file. We’ll begin by just adding our basic dependencies as we do with anything.
Now it’s time to add our first test, remember it must be a failing test.
Run it and…
Wait, how can our program fail this test? If you look closer, you’ll notice, we didn’t add the library to include our testing function. Oh the irony… Let’s change that.
Run it…
Alright, some progress, baby steps I guess.
Now let’s add a real test.
This test should produce an error like this.
Great, we’ve failed the test, now lets add the code to make it pass. We’ll start by adding a function to print the string “Hello World!”. First, we need to add the string class.
Now we can define our function. Define it however you want, here’s mine.
Run the program and it should pass.
But wait, we didn’t print to the console, just for the sake of finishing, Let’s call our function inside of main and then run it.
Success.
Let’s Do this Same thing in Julia
In Julia, we will use the assert macro.
Run it
Run it
Lets define our function
Run it
Time to make it print.
Run it
And there you have it.






















