1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Java】技術書から学んだ命名規則・例外設計を実践に活かすためのメモ

Posted at

はじめに

最近、コードを書いて気づいた「命名・例外・引数」の扱いについて、可読性や安全性を意識した考え方を自分なりにまとめてみました!


命名のポイント

  • メソッドは動詞一言で
  • 連番(getData1など)は避ける
  • 命名で処理の意図を表すのが基本

コメントに頼らない設計

  • 命名で意味を伝える
  • コメントは変更漏れのリスクがある

戻り値は意味のあるクラスで

  • int や String よりも UserId 等の専用クラスを使うと混乱が減る

引数は final + 意味のある命名

  • final指定で安全性UP
  • newUser、dbUserなどで状態を明確に

例外は throw で明確に

  • -1やnull返しはバグの元
  • IllegalArgumentExceptionなどを活用
  • 独自例外も視野に!

おわりに

こうした設計の小さな工夫が、長期的に安全で読みやすいコードにつながると実感しています。もっと良いやり方やフィードバックがあればぜひ教えてください!

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?