exercism/exalysis

Raindrops: handle tightly-coupled solutions

Closed this issue · 2 comments

package raindrops

import (
	"strconv"
)

//Convert turns integers into string based on factor
func Convert(a int) string {

	factor := []int{3, 5, 7}

	three := "Pling"
	five := "Plang"
	seven := "Plong"

	if a%factor[0] == 0 && a%factor[1] == 0 && a%factor[2] == 0 {
		return three + five + seven
	}
	if a%factor[0] == 0 && a%factor[1] == 0 {
		return three + five
	}
	if a%factor[0] == 0 && a%factor[2] == 0 {
		return three + seven
	}
	if a%factor[0] == 0 {
		return three
	}
	if a%factor[1] == 0 && a%factor[2] == 0 {
		return five + seven
	}

	if a%factor[1] == 0 {
		return five
	}
	if a%factor[2] == 0 {
		return seven
	}

	return strconv.Itoa(a)
}

(solution 1270d9243ac843fdbc6599187b506d3a)

That one makes detecting difficult 😞

I could go with the number of return statements... That many return statements usually means somebody went for this kind of solution.

The PR adds this comment to the response:

  • Although this solution passes the tests, it's a little too tightly coupled to the specific cases. If you imagine what changes you'd have to make to add a new case, you can see this code would rapidly become difficult to maintain. Can you find a way to make it more flexible, but still simple? (Hint: you can append to strings using the += operator.)