LoginSignup
2
2

More than 1 year has passed since last update.

プログラムのNGな名付け

Last updated at Posted at 2021-08-17

嘘つき

/**
 * 名前と中身が合っていない(変数)
 */
const foo = getHoge();

/**
 * 名前と中身が合っていない(関数戻り値)
 */
function getHoge() {
    return "foo";
}

/**
 * 名前と中身が合っていない(関数処理)
 * ※名称以外の役割を持っている
 */
function updateHoge(const item) {
    // 更新処理
    update(item);

    // 更新した情報を取得
    return getItem();
}

/**
 * リストなのに単数形
 */
const item = getList();

意味がない

/**
 * 何が代入されているかわからない
 * ○: const hoge = getHoge();
 */
const a = getHoge();

/**
 * 何が返されるかわからない
 * ○: getHoge()
 * ○: class Hoge { get() {} } ※クラス名を省略(何をgetしたいか理解できる)
 */
function getData() {}

/**
 * 型の総称だけ
 */
const str = "";

/**
 * 限られたスコープの場合は許容
 */
for (let i = 0; i < 1; i++) {}

/**
 * 数行でお役御免に場合はOK
 * (いい例が思い付かなかった)
 */
function getHoge(const hoge) {
    let result = "";
    if (hoge) {
        result = hoge;
    }
    return result;
}

省略形

/*
 * response? result?
 * 文脈で見ればわかるけどさ...
 * ○: result
 */
const res = "result";

/**
 * あと少し!なぜ略す?
 * (いい例が思い付かなかった)
 * ○: response
 */
const resp = "response";

/**
 * 一般用語となっているものはOK
 */
const id = "ID";

余計な情報

/**
 * なぜStringだけ`str`をつける?
 * ○: hoge
 */
const hogeStr = "";

/**
 * 機能IDを付ける
 */
class A001Hoge {}

動詞

/**
 * 動詞が後になっている
 * ○: getItem()
 */
function itemGet() {}

/**
 * 動詞にするところを名詞にしている
 * ○: calculateTotal()
 */
function calculatorTotal() {}

NEW

/**
 * 新しく定義するモノだからと安易に new と付けない
 * 何が新しくなったのかわからないし、そのうち新しいモノではなくなる
 * ○: ファイル分割、適切な名称付け etc
 */
const newHoge = "";
const hoge = "";
2
2
4

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
2
2