Table of Contents

    Hardcoding Values

    • Mistake: Directly using literal values within the code, which makes it harder to maintain or modify.
    • Solution: Use constants or configuration files to store values, so you can easily adjust them without modifying the code.

    Here's a Java example demonstrating the importance of using constants instead of hardcoded literals:

    
    public class ConstantsExample {
        // Define constants for better maintainability
        private static final double TAX_RATE = 0.08; // 8% tax rate
        private static final String WELCOME_MESSAGE = "Welcome to the Shopping System";
    
        public static void main(String[] args) {
            // Using constants instead of hardcoded values
            double price = 100.0;
            double totalPrice = price + (price * TAX_RATE);
    
            System.out.println(WELCOME_MESSAGE);
            System.out.println("Price before tax: $" + price);
            System.out.println("Total price after tax: $" + totalPrice);
        }
    }
    
    

    Explanation:

    • Mistake: Hardcoding values (e.g., 0.08 for tax or "Welcome to the Shopping System" directly in the code).
    • Solution: Store values in constants (TAX_RATE, WELCOME_MESSAGE) to make the code more maintainable and easier to modify.

    Would you like me to include a Java UI example as well? ?