🍣

[ 命名規則 ] チートシート〜〜〜 !

2025/02/04に公開

プログラミング命名規則チートシート

こういうのはまとめておくと 何かと便利 です

変数名の基本ルール

単数・複数

  • 単数の場合: user, item, book
  • 複数の場合: users, items, books

データ型による命名

  • 文字列: firstName, lastName, description
  • 数値: age, count, price
  • 真偽値: isActive, hasPermission, canEdit
  • 配列: userList, itemArray, bookCollection
  • オブジェクト: userInfo, itemData, bookDetails

よく使う接頭辞・接尾辞

  • 最大値: maxValue, maxCount
  • 最小値: minPrice, minLength
  • 合計: totalAmount, sumPrice
  • 平均: averageScore, meanValue
  • インデックス: currentIndex, selectedIndex

関数名の基本ルール

動作を表す動詞から始める

  • 取得: getName(), getUsers()
  • 設定: setPrice(), setConfig()
  • 作成: createUser(), createAccount()
  • 更新: updateProfile(), updateStatus()
  • 削除: deleteItem(), removeUser()
  • 検証: validateEmail(), checkPassword()
  • 変換: convertToString(), parseToInt()

イベントハンドラー

  • クリック: handleClick(), onButtonClick()
  • 送信: handleSubmit(), onFormSubmit()
  • 変更: handleChange(), onValueChange()
  • キー押下: handleKeyPress(), onKeyDown()

クラス名の基本ルール

名詞で表現

  • モデル: UserModel, ProductModel
  • コントローラー: UserController, ProductController
  • サービス: AuthService, PaymentService
  • ユーティリティ: StringUtil, DateUtil
  • 例外: ValidationError, NetworkException

定数名の基本ルール

大文字とアンダースコア

const MAX_RETRY_COUNT = 3;
const DEFAULT_TIMEOUT = 1000;
const API_BASE_URL = 'https://api.example.com';

コンポーネント名(フロントエンド)

React/Vueコンポーネント

  • ページ: UserPage, HomePage
  • 一覧: UserList, ProductGrid
  • フォーム: LoginForm, RegisterForm
  • ボタン: SubmitButton, DeleteButton
  • モーダル: ConfirmModal, AlertDialog

データベース関連

テーブル名

  • 複数形: users, products, orders
  • 中間テーブル: user_products, order_items

カラム名

  • ID: user_id, product_id
  • 作成日時: created_at, created_date
  • 更新日時: updated_at, modified_date
  • 削除フラグ: is_deleted, deleted_at

一般的な命名パターン

状態を表す

// 処理状態
isLoading
isProcessing
isCompleted

// 有効/無効
isEnabled
isDisabled
isActive

// 表示/非表示
isVisible
isHidden
isShown

カウントを表す

totalUsers
itemCount
errorCount

時間を表す

startDate
endTime
duration
interval
timeout

避けるべき命名

❌ 避けるべき例:

  • 意味不明な省略: a, b, temp
  • あいまいな名前: data, info, stuff
  • 誤解を招く名前: list(配列でない場合)
  • スペルミス: recieve, lenght

✅ 良い例:

  • 明確な名前: userData, userInfo
  • 目的が分かる名前: calculateTotal, validateInput
  • 一貫性のある命名: firstName, lastName
  • 正しいスペル: receive, length

Discussion