Tugas 7 - Evolusi Perangkat Lunak

Nama: Sayid Ziyad Ibrahim Alaydrus
NRP : 5025201147

Refactoring


berikut adalah contoh kode program sebuah e-commerce yang menangani menghitung total harga pesanan dalam sebuah aplikasi belanja online.

*sebelum refactoring


a.) *sebelum refactoring

// Kode yang kurang terstruktur
function calculateOrderTotal(order) {
let total = 0;

for (let i = 0; i < order.items.length; i++) {
const item = order.items[i];

if (item.type === 'product') {
total += item.price * item.quantity;
} else if (item.type === 'discount') {
total -= item.amount;
}
}

if (order.discountCode === 'FIRSTORDER') {
total *= 0.9; // Diskon 10% untuk kode FIRSTORDER
}

return total;
}

b.)sesudah refactoring

// Kode yang lebih terstruktur
function calculateProductTotal(product) {
    return product.price * product.quantity;
}

function calculateDiscountTotal(discount) {
    return -discount.amount;
}

function applyFirstOrderDiscount(total, discountCode) {
    if (discountCode === 'FIRSTORDER') {
        return total * 0.9; // Diskon 10% untuk kode FIRSTORDER
    }
    return total;
}

function calculateOrderTotal(order) {
    let total = 0;

    for (let i = 0; i < order.items.length; i++) {
        const item = order.items[i];

        if (item.type === 'product') {
            total += calculateProductTotal(item);
        } else if (item.type === 'discount') {
            total += calculateDiscountTotal(item);
        }
    }

    return applyFirstOrderDiscount(total, order.discountCode);
}

kode telah di-refactor untuk membuat fungsi-fungsi terpisah yang lebih spesifik, memudahkan pemeliharaan, dan meningkatkan kejelasan niat. Kode untuk menghitung total produk atau diskon telah dipisahkan ke dalam fungsi-fungsi terpisah. Selain itu, fungsi applyFirstOrderDiscount ditambahkan untuk mengelola logika diskon khusus yang berkaitan dengan kode diskon tertentu.


*sebelum refactoring

// Kode yang kurang terstruktur
function processFruitList(fruits) {
    let formattedFruits = [];

    for (let i = 0; i < fruits.length; i++) {
        const fruit = fruits[i];

        let formattedFruit = {
            name: fruit.name,
            color: fruit.attributes.color,
            price: calculatePrice(fruit),
        };

        formattedFruits.push(formattedFruit);
    }

    return formattedFruits;
}

function calculatePrice(fruit) {
    let basePrice = 2.5;

    if (fruit.attributes.organic) {
        basePrice *= 1.5;
    }

    return basePrice;
}

const fruits = [
    { name: 'Apple', attributes: { color: 'red', organic: true } },
    { name: 'Banana', attributes: { color: 'yellow', organic: false } },
    // ... (beberapa buah lainnya)
];

const formattedFruits = processFruitList(fruits);
console.log(formattedFruits);

*sesudah refactoring

    // Kode yang lebih terstruktur
class Fruit {
    constructor(name, color, organic) {
        this.name = name;
        this.color = color;
        this.organic = organic;
    }

    calculatePrice() {
        let basePrice = 2.5;

        if (this.organic) {
            basePrice *= 1.5;
        }

        return basePrice;
    }

    format() {
        return {
            name: this.name,
            color: this.color,
            price: this.calculatePrice(),
        };
    }
}

function processFruitList(fruits) {
    return fruits.map((fruit) => fruit.format());
}

const fruits = [
    new Fruit('Apple', 'red', true),
    new Fruit('Banana', 'yellow', false),
    // ... (beberapa buah lainnya)
];

const formattedFruits = processFruitList(fruits);
console.log(formattedFruits);

Dalam contoh ini, kita memanfaatkan kelas Fruit untuk merepresentasikan buah, dengan metode calculatePrice dan format yang terpisah. Fungsi processFruitList kemudian menggunakan metode format untuk memproses dan memformat setiap buah. Ini membuat kode lebih bersih, mudah dimengerti, dan lebih mudah untuk ditambahkan atau diubah di masa mendatang.


Comments

Popular posts from this blog

Tugas 1 -Evolusi Perangkat Lunak

Tugas 6 - Evolusi Perangkat Lunak

Tugas 3- Evolusi perangkat Lunak