22.4Type Casting & Coercion / Bitshift

パフォーマンスのためのビットシフト

パフォーマンス上の理由でビットシフトを使用する場合は、その理由を説明するコメントを残します

ビットシフト `>> 0` は、数値を32ビット整数に変換する高速な方法として知られていますが、その意図は初見では分かりにくいです。パフォーマンスが非常に重要なボトルネックとなっている箇所でこれを使う場合は、なぜこの最適化が必要だったのかをコメントで説明することで、将来の自分や他の開発者がコードを理解するのを助けます。

❌ Bad
// No comment explaining the reason
const val = inputValue >> 0;
✅ Good
// good
/**
 * parseInt was the reason my code was slow.
 * Bitshifting the String to coerce it to a
 * Number made it a lot faster.
 */
const val = inputValue >> 0;