HiveBrain v1.2.0
Get Started
← Back to all entries
patterngoMinor

Library to handle strings and numbers

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
handlenumberslibraryandstrings

Problem

I have created a pretty bad (just being honest) Golang library. I will take any and all advice, as long as it's helpful. Sorry it's so long. Also, I easily forget some things, so many functions in convert are just light wrappers around strconv, etc.

GitHub

Convert

```
package convert

import (
"github.com/retep-mathwizard/utils/depend"
o "github.com/retep-mathwizard/utils/other"
"strconv"
"strings"
"math"
)

func IntToString(num int) string {
str := strconv.Itoa(num)
return str
}

func StringToInt(str string) int {
num, err := strconv.Atoi(str)
if err != nil {
o.Exit("there was an error converting the type")
}
return num
}
func FloatToString(dec float64) string {
str := strconv.FormatFloat(dec, 'f', -1, 64)
return str
}
func StringToFloat(str string) float64 {
dec, err := strconv.ParseFloat(str, 64)
if err != nil {
o.Exit("there was an error converting the type")
}
return dec
}

func FloatToInt(dec float64) int {
num := int(dec)
return num
}

func IntToFloat(num int) float64 {
dec := float64(num)
return dec
}
func BoolToString(boo bool) string {
str := strconv.FormatBool(boo)
return str
}
func StringToBool(str string) bool {
boo, err := strconv.ParseBool(str)
if err != nil {
o.Exit("there was an error converting the type")
}
return boo
}
func StringToSlice(str string, splitat string) []string {
stringSlice := strings.Split(str, splitat)
return stringSlice
}
func SliceToString(array []string) string {
str := strings.Join(array, "")
return str

}
func IntToArray(num int) []int {
list := []int{}
digits := int(math.Log10(float64(num))) + 1
for i := digits - 1; i >= 0; i-- {
list = append(list, depend.IndexInt(num, i))
}
return list
}
func IntArrayToStringArray(list []int) []string {
newlist := []string{}
for _, item := range list {
stritem := IntToString(item)
newlis

Solution

It's the Go programming language: The Go Programming Language Specification. Using a misnomer like Golang is like spelling and typographical errors in a resume, it's an immediate red flag.

Your packages are undocumented. See Godoc: documenting Go code and GoDoc.


An idiosyncrasy is an unusual feature of a person. It also means
odd habit. The term is often used to express eccentricity or
peculiarity.

You confess that your libraries are idiosyncratic, which means that they and the code that uses them will be of little use to others.

convert:

You write

func IntToString(num int) string {
    str := strconv.Itoa(num)
    return str
}


which, simplified, is

func IntToString(i int) string {
    return strconv.Itoa(i)
}


It's obvious that IntToString is merely renaming a function from the strconv package in the Go standard library.

s = IntToString(i)
s = strconv.Itoa(i)


Your rationale is "I easily forget some things." That's not a problem. What is a problem is that you don't know how to easily find this information in the documentation. No employer wants employees who write idiosyncratic, unreadable, and unmaintainable code.

Similarly, you write,

func FloatToInt(dec float64) int {
    num := int(dec)
    return num
}


which, simplified, is

func FloatToInt(f float64) int {
    return int(f)
}


It's obvious that FloatToInt is merely performing an int conversion.

i = FloatToInt(f)
i = int(f)


I see negative value in your idosyncratic Go standard library function wrappers.

Similarly the input, mmath, mod, other, and depend packages seem idiosyncratic.

Code Snippets

func IntToString(num int) string {
    str := strconv.Itoa(num)
    return str
}
func IntToString(i int) string {
    return strconv.Itoa(i)
}
s = IntToString(i)
s = strconv.Itoa(i)
func FloatToInt(dec float64) int {
    num := int(dec)
    return num
}
func FloatToInt(f float64) int {
    return int(f)
}

Context

StackExchange Code Review Q#123806, answer score: 3

Revisions (0)

No revisions yet.