如何识别和避免魔鬼数字在代码中的不良影响

大家好,我是微赚淘客返利系统3.0的小编,是个冬天不穿秋裤,天冷也要风度的程序猿!今天我们来聊聊编程中的一个常见问题——魔鬼数字(Magic Numbers)。魔鬼数字是指那些在代码中直接使用的数值常量,它们没有任何解释或说明,导致代码的可读性和维护性变差。我们将探讨如何识别魔鬼数字及其带来的不良影响,并提供一些避免使用魔鬼数字的技巧。

魔鬼数字的危害

魔鬼数字会导致代码难以理解和维护。例如,假设你在代码中看到一个神秘的数字42,你可能不知道它的意义。如果这个数字被多次使用,你将很难修改它,因为你不确定每个出现的地方是否应该修改。

来看一个Java的例子:

package cn.juwatech.example;

public class MagicNumberExample {
    public static void main(String[] args) {
        int totalAmount = 100;
        int discount = totalAmount * 42 / 100; // 42 代表什么?
        System.out.println("Discount amount: " + discount);
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

在上面的代码中,42是一个魔鬼数字。它的意义不明确,导致代码难以理解。如果需要修改这个折扣比例,你必须找到所有的42并替换它们,这非常容易出错。

如何识别魔鬼数字

识别魔鬼数字的关键在于寻找那些不具有明显上下文意义的数字。通常,这些数字会出现在算术运算、条件判断和数组索引等地方。

package cn.juwatech.example;

public class MagicNumberDetection {
    public static void main(String[] args) {
        int[] scores = {85, 90, 78, 92};
        int passingScore = 75;
        
        for (int score : scores) {
            if (score >= passingScore) {
                System.out.println("Passed");
            } else {
                System.out.println("Failed");
            }
        }
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.

在上面的代码中,75是一个魔鬼数字,因为它的意义没有在代码中明确说明。

避免魔鬼数字的方法

使用常量是避免魔鬼数字的一个有效方法。通过为这些数字赋予有意义的名字,可以提高代码的可读性和可维护性。

package cn.juwatech.example;

public class AvoidMagicNumbers {
    private static final int DISCOUNT_PERCENTAGE = 42;
    private static final int PASSING_SCORE = 75;

    public static void main(String[] args) {
        int totalAmount = 100;
        int discount = totalAmount * DISCOUNT_PERCENTAGE / 100;
        System.out.println("Discount amount: " + discount);

        int[] scores = {85, 90, 78, 92};
        
        for (int score : scores) {
            if (score >= PASSING_SCORE) {
                System.out.println("Passed");
            } else {
                System.out.println("Failed");
            }
        }
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.

在上面的代码中,我们用DISCOUNT_PERCENTAGE和PASSING_SCORE来代替魔鬼数字42和75。这样,代码的意义变得清晰了许多。

使用枚举类型

在某些情况下,使用枚举类型(enum)也是一个不错的选择,尤其是当魔鬼数字代表特定的一组值时。

package cn.juwatech.example;

public class EnumExample {
    public enum DiscountType {
        NONE(0),
        SILVER(10),
        GOLD(20),
        PLATINUM(30);

        private final int percentage;

        DiscountType(int percentage) {
            this.percentage = percentage;
        }

        public int getPercentage() {
            return percentage;
        }
    }

    public static void main(String[] args) {
        int totalAmount = 100;
        DiscountType discountType = DiscountType.GOLD;
        int discount = totalAmount * discountType.getPercentage() / 100;
        System.out.println("Discount amount: " + discount);
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.

在这个例子中,我们用枚举类型DiscountType来表示不同的折扣类型,每个类型都有对应的折扣百分比。

使用配置文件

当需要频繁修改某些数值时,可以考虑将这些数值放在配置文件中,而不是硬编码在程序中。这样可以更方便地进行调整而无需修改代码。

package cn.juwatech.example;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class ConfigExample {
    private static final String CONFIG_FILE = "config.properties";
    private static int discountPercentage;
    private static int passingScore;

    static {
        try (InputStream input = ConfigExample.class.getClassLoader().getResourceAsStream(CONFIG_FILE)) {
            Properties prop = new Properties();
            if (input == null) {
                System.out.println("Sorry, unable to find " + CONFIG_FILE);
                return;
            }
            prop.load(input);
            discountPercentage = Integer.parseInt(prop.getProperty("discount.percentage"));
            passingScore = Integer.parseInt(prop.getProperty("passing.score"));
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public static void main(String[] args) {
        int totalAmount = 100;
        int discount = totalAmount * discountPercentage / 100;
        System.out.println("Discount amount: " + discount);

        int[] scores = {85, 90, 78, 92};
        
        for (int score : scores) {
            if (score >= passingScore) {
                System.out.println("Passed");
            } else {
                System.out.println("Failed");
            }
        }
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.

在这个例子中,折扣百分比和及格分数被存储在配置文件config.properties中,使得修改这些值变得更加容易。

总结

魔鬼数字会导致代码难以理解和维护。通过使用常量、枚举类型和配置文件,可以有效地避免魔鬼数字的使用,从而提高代码的可读性和可维护性。希望通过本文的讲解,你能更好地识别和避免魔鬼数字的使用,从而编写出更高质量的代码。