diff --git a/arrays/readme.md b/arrays/readme.md index cc8d38a46..a7f35165b 100644 --- a/arrays/readme.md +++ b/arrays/readme.md @@ -6,7 +6,7 @@ When you have an array, it is very common to have to iterate over them so let's Let's use our TDD skills -## 1. Write the test first +## Write the test first In `sum_test.go` ```go @@ -27,11 +27,11 @@ Arrays have a _fixed capacity_ which you define when you declare the variable. I [Read more about the format strings](https://golang.org/pkg/fmt/) -## 2. Try and run the test +## Try and run the test By running `go test` the compiler will fail with `./sum_test.go:10:15: undefined: Sum` -## 3. Write the minimal amount of code for the test to run and check the failing test output +## Write the minimal amount of code for the test to run and check the failing test output In `sum.go` @@ -45,7 +45,7 @@ Your test should now fail with _a clear error message_ `sum_test.go:13: expected the sum to be 15 but was 0 given, [1 2 3 4 5]` -## 4. Write enough code to make it pass +## Write enough code to make it pass ```go func Sum(numbers [5]int) (sum int) { @@ -64,7 +64,7 @@ At this point if you are using source control (which you should!) I would `commi I _wouldnt_ push to master though, because I plan to refactor next. It is nice to commit at this point in case you somehow get in to a mess with refactoring - you can always go back to the working version. -## 5. Refactor +## Refactor Let's introduce `range` to help clean up our code @@ -77,4 +77,10 @@ func Sum(numbers [5]int) (sum int) { } ``` -`range` lets you iterate over an array. Every time it is called it returns two values, the index and the value. We are choosing to ignore the index value by using `_` \ No newline at end of file +`range` lets you iterate over an array. Every time it is called it returns two values, the index and the value. We are choosing to ignore the index value by using `_` + +## Wrapping up + +You may be thinking it's quite cumbersome that arrays are fixed length and most of the time you probably wont be using them! Go has _slices_ which are dynamic in size and most of the time you will probably be using them instead. + +An interesting property of arrays though is the size is encoded in its type. If you try and pass an `[4]int` into a function that expects `[5]int`, it wont compile \ No newline at end of file diff --git a/for/readme.md b/for/readme.md index c3a4f94af..6c72dd711 100644 --- a/for/readme.md +++ b/for/readme.md @@ -23,11 +23,11 @@ func TestRepeat(t *testing.T) { } ``` -## 2. Try and run the test +## Try and run the test `./repeat_test.go:6:14: undefined: Repeat` -## 3. Write the minimal amount of code for the test to run and check the failing test output +## Write the minimal amount of code for the test to run and check the failing test output _Keep the discipline!_ You don't need to know anything new right now to make the test fail properly. @@ -45,7 +45,7 @@ Isn't it nice to know you already know enough Go to write tests for some basic p `repeat_test.go:10: expected 'aaaaa' but got ''` -## 4. Write enough code to make it pass +## Write enough code to make it pass The `for` syntax is very unremarkable and follows most C-like languages. @@ -60,7 +60,7 @@ func Repeat(character string) (repeated string) { Run the test and it should pass. -## 5. Refactor +## Refactor Now it's time to refactor and introduce another construct `+=` diff --git a/integers/readme.md b/integers/readme.md index 4683b20d4..91a9993b7 100644 --- a/integers/readme.md +++ b/integers/readme.md @@ -21,7 +21,7 @@ func TestAdder(t *testing.T) { You will notice that we're using `%d` as our format strings rather than `%s`. That's because we want it to print an integer rather than a string. -## 2. Try and run the test +## Try and run the test Run the test `go test` @@ -29,7 +29,7 @@ Inspect the compilation error `./adder_test.go:6:9: undefined: Add` -## 3. Write the minimal amount of code for the test to run and check the failing test output +## Write the minimal amount of code for the test to run and check the failing test output Write enough code to satisfy the compiler *and that's all* - remember we want to check that our tests fail for the correct reason. @@ -45,7 +45,7 @@ Now run the tests and we should be happy that the test is correctly reporting wh `adder_test.go:10: expected '4' but got '0'` -## 4. Write enough code to make it pass +## Write enough code to make it pass In the strictest sense of TDD we should now write the _minimal amount of code to make the test pass_. A pedantic programmer may do this @@ -71,7 +71,7 @@ func Add(x, y int) (sum int) { If you re-run the tests they should pass. -## 5. Refactor +## Refactor There's not a lot in the _actual_ code we can really improve on here. diff --git a/template.md b/template.md index eed021c3d..6318a6655 100644 --- a/template.md +++ b/template.md @@ -2,10 +2,10 @@ Some intro -## 1. Write the test first -## 2. Try and run the test -## 3. Write the minimal amount of code for the test to run and check the failing test output -## 4. Write enough code to make it pass -## 5. Refactor -## 6. Repeat for new requirements -## 7. Wrap-up \ No newline at end of file +## Write the test first +## Try and run the test +## Write the minimal amount of code for the test to run and check the failing test output +## Write enough code to make it pass +## Refactor +## Repeat for new requirements +## Wrapping up \ No newline at end of file