「コードにコメントは必要か?」という議論。実は母国語の問題ではないかという仮説

「コメント不要論(コードが説明的であるべき)」「コメント必要論」

「良いコードならコメントがなくても読めるはずだ」と言う人もいれば、「いや、日本語の補足がないと意図が伝わらない」と言う人もいます。

「そもそもプログラミング言語が日本語(母国語)で書かれていないから、コメントが必要になるのではないか?」

っと思っています。


1. 一般的なプログラミング(英語命名 + 日本語コメント)

/**
 * 商品価格の計算管理クラス
 */
public class PriceCalculator {

    // 消費税率(10%)
    private static final double TAX_RATE = 0.10;

    /**
     * 税抜き価格から税込み価格を計算する
     * @param basePrice 税抜き価格
     * @return 税込み価格(整数)
     */
    public static int calculateTotalPrice(int basePrice) {
        // 税額を計算
        double taxAmount = basePrice * TAX_RATE;
        
        // 元の価格に税額を足して計算結果を返す
        return (int) (basePrice + taxAmount);
    }

    public static void main(String[] args) {
        int price = 1000;
        int totalPrice = calculateTotalPrice(price);
        System.out.println("合計: " + totalPrice + "円");
    }
}

2. 日本語プログラミング(日本語命名 + コメントなし)

public class 価格計算機 {

    private static final double 消費税率 = 0.10;

    public static int 税込み価格を計算する(int 税抜き価格) {
        double 税額 = 税抜き価格 * 消費税率;
        
        return (int) (税抜き価格 + 税額);
    }

    public static void main(String[] 引数) {
        int 元の価格 = 1000;
        int 最終的な税込み価格 = 税込み価格を計算する(元の価格);
        System.out.println("合計: " + 最終的な税込み価格 + "円");
    }
}

3. 一般的なプログラミング(英語命名 + 英語コメント)

/**
 * Class to manage price calculations.
 */
public class PriceCalculator {

    // Consumption tax rate (10%)
    private static final double TAX_RATE = 0.10;

    /**
     * Calculates the total price including tax.
     * @param basePrice Price excluding tax
     * @return Total price including tax
     */
    public static int calculateTotalPrice(int basePrice) {
        // Calculate tax amount
        double taxAmount = basePrice * TAX_RATE;
        
        // Return base price plus tax amount
        return (int) (basePrice + taxAmount);
    }

    public static void main(String[] args) {
        int price = 1000;
        int totalPrice = calculateTotalPrice(price);
        System.out.println("Total: " + totalPrice + " JPY");
    }
}

4. 日本語プログラミング(日本語命名 + 日本語コメント)

/**
 * 価格計算機クラス
 */
public class 価格計算機 {

    // 消費税率(10%)
    private static final double 消費税率 = 0.10;

    /**
     * 税込み価格を計算する
     * @param 税抜き価格 税抜き価格
     * @return 最終的な税込み価格
     */
    public static int 税込み価格を計算する(int 税抜き価格) {
        // 税額を計算
        double 税額 = 税抜き価格 * 消費税率;
        
        // 税抜き価格と税額を足す
        return (int) (税抜き価格 + 税額);
    }

    public static void main(String[] 引数) {
        int 元の価格 = 1000;
        int 最終的な税込み価格 = 税込み価格を計算する(元の価格);
        System.out.println("合計: " + 最終的な税込み価格 + "円");
    }
}

5. 日本語プログラミング(日本語命名 + 英語コメント)

/**
 * Class to manage price calculations.
 */
public class 価格計算機 {

    // Consumption tax rate (10%)
    private static final double 消費税率 = 0.10;

    /**
     * Calculates the total price including tax.
     * @param 税抜き価格 Price excluding tax
     * @return Total price including tax
     */
    public static int 税込み価格を計算する(int 税抜き価格) {
        // Calculate tax amount
        double 税額 = 税抜き価格 * 消費税率;
        
        // Return base price plus tax amount
        return (int) (税抜き価格 + 税額);
    }

    public static void main(String[] 引数) {
        int 元の価格 = 1000;
        int 最終的な税込み価格 = 税込み価格を計算する(元の価格);
        System.out.println("Total: " + 最終的な税込み価格 + " JPY");
    }
}

見慣れないなってコードは思うけど、読むのなら、結局日本人で日本語がメインだから、日本語あってほしいよね?

英語理解できたら英語だけでいいし。

つまり、自分はコメント不要です。

設計=コメント=実装=テスト

より

設計=実装=テスト

と一個でも減らして管理コスト減らしたい人です。