Skip to content

20.ValidParentheses#8

Open
xbam326 wants to merge 3 commits into
mainfrom
20
Open

20.ValidParentheses#8
xbam326 wants to merge 3 commits into
mainfrom
20

Conversation

@xbam326
Copy link
Copy Markdown
Owner

@xbam326 xbam326 commented Dec 31, 2025

else:
raise ValueError("Invalid input: only parentheses are allowed")

return not open_parentheses_stack
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[IMO]
return not open_parentheses_stackは認知負荷が高いかなと私は思いました。
この行だけ見たときに、open_parenthese_stackはどんな変数なのか、その否定とは?みたいな疑問が湧くためです。

私は return len(open_parentheses_stack) == 0 というような形で書いてまして、
これはopen_parentheses_stackの長さが0であるかどうかを検証している関数なんだなというのがわかりやすいだろうという意図です。
文章を書くときと同様に結論はなにをしたいのかというのはわかりやすくなるように心がけてます。

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.

ご指摘ありがとうございます。
私も感覚としてlen(open_parentheses_stack) == 0の方が若干読みやすいのは賛成なのですが、下記のような指摘があり、変数名にstackとあることもありより簡潔に書ける方を採用しました。
https://github.com/komdoroid/arai60/pull/12/files#r2630417643

もう一度他の方のコードを見返してみるとlenで書かれてる方が多いようでしたので次回以降はlen~で書くようにしようと思います。

for char in s:
if char in open_parentheses:
open_parentheses_stack.append(char)
elif char in close_parentheses:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

読み手としては、条件分岐を頭に入れながら読み進める必要があるので、さっさとearly returnして新しいif文があるほうが読みやすいです。さらにif文が続く場合はなおさらです。

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.

3 participants