DEV Community

Cover image for My TDD Journey: How It Saved My Go Code (and My Sanity!)
Fred
Fred

Posted on

My TDD Journey: How It Saved My Go Code (and My Sanity!)

What is it Really?

Test Driven Development (TDD) is basically a safety net for your code. It makes sure the code does what you want it to do (intention).
Plus, it helps keep your code clean and organized, so it doesn't turn into a chaotic mess.
I got to talk about TDD with a bunch of cool Go devs at the Gophers Kisumu meetup on 21st September. It was awesome to see so many people interested in it, and we had some great discussions about how it can help you write better code.

How simple is it?

TDD is a cyclic process of three main steps: Red, Green, and Refactor. You start by writing a test for a new feature before writing the code that implements it.

  1. Red: Write a failing test.
  2. Green: Write just enough code to pass the test.
  3. Refactor: Improve the code while keeping all tests passing.

See TDD in action

I came up with an engaging example that the audience could relate to: a robot(functionality) that welcomes attendees.

Image description

😑
First, you write a little test that makes your code throw its hands up and say 'Nope, can't do that yet!'

package main

import "testing" // the testing package is used to write unit tests

// Note on the naming convention TestXxx, and a *testing.T argument. 
func TestWelcome(t *testing.T) {
    robot := NewRobot()
    greeting := robot.Welcome("Alice")
    expected := "Welcome, Alice"

    if greeting != expected {
        t.Errorf("Expected %s but got %s", expected, greeting)
    }
}
Enter fullscreen mode Exit fullscreen mode

The test will fail😞 because the 'Welcome' method does not yet exist. Here, we have created the need for the functionality we’re about to create.

βœ…
Then you write just enough code to make your "red" test turn "green.
Drum rolls!!πŸ₯ We create the Welcome method within our Robot struct:

package main

type Robot struct{}

func NewRobot() *Robot {
    return &Robot{}
}

func (r *Robot) Welcome(name string) string {
    return "Welcome, " + name
}
Enter fullscreen mode Exit fullscreen mode

By running the test again, we see it pass, indicating that your implementation meets the specified requirement.
Now, our test ensures that our code serves its intended purpose.

πŸ€– Refactor
Now for the fun part!😎 You get to clean and refactor your code. But don't forget - all your tests have to keep passing! It's like a coding dance: refactor, test, refactor, test, and so on!

Why you should care about TDD? Let me tell you...

Intent: It makes makes you think clearly about what your code needs to do BEFORE you dive into code.

Better Design: TDD often leads to smaller, more focused functions and classes, as developers must consider how their code interacts with the tests.

Bugs:πŸ› By catching issues early through testing, TDD helps create more reliable software and reduces the likelihood of defects in production.

Let's chat about TDD! What are your thoughts?

Top comments (0)