Skip to content

2. Add Two Numbers#5

Open
hroc135 wants to merge 4 commits intomainfrom
sendahuang14-patch-5
Open

2. Add Two Numbers#5
hroc135 wants to merge 4 commits intomainfrom
sendahuang14-patch-5

Conversation

@hroc135
Copy link
Copy Markdown
Owner

@hroc135 hroc135 commented Jul 21, 2024

@hayashi-ay
Copy link
Copy Markdown

ファイルをコミットできてなさそうです。

@hroc135
Copy link
Copy Markdown
Owner Author

hroc135 commented Jul 21, 2024

失礼しました!コミットし直しました。ご指摘ありがとうございます。

Copy link
Copy Markdown

@hayashi-ay hayashi-ay left a comment

Choose a reason for hiding this comment

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

コメントしました

func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
dummy := &ListNode{Val: -1, Next: nil}
added := dummy
carryUp := false
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

フラグでも良いですが、0,1で持つと処理が完結に書けますね。

carryUp := false

for l1 != nil || l2 != nil || carryUp {
s := 0
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

sでも良いですが、sumとかの方が良い気がします。

s := 0
if l1 != nil {
s += l1.Val
l1 = l1.Next
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

足すのとノードを動かすのを一緒にやるか、分けるかは好みですかね?自分は足す処理は足す処理で書いて、ノードを動かすのは別に書くのが好みです。

l2 = l2.Next
}

if carryUp {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

自分はヘルパー関数とかを用意してあげて以下のように書く気がします。carryは0, 1で持っている。

sum = getValue(l1) + getValue(l2) + carry

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

たしかにそう書いたらもっとすっきりしそうですね。ありがとうございます


```Go
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
dummy := &ListNode{Val: -1, Next: nil}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

sentinel という単語を使っている方もいらっしゃいます。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

sentinelという単語は外ではあまり見かけずこの界隈ではよく見るのですが、「ソフトウェアエンジニアの常識」に入りますか?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

自分は常識に入っていると思います。

日本語だと「番兵」と呼ばれることが多いように思います。
https://ja.wikipedia.org/wiki/%E7%95%AA%E5%85%B5
https://en.wikipedia.org/wiki/Sentinel_node
https://en.wikipedia.org/wiki/Sentinel_value

X (Twitter) でも聞いてみましょう…。
https://x.com/nodchip/status/1815199895860707526

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

https://source.chromium.org/search?q=%22sentinel%22
「番兵」という概念はときどき使います。変数名として使うのがとても一般的とまでは思いません。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

とはいえ、Chromium のソースを検索するとそれなりに出てきますね。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

https://x.com/nodchip/status/1815199895860707526
プログラミング用語の「番兵」という言葉を知っている人と知らない人が、ほぼ半々のようですね。ソフトウェアエンジニアの常識には含まれていなさそうです。

チームで開発する際、チームの平均的なエンジニアが sentinel という単語を知っていて、変数名に使っても違和感を感じないのであれば、使っても良いと思います。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

わざわざ集計ありがとうございます。少なくとも協会参加者内だと常識ということでここでは使っていこうと思います。

l1, l2 = l1.Next, l2.Next
}

for l1 != nil {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

同じ処理が 2 回連続していますので、関数化しても良いと思います。

carryUpFlag := false

for l1 != nil || l2 != nil || carryUpFlag {
s := 0
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

変数のスコープの長さがそこそこありますので、変数の値が表すものを端的に表した英単語・英語句を変数名として付けることをお勧めいたします。

func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
dummy := &ListNode{Val: -1, Next: nil}
addedNode := dummy
carryUpFlag := false
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

flag という単語にはほとんど情報量がないため、 step 3 のように carryUp としたほうが良いと思います。

- addedNodeをaddedに変更。`added := dummy`から、`added`がノードであることを推測できるから。
- carryUpFlagをcarryUpに変更。`if carryUpFlag`より`if carryUp`の方が自然だと思った。

```
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

言語指定がないのでシンタックスハイライトが消えてますね!

return dummy.Next
}

func addTwoNodes(addedNode, l1, l2 *ListNode, carryUpFlag bool) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

step3でコメントありましたが、carryは自然数で持つほうが自然かなと思います

今回は問題設定の性質上carryが0 or 1 ですが、場合によっては2とかもありうるので

for l1 != nil || l2 != nil || cu == do {
sum := getVal(l1) + getVal(l2) + int(cu)
cu = dont
if sum >= 10 {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

carryUpは数値で持たせsum / 10で算出すると、この分岐を消せてコードをシンプルにできます。

Comment on lines +17 to +29
getVal := func(node *ListNode) int {
if node == nil {
return 0
}
return node.Val
}

moveNode := func(node *ListNode) *ListNode {
if node == nil {
return node
}
return node.Next
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

わざわざメソッドにするほどの処理でもなさそうに思います。

また、メソッド名から受け取る印象と実際の挙動の差分に違和感を感じました。
getValという名前で0が返ってきた時、それはnilを渡したからなのかnodeのvalが実際に0だからなのか区別がつかず、混読みづらさを感じます。
moveNodeについても同じで、moveしてるはずなのに渡したノードそのものが返ってくるのは読んだ時に違和感を覚えます。

@rihib rihib mentioned this pull request Aug 6, 2024
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.

6 participants