Skip to content

Commit

Permalink
little edits
Browse files Browse the repository at this point in the history
  • Loading branch information
quii committed Mar 10, 2018
1 parent 0a5529c commit fc349ff
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 21 deletions.
18 changes: 12 additions & 6 deletions arrays/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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`

Expand All @@ -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) {
Expand All @@ -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

Expand All @@ -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 `_`
`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
8 changes: 4 additions & 4 deletions for/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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.

Expand All @@ -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 `+=`

Expand Down
8 changes: 4 additions & 4 deletions integers/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ 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`

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.

Expand All @@ -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

Expand All @@ -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.

Expand Down
14 changes: 7 additions & 7 deletions template.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
## 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

0 comments on commit fc349ff

Please sign in to comment.