Skip to content

Commit

Permalink
Use parameter type instead of named result parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
kunalkapadia committed Mar 21, 2018
1 parent 6359ce7 commit 7567e6b
Show file tree
Hide file tree
Showing 23 changed files with 116 additions and 89 deletions.
40 changes: 23 additions & 17 deletions arrays/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ By running `go test` the compiler will fail with `./sum_test.go:10:15: undefined
In `sum.go`

```go
func Sum(numbers [5]int) (sum int) {
return
func Sum(numbers [5]int) int {
return 0
}
```

Expand All @@ -48,11 +48,12 @@ Your test should now fail with _a clear error message_
## Write enough code to make it pass

```go
func Sum(numbers [5]int) (sum int) {
func Sum(numbers [5]int) int {
sum := 0
for i := 0; i < 5; i++ {
sum += numbers[i]
}
return
return sum
}
```

Expand All @@ -69,11 +70,12 @@ I _wouldn't_ push to master though, because I plan to refactor next. It is nice
Let's introduce `range` to help clean up our code

```go
func Sum(numbers [5]int) (sum int) {
func Sum(numbers [5]int) int {
sum := 0
for _, number := range numbers {
sum += number
}
return
return sum
}
```

Expand Down Expand Up @@ -142,11 +144,12 @@ The problem here is we can either
In our case, no-one else is using our function so rather than having two functions to maintain let's just have one.

```go
func Sum(numbers []int) (sum int) {
func Sum(numbers []int) int {
sum := 0
for _, number := range numbers {
sum += number
}
return
return sum
}
```

Expand Down Expand Up @@ -294,15 +297,15 @@ Change the test back again and run it, you should have test output looking like
What we need to do is iterate over the varargs, calculate the sum using our `Sum` function from before and then add it to the slice we will return

```go
func SumAll(numbersToSum ...[]int) (sums []int) {
func SumAll(numbersToSum ...[]int) []int {
lengthOfNumbers := len(numbersToSum)
sums = make([]int, lengthOfNumbers)
sums := make([]int, lengthOfNumbers)

for i, numbers := range numbersToSum {
sums[i] = Sum(numbers)
}

return
return sums
}
```

Expand All @@ -321,12 +324,13 @@ As mentioned, slices have a capacity. If you have a slice with a capacity of 2 a
However you can use the `append` function which takes a slice and a new value, returning a new slice with all the items in it.

```go
func SumAll(numbersToSum ...[]int) (sums []int) {
func SumAll(numbersToSum ...[]int) []int {
var sums []int
for _, numbers := range numbersToSum {
sums = append(sums, Sum(numbers))
}

return
return sums
}
```

Expand Down Expand Up @@ -360,13 +364,14 @@ Rename the function to `SumAllTails` and re-run the test
## Write enough code to make it pass

```go
func SumAllTails(numbersToSum ...[]int) (sums []int) {
func SumAllTails(numbersToSum ...[]int) []int {
var sums []int
for _, numbers := range numbersToSum {
tail := numbers[1:]
sums = append(sums, Sum(tail))
}

return
return sums
}
```

Expand Down Expand Up @@ -416,7 +421,8 @@ Oh no! It's important to note the test _has compiled_, it is a runtime error. Co
## Write enough code to make it pass

```go
func SumAllTails(numbersToSum ...[]int) (sums []int) {
func SumAllTails(numbersToSum ...[]int) []int {
var sums []int
for _, numbers := range numbersToSum {
if len(numbers) == 0 {
sums = append(sums, 0)
Expand All @@ -426,7 +432,7 @@ func SumAllTails(numbersToSum ...[]int) (sums []int) {
}
}

return
return sums
}
```

Expand Down
5 changes: 3 additions & 2 deletions arrays/v1/sum.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package main

func Sum(numbers [5]int) (sum int) {
func Sum(numbers [5]int) int {
sum := 0
for i := 0; i < 5; i++ {
sum += numbers[i]
}
return
return sum
}
5 changes: 3 additions & 2 deletions arrays/v2/sum.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package main

func Sum(numbers [5]int) (sum int) {
func Sum(numbers [5]int) int {
sum := 0
for _, number := range numbers {
sum += number
}
return
return sum
}
5 changes: 3 additions & 2 deletions arrays/v3/sum.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package main

func Sum(numbers []int) (sum int) {
func Sum(numbers []int) int {
sum := 0
for _, number := range numbers {
sum += number
}
return
return sum
}
12 changes: 7 additions & 5 deletions arrays/v4/sum.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
package main

func Sum(numbers []int) (sum int) {
func Sum(numbers []int) int {
sum := 0
for _, number := range numbers {
sum += number
}
return
return sum
}

func SumAll(numbersToSum ...[]int) (sums []int) {

func SumAll(numbersToSum ...[]int) []int {
lengthOfNumbers := len(numbersToSum)
sums = make([]int, lengthOfNumbers)
sums := make([]int, lengthOfNumbers)

for i, numbers := range numbersToSum {
sums[i] = Sum(numbers)
}

return
return sums
}
10 changes: 6 additions & 4 deletions arrays/v5/sum.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package main

func Sum(numbers []int) (sum int) {
func Sum(numbers []int) int {
sum := 0
for _, number := range numbers {
sum += number
}
return
return sum
}

func SumAll(numbersToSum ...[]int) (sums []int) {
func SumAll(numbersToSum ...[]int) []int {
var sums []int
for _, numbers := range numbersToSum {
sums = append(sums, Sum(numbers))
}

return
return sums
}
10 changes: 6 additions & 4 deletions arrays/v6/sum.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package main

func Sum(numbers []int) (sum int) {
func Sum(numbers []int) int {
sum := 0
for _, number := range numbers {
sum += number
}
return
return sum
}

func SumAllTails(numbersToSum ...[]int) (sums []int) {
func SumAllTails(numbersToSum ...[]int) []int {
var sums []int
for _, numbers := range numbersToSum {
tail := numbers[1:]
sums = append(sums, Sum(tail))
}

return
return sums
}
11 changes: 7 additions & 4 deletions arrays/v7/sum.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package main

func Sum(numbers []int) (sum int) {
func Sum(numbers []int) int {
sum := 0
for _, number := range numbers {
sum += number
}
return
return sum
}

func SumAllTails(numbersToSum ...[]int) (sums []int) {

func SumAllTails(numbersToSum ...[]int) []int {
var sums []int
for _, numbers := range numbersToSum {
if len(numbers) == 0 {
sums = append(sums, 0)
Expand All @@ -17,5 +20,5 @@ func SumAllTails(numbersToSum ...[]int) (sums []int) {
}
}

return
return sums
}
12 changes: 7 additions & 5 deletions for/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ All you need to do right now is enough to make it compile so you can check your
```go
package main

func Repeat(character string) (repeated string) {
func Repeat(character string) string {
return
}
```
Expand All @@ -50,11 +50,12 @@ Isn't it nice to know you already know enough Go to write tests for some basic p
The `for` syntax is very unremarkable and follows most C-like languages.

```go
func Repeat(character string) (repeated string) {
func Repeat(character string) string {
var repeated string
for i := 0; i < 5; i++ {
repeated = repeated + character
}
return
return repeated
}
```

Expand All @@ -67,11 +68,12 @@ Now it's time to refactor and introduce another construct `+=`
```go
const repeatCount = 5

func Repeat(character string) (repeated string) {
func Repeat(character string) string {
var repeated string
for i := 0; i < repeatCount; i++ {
repeated += character
}
return
return repeated
}
```

Expand Down
5 changes: 3 additions & 2 deletions for/v1/repeat.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package main

func Repeat(character string) (repeated string) {
func Repeat(character string) string {
var repeated string
for i := 0; i < 5; i++ {
repeated = repeated + character
}
return
return repeated
}
5 changes: 3 additions & 2 deletions for/v2/repeat.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package main

const repeatCount = 5

func Repeat(character string) (repeated string) {
func Repeat(character string) string {
var repeated string
for i := 0; i < repeatCount; i++ {
repeated += character
}
return
return repeated
}
5 changes: 3 additions & 2 deletions for/vx/repeat.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package main

const repeatCount = 5

func Repeat(character string) (repeated string) {
func Repeat(character string) string {
var repeated string
for i := 0; i < repeatCount; i++ {
repeated += character
}
return
return repeated
}
14 changes: 9 additions & 5 deletions integers/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Inspect the compilation error
Write enough code to satisfy the compiler *and that's all* - remember we want to check that our tests fail for the correct reason.

```go
func Add(x, y int) (sum int) {
func Add(x, y int) int {
return
}
```
Expand All @@ -45,12 +45,16 @@ 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'`

If you have noticed we learnt about _named return value_ in the [last](/hello-world/readme.md#one...last...refactor?) section but aren't using the same here.
It should generally be used when the meaning of the result isn't clear from context, in our case it's pretty much clear that `Add` function will add the parameters.
You can refer [this](https://github.com/golang/go/wiki/CodeReviewComments#named-result-parameters) wiki for more details.

## 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

```go
func Add(x, y int) (sum int) {
func Add(x, y int) int {
return 4
}
```
Expand All @@ -64,7 +68,7 @@ Once we're more familiar with Go's syntax I will introduce a technique called Pr
For now, let's fix it properly

```go
func Add(x, y int) (sum int) {
func Add(x, y int) int {
return x + y
}
```
Expand All @@ -83,7 +87,7 @@ You can add documentation to functions with comments, and these will appear in G

```go
// Add takes two integers and returns the sum of them
func Add(x, y int) (sum int) {
func Add(x, y int) int {
return x + y
}
```
Expand Down Expand Up @@ -115,4 +119,4 @@ What we have covered:
- More practice of the TDD workflow
- Integers, addition
- Writing better documentation so users of our code can understand its usage quickly
- Examples of how to use our code, which are checked as part of our tests
- Examples of how to use our code, which are checked as part of our tests
2 changes: 1 addition & 1 deletion integers/v1/adder.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package main

func Add(x, y int) (sum int) {
func Add(x, y int) int {
return x + y
}
Loading

0 comments on commit 7567e6b

Please sign in to comment.