exercism / go-analyzer

GNU Affero General Public License v3.0
10 stars 12 forks source link

Raindrops: analyzer keeps saying variable should be declared & initialized on oneline #24

Closed kahgoh closed 3 years ago

kahgoh commented 3 years ago

In the raindrops exercise, the analyzer gives the following comment, even though the variable is already declared and initialized on one line:

Rather than declaring the variable result first, and then assigning a value to it afterwards, it is more common to declare and assign in a single statement.

This was the code in the iteration:

package raindrops

import (
    "strconv"
)

// Convert number to raindrops string
func Convert(n int) string {
    result := ""

    if n%3 == 0 {
        result += "Pling"
    }
    if n%5 == 0 {
        result += "Plang"
    }
    if n%7 == 0 {
        result += "Plong"
    }
    if len(result) == 0 {
        return strconv.Itoa(n)
    }
    return result
}

I also tried using var result = "" instead, but still got the same comment from the analyzer.

mhutter commented 3 years ago

I think this is a localization error.

The output from go-analyzer is the following:

{
        "status": "approve",
        "comments": [
                {
                        "comment": "go.general.taking_length_of_string_to_check_empty",
                        "params": {
                                "name": "result"
                        }
                }
        ]
}

When I fix this from the example code given (by using if result == "" instead), the message goes away.

junedev commented 3 years ago

@mhutter Thanks for checking on this. I created a PR with the fix.