-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
treat message by pointer #38
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
著者解説~
keys := make([]MessageMonthKey, 0, len(msgsMap)) | ||
for key := range msgsMap { | ||
keys = append(keys, key) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MessageMap.Keys()
へ移動した。
@@ -79,13 +102,15 @@ func (m *MessageTable) ReadLogFile(path string) error { | |||
return nil | |||
} | |||
|
|||
var msgs []Message | |||
var visibleMsgs []Message |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
少し後ろの、本当に必要になる直前で宣言するように移動した。
} | ||
thread.Put(msg) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mapの値がポインタならば以下の3ステップで書くのが見通しが良くなるコツ。
ここはその形式に直した。
- ローカル変数へ取り出してみる
- なければ空のを突っ込むローカル変数とmapに突っ込む
- ローカル変数のを加工する
if _, ok := m.MsgsMap[key]; !ok { | ||
m.MsgsMap[key] = visibleMsgs | ||
} else { | ||
m.MsgsMap[key] = append(m.MsgsMap[key], visibleMsgs...) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ここは上記のスライス版。
- 存在しないkeyにmapはゼロ値を返す
- スライスのゼロ値はnil
- nil スライスは append() の第1引数に渡せる&適切にアロケーションしてくれる
のコンボでこうなる。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
あー、nil スライス append に渡せないと勘違いしてました
sort.SliceStable(m.MsgsMap[key], func(i, j int) bool { | ||
// must be the same digits, so no need to convert the timestamp to a number | ||
return m.MsgsMap[key][i].Ts < m.MsgsMap[key][j].Ts | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Messages.Sort()
として切り出した。
} | ||
|
||
func (t Thread) LastReplyTime() time.Time { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
以下レシーバーの名前を t
から th
へ変更。
テストを書くと t *testing.T
となるので Thread のレシーバーとしては t
は使えずぐんにょりするのを今のうちに回避した。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// 丁度別件で踏みました...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
問題なさげなのでマージしました。 |
Message
をポインタで取り扱うようにしました。これは #36 で指摘・依頼されたようにツール側でMessage
以下の情報を埋める場合に値のままだと非効率&バグの温床になるのを事前に防ぎます。(例: メッセージ側には情報が入ってるがスレッドには入ってないなどなど)
その結果として以下のことが起こっています。
Messages
([]*Message
) の定義Sort()
メソッドを生やした & 使ってる場所の見通しが良くなったMessagesMap
(map[MessageMonthKey]Messages
) の定義Keys()
メソッドを生やした & 使ってる場所の見通しが良くなったThread
関連Put()
を生やした & 使ってる場所の見通しが良くなったt
からth
へ変更した。t
のままテストを書き始めるとめんどいことになるので