以下の内容はhttps://tech.askul.co.jp/entry/2022/03/24/090000より取得しました。


🆒 な関数名選手権

こんにちは。しゅん(@MxShun)です。

はじめに

みんな~コーディングしてる~?(Yeah~!)命名してる~?(Yeah~!)

コーディングをしていくうえで避けては通れない、関数や変数のネーミング。

この記事では、言語公式・組込みなメソッドの中で、私が 🆒 と感じた関数名をランキング形式で3つ紹介します。

※本記事は、社内で実施したAdvent Calendar投稿記事を一部改変したものです。

第 3 位 🥉 Collectors.teeing()

第3位は、java.util.stream.Collectors.teeing です。

teeing

Returns a Collector that is a composite of two downstream collectors. Every element passed to the resulting collector is processed by both downstream collectors, then their results are merged using the specified merge function into the final result.

Java 12に追加された関数で、2つのCollectorを集約したCollectorを返却するメソッドです。

Stream.of(1, 2, 3, 4, 5, 6)
    .collect(
        Collectors.teeing(
            Collectors.summingDouble(i -> i),
            Collectors.counting(),
            (sum, count) -> sum / count
        )
    ); // 3.5

なにが 🆒 って、Collectorをがっちゃんこする関数に「teeing」という名前が付いていることです。

teeing

teeingですよ! teeing!

aggregateやmergeと付けそうなところをteeingですよ。この意外性と英語的なニュアンスがとっても 🆒 です!

第 2 位 🥈 panic()

お次は、Golang builtinの panic です。

func panic

The panic built-in function stops normal execution of the current goroutine. When a function F calls panic, normal execution of F stops immediately. Any functions whose execution was deferred by F are run in the usual way, and then F returns to its caller. To the caller G, the invocation of F then behaves like a call to panic, terminating G's execution and running any deferred functions. This continues until all functions in the executing goroutine have stopped, in reverse order. At that point, the program is terminated with a non-zero exit code. This termination sequence is called panicking and can be controlled by the built-in function recover.

panic

throw Error、throw Exceptionではなくpanicです。なんて直観的なんでしょう!(よく考えたらthrowableなErrorとExceptionも 🆒 なんですが。)

例外が例外としてではなく、処理を抜けるための便利な道具として使われるようになっていることに対して、panicがアンチテーゼになっていることも 🆒 だと思います。

Why Go gets exceptions right | Dave Cheney

第 1 位 🥇 Promise.race()

圧倒的1位は、JavaScriptの非同期関数 Promice.race です。

Promise.race()

Promise.race() メソッドは、反復可能オブジェクトの中の Promise のうちの1つが解決または拒否するとすぐに、その Promise の値または理由で解決または拒否する Promise を返します。

私が関数名に 🆒 を感じるきっかけになったメソッドです。 だって、Promise(約束)がraceですよ!?

promise

「🆒🆒🆒」、🆒 3つです。

非同期処理をPromiseを付ける時点で 🆒 なんですが、そのPromiseにレースさせちゃうんですから、もう 🆒 です! しかも、複数の非同期処理で最初に解決した非同期処理を採用するので、まさにraceです。🆒 なだけでなく意味も損なっていません。

なぜかは分かりませんが、非同期処理ってカッコいいものが多いですよね。 C++ のget_future() 、Kotlin Coroutineのlaunch() ... ああ、もっと使いこなせるようになりたい。

おわりに

今回は言語公式なメソッドの中で、私が 🆒 と感じた関数名を、ランキング形式で3つ紹介しました。

ぜひみなさんも 🆒 な関数名を探してみてください!

最後までご覧いただきありがとうございました。

告知

3月24日(木)夜、株式会社ラクス主催の「リーダブルコードLT会 - vol.3」に登壇します(8ヵ月ぶり2回目)

「EditorConfigで導くコードの『美しさ』」と題して話す予定です。視聴いただけると嬉しいです。

https://rakus.connpass.com/event/238395/rakus.connpass.com




以上の内容はhttps://tech.askul.co.jp/entry/2022/03/24/090000より取得しました。
このページはhttp://font.textar.tv/のウェブフォントを使用してます

不具合報告/要望等はこちらへお願いします。
モバイルやる夫Viewer Ver0.14