Hamdi Ahmadi Muzakkiy
avoid :
use :
avoid :
use :
class, object or struct better use noun
avoid :
use :
method name better use verb
avoid :
use :
avoid :
use :
method name better use verb
avoid :
func CompareAppVersion(s1, s2 string) int {
tmps1 := strings.Split(s1, ".")
tmps2 := strings.Split(s2, ".")
s1L := len(s1)
s2L := len(s2)
ss1 := 0
ss2 := 0
for {
if ss1 == s1L && ss2 == s2L {
return 0
}
if ss1 == s1L {
return 1
}
if ss2 == s2L {
return -1
}
a1, _ := strconv.Atoi(tmps1[ss1])
a2, _ := strconv.Atoi(tmps2[ss2])
if a1 > a2 {
return -1
} else if a1 < s2 {
return 1
}
ss1++
ss2++
}
return 0
}
func CompareAppVersion(appVersion, appVersionCompared string) int {
splitedAppVersion := strings.Split(appVersion, ".")
splitedAppVersionCompared := strings.Split(appVersionCompared, ".")
splitedAppVersionLength := len(splitedAppVersion)
splitedAppVersionComparedLength := len(splitedAppVersionCompared)
start := 0
startCompared := 0
for {
if start == splitedAppVersionLength && startCompared == splitedAppVersionComparedLength {
return APPVERSION_EQUAL // 0
}
if start == splitedAppVersionLength {
return COMPARED_APPVERSION_HIGHER // 1
}
if startCompared == splitedAppVersionComparedLength {
return APPVERSION_HIGHER // -1
}
numAppVersion, _ := strconv.Atoi(splitedAppVersion[start])
numAppVersionCompared, _ := strconv.Atoi(splitedAppVersionCompared[startCompared])
if numAppVersion > numAppVersionCompared {
return -1
} else if numAppVersion < numAppVersionCompared {
return 1
}
start++
startCompared++
}
return APPVERSION_EQUAL
}
func (payment Payment) EvaluatePayment() int {
if ... {
return 1
}
...
if ... {
return 2
}
return 3
}
func (payment Payment) EvaluatePayment() int {
if ... {
return PAYMENT_ABUSE
}
...
if ... {
return PAYMENT_FAILED
}
return PAYMENT_SUCCESS
}
func (adDataGroup *AdDataGroup) GetAdsRawData(from, to string) error {
var result AdDataGroup
param := url.Values{}
param.Add("api_token", AMod.Config.API["blacklotus"].Token)
param.Add("from", from)
param.Add("to", to)
agent := utils.NewHTTPRequest()
agent.Url = AMod.Config.API["blacklotus"].URL
agent.Path = AMod.Config.API["blacklotus"].Path
agent.Method = "GET"
agent.Param = param
agent.Timeout = time.Duration(60 * 60 * time.Second)
body, err := agent.DoReq()
if err != nil {
utils.Error(err)
return err
}
repostCSVString := string(*body)
reportCSVGroup := csv.NewReader(strings.NewReader(repostCSVString))
flags := true
for {
record, err := reportCSVGroup.Read()
// passed first rec
if flags {
flags = !flags
continue
}
if err == io.EOF {
break
}
if err != nil {
utils.Error(err)
return err
}
var adData AdData
adData.actionSetReport(record)
result = append(result, adData)
}
*adDataGroup = result
return nil
}
func (adDataGroup *AdDataGroup) GetAdsRawData(from, to string) *io.Reader {
var result AdDataGroup
param := url.Values{}
param.Add("api_token", AMod.Config.API["blacklotus"].Token)
param.Add("from", from)
param.Add("to", to)
agent := utils.NewHTTPRequest()
agent.Url = AMod.Config.API["blacklotus"].URL
agent.Path = AMod.Config.API["blacklotus"].Path
agent.Method = "GET"
agent.Param = param
agent.Timeout = time.Duration(60 * 60 * time.Second)
body, err := agent.DoReq()
if err != nil {
utils.Error(err)
return err
}
repostCSVString := string(*body)
reportCSVGroup := csv.NewReader(strings.NewReader(repostCSVString))
return reportCSVGroup
}
func (adDataGroup *AdDataGroup) MappingAdsCSVData (csvData *io.Reader) {
flags := true
for {
record, err := csvData.Read()
// passed first rec
if flags {
flags = !flags
continue
}
if err == io.EOF {
break
}
if err != nil {
utils.Error(err)
return err
}
var adData AdData
adData.actionSetReport(record)
result = append(result, adData)
}
*adDataGroup = result
return nil
}
passed data from argument rather instance
// Avoid
func (adData AdData) isOperatorContainOnOperatorList() bool {
operatorGroup := GetOperatorGroup()
searchedOperator := strings.ToLower(adData.Operator)
for _, operator := range operatorGroup {
if strings.Contains(searchedOperator, operator) {
return true
}
}
return false
}
// Use
func (adData AdData) isOperatorContainOnOperatorList(operatorGroup []string) bool {
searchedOperator := strings.ToLower(adData.Operator)
for _, operator := range operatorGroup {
if strings.Contains(searchedOperator, operator) {
return true
}
}
return false
}
// Avoid
func rander(flag bool) {
if flag {
...
return
}
...
}
// Use
func randerHomePage() {
...
}
func randerLoginPage() {
...
}
Ideal:
// Avoid
func MakeCircle(x, y ,radius float) Circle {
...
}
// strategy : make x and y an object/struct
// Use
func MakeCircle(point Center, radius float) Circle {
...
}
Make sure a function have no side effect
func CheckPassword(username, password string) bool {
var user User
user = getUserPassword(username)
if user.Password == password {
user.SetSession()
return true
}
return false
}
func (cartGroup *[]Cart) CalculateTotalPaymentWithDiscount() int {
for _, cart := cartGroup {
if cart.Amount < 100000 {
cartGroup.Total -= (cart.Amount/0.1)
} else {
cartGroup.Total -= (cart.Amount/0.5)
}
}
return cartGroup.Total
}
// Copyright (C) 2003,2004,2005 by Object Mentor, Inc. All rights reserved. // Released under the terms of the GNU General Public License version 2 or later.
// format matched kk:mm:ss EEE, MMM dd, yyyy
// Don't run unless you // have some time to kill.
Redundant Comments (take more time to read comment then read the code )
f, err := os.Open("filename.ext")
if err != nil {
...
}
if err := os.Unsetenv("test"); err != nil {
...
}
http://slides.com/hamdiahmadi/tdd#/
type Event struct {
Name string
Schedule time.Time
}
func NotifyEventA() { ... }
func NotifyEventB() { ... }
func NotifyEventC() { ... }
func (event Event) SetCron(option string) {
cronClient := cron.New()
if option == "event a" {
cronClient.AddFunc(FormatingSchedule(event.Schedule), func(){
NotifyEventA()
})
} else if option == "event b" {
cronClient.AddFunc(FormatingSchedule(event.Schedule), func(){
NotifyEventB()
})
} else if option == "event c" {
cronClient.AddFunc(FormatingSchedule(event.Schedule), func(){
NotifyEventC()
})
}
cronClient.Start()
}
func main() {
event := Event{
Name : "Event Something"
Schedule : time.Now().Add(24 * time.Hour * 10)
}
event.SetCron("event a")
}
type Event struct {
Name string
Schedule time.Time
}
type NotifyJob func()
func NotifyEventA() { ... }
func NotifyEventB() { ... }
func NotifyEventC() { ... }
func (event Event) SetCron(job NotifyJob) {
cronClient := cron.New()
cronClient.AddFunc(FormatingSchedule(event.Schedule), func(){
job()
})
cronClient.Start()
}
func main() {
event := Event{
Name : "Event Something"
Schedule : time.Now().Add(24 * time.Hour * 10)
}
event.SetCron(NotifyEventA)
}
Invalid +build tags
go vet github.com/username/repository/src/package
errcheck github.com/username/repository/src/package
golint src/package
gofmt -w src/package