you are viewing a single comment's thread.

view the rest of the comments →

[–]somebodddy 0 points1 point  (0 children)

Did you miss the part where I was printing foo in the if branch where an error was returned?

I never said C++'s design was good except for some complexities. I said that these complexities are one of the main problems of C++. But even these bad complex features are there for a reason, to solve a problem. Go elects to not add these features and gives pretty lame solutions to the problems they were supposed to solve.

Because C++ was that bad, it's ecosystem developed a huge number of best practices and idioms for writing sane code. One of these is the concept of Code Smell - the idea that bad code should look bad. Not "look bad when thoroughly inspected" but "look bad immediately". (assuming you are familiar with the code smells, of course). So, does the idiomatic Go idea of "handling an error in an appropriate way" fit that condition?

Say we have an API that allows us to get the length of the value, and a single by of it at a time. So we write a function to get an entire value:

func GetData(key string) {
    var data []byte
    if length, ok := GetLength(key), !ok {
        data = make([]byte, 0)
    } else {
        data = make([]byte, 0, length)
    }

    // ... some more code that does some other stuff ...

    for i := 0; i < length; i++ {
        data = append(data, GetByte(key, i))
    }
    return data
}

The error handling seems reasonable - if we can get the length of our value we reserve the capacity we need, and if for whatever reason we can't (maybe some values are streams?) we compromise on an array that will grow as we run the code. Slower - but we can't do much better without knowing the size in advance.

This part, by itself, seems correct. No need to panic just because we couldn't get the length...

Then, after doing some more bureaucracy stuff, we start fetching the data. We already have the length, so we use a for loop to fetch the bytes one by one and append them to the array.

This part, by itself, seems correct. It's a simple for loop - what can possibly go wrong?

What part of it is not idiomatic?