略語ではなく完全な単語を使う
Contents
Use full words rather than abbreviations (e.g., compensatingTransaction instead of ct). Commonly accepted abbreviations like db are fine.
(略語ではなく完全な単語を使う(例:ctではなくcompensatingTransaction)。dbのように一般的に受け入れられている略語は問題ない)
解説
略語は、書いた本人以外には意味が不明確で、コードの理解を妨げます。完全な単語を使用することで、コードの意図が明確になり、新しいメンバーでも容易に理解できます。現代のエディタには自動補完機能があるため、長い名前でも入力の手間は問題になりません。ただし、広く認知されている略語(例:db、api、html、url)は使用しても問題ありません。
具体例
// 悪い例(独自の略語を使用)
func ProcUsrData(usr *Usr) error {
// usr, Procは何の略か不明
res := CalcTtl(usr.Itms)
// res, Ttl, Itmsは何の略か不明
if err := SaveToDb(res); err != nil {
return err
}
return nil
}
type Usr struct {
Nm string // Nmは何の略?
Em string // Emは何の略?
Ph string // Phは何の略?
Itms []Item
}
func CalcTtl(itms []Item) float64 {
ttl := 0.0
for _, itm := range itms {
ttl += itm.Prc
}
return ttl
}
// 良い例(完全な単語を使用)
func ProcessUserData(user *User) error {
total := CalculateTotal(user.Items)
if err := SaveToDatabase(total); err != nil {
return err
}
return nil
}
type User struct {
Name string
Email string
Phone string
Items []Item
}
func CalculateTotal(items []Item) float64 {
total := 0.0
for _, item := range items {
total += item.Price
}
return total
}
// 一般的な略語は使用可(良い例)
type UserRepository struct {
db *sql.DB // dbは一般的
api APIClient // apiは一般的
}
func FetchFromAPI(url string) ([]byte, error) {
// url, apiは一般的
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return io.ReadAll(resp.Body)
}
func GenerateHTML(data TemplateData) string {
// htmlは一般的
return renderTemplate(data)
}
参考リンク
略語ではなく完全な単語を使う https://www.tricrow.com/core/coding-standard/full-words-not-abbrev.html

