DEV Community

Object Calisthenics in Golang

Elton Minetto on June 04, 2019

Jeff Bay introduced the term Object Calisthenics in his book Thought Works Anthology. It is a set of good practices and programming rules that can ...
Collapse
 
grimkey profile image
James White • Edited

Thank you for writing this. I had not encountered Object Calisthenics before and there are some interesting ideas.

I did not find your indention example easy to read. In fact, I only quickly understood it because I had the benefit of previous example. One thing that I feel got missed is Go has a very elegant way of traversing multi-dimensional arrays.

func (b *board) Board() string {
    var buffer = &bytes.Buffer{}

    for x, row := range b.data {
        for y, v:= range row {
            cell := parseCell(x, y, v)
            buffer.WriteString(cell)
        }
        buffer.WriteString("\n")
    }

    return buffer.String()
}

Perhaps a better rule for nesting is that you should nest for only one purpose. Here the nesting is about traversing a board. But if you did any logic in the middle of my loop, that often makes it more difficult to read.

However, if I were manipulating 3-D space, having three nested loops would all share the same purpose. I just don't believe you can achieve the same easy understanding if you split it up into lots of functions.

Collapse
 
inginfgil profile image
Manuel [LTC]

I think is much more readable the 2 nested "for cycle", if you keep going like that the next step it will be the functional programming you can choose scala or java stream, go has to be easy and simple, stop with the rules, just code buddy!

Collapse
 
szymongib profile image
Szymon Gibała

Thank you for the article, interesting read.

I was just wondering why in First class collections have you chosen to use:

type friends struct {
    data []string
}

instead of using type alias like:

type friends []string

It seems to me that it would work fine with the examples.
Are there any advantages of the approach you used?

Collapse
 
eminetto profile image
Elton Minetto

I think there’s no difference at all. Both constructions are valid in this case.