Skip to content
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

Merged
merged 1 commit into from
May 7, 2020
Merged

treat message by pointer #38

merged 1 commit into from
May 7, 2020

Conversation

koron
Copy link
Member

@koron koron commented May 7, 2020

Message をポインタで取り扱うようにしました。これは #36 で指摘・依頼されたようにツール側で Message 以下の情報を埋める場合に値のままだと非効率&バグの温床になるのを事前に防ぎます。
(例: メッセージ側には情報が入ってるがスレッドには入ってないなどなど)

その結果として以下のことが起こっています。

  • Messages ([]*Message) の定義
    • Sort() メソッドを生やした & 使ってる場所の見通しが良くなった
  • MessagesMap (map[MessageMonthKey]Messages) の定義
    • Keys() メソッドを生やした & 使ってる場所の見通しが良くなった
  • Thread 関連
    • Put() を生やした & 使ってる場所の見通しが良くなった
    • レシーバの名前を t から th へ変更した。 t のままテストを書き始めるとめんどいことになるので
  • いくつかのループからインデックスが消え、インデックスを使った参照が消え見通しが良くなった

@koron koron force-pushed the message-by-pointer branch from 9e513f4 to 21f0d91 Compare May 7, 2020 02:05
@koron koron force-pushed the message-by-pointer branch from 21f0d91 to 2dd5a02 Compare May 7, 2020 02:07
Copy link
Member Author

@koron koron left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

著者解説~

Comment on lines -125 to -128
keys := make([]MessageMonthKey, 0, len(msgsMap))
for key := range msgsMap {
keys = append(keys, key)
}
Copy link
Member Author

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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

少し後ろの、本当に必要になる直前で宣言するように移動した。

}
thread.Put(msg)
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mapの値がポインタならば以下の3ステップで書くのが見通しが良くなるコツ。
ここはその形式に直した。

  1. ローカル変数へ取り出してみる
  2. なければ空のを突っ込むローカル変数とmapに突っ込む
  3. ローカル変数のを加工する

Comment on lines -120 to -124
if _, ok := m.MsgsMap[key]; !ok {
m.MsgsMap[key] = visibleMsgs
} else {
m.MsgsMap[key] = append(m.MsgsMap[key], visibleMsgs...)
}
Copy link
Member Author

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引数に渡せる&適切にアロケーションしてくれる

のコンボでこうなる。

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

あー、nil スライス append に渡せないと勘違いしてました

Comment on lines -128 to -131
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
})
Copy link
Member Author

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 {
Copy link
Member Author

@koron koron May 7, 2020

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 は使えずぐんにょりするのを今のうちに回避した。

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// 丁度別件で踏みました...

@koron koron marked this pull request as ready for review May 7, 2020 02:15
Copy link

@tennashi tennashi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@koron koron merged commit 4f6e153 into master May 7, 2020
@koron
Copy link
Member Author

koron commented May 7, 2020

問題なさげなのでマージしました。

@koron koron deleted the message-by-pointer branch May 7, 2020 09:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants