chapter four

4 Tidying up

 

This chapter covers

  • Writing testable examples
  • Producing executable documentation
  • Measuring test coverage and benchmarking
  • Refactoring the URL parser
  • Differences between external and internal tests

In this chapter, I'll teach you how to generate automatic and runnable documentation from code. I will show you how you can provide testable examples that never go out of date.

You'll learn about how to generate test coverage for the url package. I'll show you how to benchmark your code and give you a few tips about improving its performance.

Lastly, you'll learn to refactor the url package to make it more maintainable with the new knowledge you'll be acquiring in this chapter.

This is the last chapter of Part 1, and it's time to tidy up some left-overs from the previous chapters. Ready? Let's get started!

4.1 Testable Examples

These people again! The Wizards team, from the previous chapters. Oh my goodness. They're growing, and new people joined the team, and the existing team members got sick of explaining how the url package works to the new members. They ask you to provide sample code on using the package so the novice wizards can look at that instead of asking The Mighty Wizards.

4.1.1 Writing a testable example

4.1.2 Self-Printing URLs

4.1.3 Runnable examples

4.1.4 Wrap up

4.2 Test coverage

4.2.1 Measuring test coverage

4.2.2 Test coverage != Bug-free

4.2.3 Wrap up

4.3 Benchmarks

4.3.1 Optimizing the String method

4.3.2 Proper benchmarking

4.3.3 Comparing benchmarks

4.3.4 Wrap up

4.4 Refactoring

4.5 External tests

4.5.1 Internal vs. external tests

4.5.2 Testing unexported identifiers

4.5.3 Wrap up

4.6 Summary