Skip to main content
Code Review

Return to Answer

deleted 1 character in body
Source Link
Ted
  • 701
  • 3
  • 12
  • Handle errors. In Go, foo, _ := something() is a big code smell.

  • Iterating on a string gives you a rune; don't convert it to a one-char string just to compare it to another one-char string. Compare it to another rune instead ('a' instead of "a").

  • Your functions isVowel and isConsonant are re-building the vowel/consonant map from scratch every time they're called! This is very inefficient: initialize both maps as package variables, and inline these two functions completely. You have two options to do so, I like the second one better because it's slightly more readable.

    Option 1:

     var (
     vowels = map[rune]bool{
     'a': true,
     'e': true,
     'i': true,
     'o': true,
     'u': true,
     }
     consonants = map[rune]bool{
     // ...
     }
     )
    

    Option 2:

     var (
     vowels = toSet('a', 'e', 'i', 'o', 'u')
     consonants = toSet(/* ... */)
     )
     func toSet(chars ...rune) map[rune]bool {
     m := make(map[rune]bool, len(chars))
     for _, c := range chars {
     m[c] = true
     }
     return m
     }
    

    You then check if a rune r is a vowelsvowel by just calling vowels[r].

  • Your s = fmt.Sprintf("%c", x) statement looks a lot like printf debugging. Were you intending to keep it in your code?

  • countVowels and countConsonants can be inlined as well, and called on each char in the same loop like in Curtis' answer.

  • Replace foo += 1 by foo++.

  • I'd say you have needless blank lines in your code (I think each of your functions is simple enough not to have any), but that's a minor style thing.

  • Your code doesn't support non-ASCII encodings (and it probably should, UTF-8 is everywhere nowadays). It doesn't support uppercase letters, either =)

  • Handle errors. In Go, foo, _ := something() is a big code smell.

  • Iterating on a string gives you a rune; don't convert it to a one-char string just to compare it to another one-char string. Compare it to another rune instead ('a' instead of "a").

  • Your functions isVowel and isConsonant are re-building the vowel/consonant map from scratch every time they're called! This is very inefficient: initialize both maps as package variables, and inline these two functions completely. You have two options to do so, I like the second one better because it's slightly more readable.

    Option 1:

     var (
     vowels = map[rune]bool{
     'a': true,
     'e': true,
     'i': true,
     'o': true,
     'u': true,
     }
     consonants = map[rune]bool{
     // ...
     }
     )
    

    Option 2:

     var (
     vowels = toSet('a', 'e', 'i', 'o', 'u')
     consonants = toSet(/* ... */)
     )
     func toSet(chars ...rune) map[rune]bool {
     m := make(map[rune]bool, len(chars))
     for _, c := range chars {
     m[c] = true
     }
     return m
     }
    

    You then check if a rune r is a vowels by just calling vowels[r].

  • Your s = fmt.Sprintf("%c", x) statement looks a lot like printf debugging. Were you intending to keep it in your code?

  • countVowels and countConsonants can be inlined as well, and called on each char in the same loop like in Curtis' answer.

  • Replace foo += 1 by foo++.

  • I'd say you have needless blank lines in your code (I think each of your functions is simple enough not to have any), but that's a minor style thing.

  • Your code doesn't support non-ASCII encodings (and it probably should, UTF-8 is everywhere nowadays). It doesn't support uppercase letters, either =)

  • Handle errors. In Go, foo, _ := something() is a big code smell.

  • Iterating on a string gives you a rune; don't convert it to a one-char string just to compare it to another one-char string. Compare it to another rune instead ('a' instead of "a").

  • Your functions isVowel and isConsonant are re-building the vowel/consonant map from scratch every time they're called! This is very inefficient: initialize both maps as package variables, and inline these two functions completely. You have two options to do so, I like the second one better because it's slightly more readable.

    Option 1:

     var (
     vowels = map[rune]bool{
     'a': true,
     'e': true,
     'i': true,
     'o': true,
     'u': true,
     }
     consonants = map[rune]bool{
     // ...
     }
     )
    

    Option 2:

     var (
     vowels = toSet('a', 'e', 'i', 'o', 'u')
     consonants = toSet(/* ... */)
     )
     func toSet(chars ...rune) map[rune]bool {
     m := make(map[rune]bool, len(chars))
     for _, c := range chars {
     m[c] = true
     }
     return m
     }
    

    You then check if a rune r is a vowel by just calling vowels[r].

  • Your s = fmt.Sprintf("%c", x) statement looks a lot like printf debugging. Were you intending to keep it in your code?

  • countVowels and countConsonants can be inlined as well, and called on each char in the same loop like in Curtis' answer.

  • Replace foo += 1 by foo++.

  • I'd say you have needless blank lines in your code (I think each of your functions is simple enough not to have any), but that's a minor style thing.

  • Your code doesn't support non-ASCII encodings (and it probably should, UTF-8 is everywhere nowadays). It doesn't support uppercase letters, either =)

added 153 characters in body
Source Link
Ted
  • 701
  • 3
  • 12
  • Handle errors. In Go, foo, _ := something() is a big code smell.

  • Iterating on a string gives you a rune; don't convert it to a one-char string just to compare it to another one-char string. Compare it to another rune instead ('a' instead of "a").

  • Your functions isVowel and isConsonant are re-building the vowel/consonant map from scratch every time they're called! This is very inefficient: initialize both maps as package variables, and inline these two functions completely. You have two options to do so, I like the second one better because it's slightly more readable.

    Option 1:

     var (
     vowels = map[rune]bool{
     'a': true,
     'e': true,
     'i': true,
     'o': true,
     'u': true,
     }
     consonants = map[rune]bool{
     // ...
     }
     )
    

    Option 2:

     var (
     vowels = toSet('a', 'e', 'i', 'o', 'u')
     consonants = toSet(/* ... */)
     )
     func toSet(chars ...rune) map[rune]bool {
     m := make(map[rune]bool, len(chars))
     for _, c := range chars {
     m[c] = true
     }
     return m
     }
    

    You then check if a rune r is a vowels by just calling vowels[r].

  • Your s = fmt.Sprintf("%c", x) statement looks a lot like printf debugging. Were you intending to keep it in your code?

  • countVowels and countConsonants can be inlined as well, and called on each char in the same loop like in Curtis' answer.

  • Replace foo += 1 by foo++.

  • I'd say you have needless blank lines in your code (I think each of your functions is simple enough not to have any), but that's a minor style thing.

  • Your code doesn't support non-ASCII encodings (and it probably should, UTF-8 is everywhere nowadays). It doesn't support uppercase letters, either =)

  • Handle errors. In Go, foo, _ := something() is a big code smell.

  • Iterating on a string gives you a rune; don't convert it to a one-char string just to compare it to another one-char string. Compare it to another rune instead ('a' instead of "a").

  • Your functions isVowel and isConsonant are re-building the vowel/consonant map from scratch every time they're called! This is very inefficient: initialize both maps as package variables, and inline these two functions completely. You have two options to do so, I like the second one better because it's slightly more readable.

    Option 1:

     var (
     vowels = map[rune]bool{
     'a': true,
     'e': true,
     'i': true,
     'o': true,
     'u': true,
     }
     consonants = map[rune]bool{
     // ...
     }
     )
    

    Option 2:

     var (
     vowels = toSet('a', 'e', 'i', 'o', 'u')
     consonants = toSet(/* ... */)
     )
     func toSet(chars ...rune) map[rune]bool {
     m := make(map[rune]bool, len(chars))
     for _, c := range chars {
     m[c] = true
     }
     return m
     }
    

    You then check if a rune r is a vowels by just calling vowels[r].

  • Your s = fmt.Sprintf("%c", x) statement looks a lot like printf debugging. Were you intending to keep it in your code?

  • countVowels and countConsonants can be inlined as well, and called on each char in the same loop like in Curtis' answer.

  • Replace foo += 1 by foo++.

  • I'd say you have needless blank lines in your code (I think each of your functions is simple enough not to have any), but that's a minor style thing.

  • Handle errors. In Go, foo, _ := something() is a big code smell.

  • Iterating on a string gives you a rune; don't convert it to a one-char string just to compare it to another one-char string. Compare it to another rune instead ('a' instead of "a").

  • Your functions isVowel and isConsonant are re-building the vowel/consonant map from scratch every time they're called! This is very inefficient: initialize both maps as package variables, and inline these two functions completely. You have two options to do so, I like the second one better because it's slightly more readable.

    Option 1:

     var (
     vowels = map[rune]bool{
     'a': true,
     'e': true,
     'i': true,
     'o': true,
     'u': true,
     }
     consonants = map[rune]bool{
     // ...
     }
     )
    

    Option 2:

     var (
     vowels = toSet('a', 'e', 'i', 'o', 'u')
     consonants = toSet(/* ... */)
     )
     func toSet(chars ...rune) map[rune]bool {
     m := make(map[rune]bool, len(chars))
     for _, c := range chars {
     m[c] = true
     }
     return m
     }
    

    You then check if a rune r is a vowels by just calling vowels[r].

  • Your s = fmt.Sprintf("%c", x) statement looks a lot like printf debugging. Were you intending to keep it in your code?

  • countVowels and countConsonants can be inlined as well, and called on each char in the same loop like in Curtis' answer.

  • Replace foo += 1 by foo++.

  • I'd say you have needless blank lines in your code (I think each of your functions is simple enough not to have any), but that's a minor style thing.

  • Your code doesn't support non-ASCII encodings (and it probably should, UTF-8 is everywhere nowadays). It doesn't support uppercase letters, either =)

Source Link
Ted
  • 701
  • 3
  • 12
  • Handle errors. In Go, foo, _ := something() is a big code smell.

  • Iterating on a string gives you a rune; don't convert it to a one-char string just to compare it to another one-char string. Compare it to another rune instead ('a' instead of "a").

  • Your functions isVowel and isConsonant are re-building the vowel/consonant map from scratch every time they're called! This is very inefficient: initialize both maps as package variables, and inline these two functions completely. You have two options to do so, I like the second one better because it's slightly more readable.

    Option 1:

     var (
     vowels = map[rune]bool{
     'a': true,
     'e': true,
     'i': true,
     'o': true,
     'u': true,
     }
     consonants = map[rune]bool{
     // ...
     }
     )
    

    Option 2:

     var (
     vowels = toSet('a', 'e', 'i', 'o', 'u')
     consonants = toSet(/* ... */)
     )
     func toSet(chars ...rune) map[rune]bool {
     m := make(map[rune]bool, len(chars))
     for _, c := range chars {
     m[c] = true
     }
     return m
     }
    

    You then check if a rune r is a vowels by just calling vowels[r].

  • Your s = fmt.Sprintf("%c", x) statement looks a lot like printf debugging. Were you intending to keep it in your code?

  • countVowels and countConsonants can be inlined as well, and called on each char in the same loop like in Curtis' answer.

  • Replace foo += 1 by foo++.

  • I'd say you have needless blank lines in your code (I think each of your functions is simple enough not to have any), but that's a minor style thing.

lang-golang

AltStyle によって変換されたページ (->オリジナル) /