Section 2/221 menit

2. Masalah yang Dipecahkan

2. Masalah yang Dipecahkan

Problem 1: Type Inference yang Terlalu Ambisius

Swift compiler harus menginfert tipe dari expression kompleks. Semakin ambisius inference-nya, semakin lama.

swift
// ❌ TANPA optimasi: compiler harus infer tipe dari chained expression panjang
let result = items
    .filter { $0.isActive && $0.score > 50 && $0.category == .premium }
    .map { $0.price * 0.9 * (1 + $0.taxRate) }
    .reduce(0) { $0 + $1 }
    .rounded(.toNearestOrAwayFromZero)
swift
// ✓ DENGAN anotasi tipe eksplisit: compiler tidak perlu inference
let activeItems: [Item] = items.filter { $0.isActive && $0.score > 50 && $0.category == .premium }
let discountedPrices: [Double] = activeItems.map { $0.price * 0.9 * (1 + $0.taxRate) }
let total: Double = discountedPrices.reduce(0, +)
let rounded: Double = total.rounded(.toNearestOrAwayFromZero)

Problem 2: Monolithic Target — Tidak Ada Incremental Build yang Efektif

Ketika seluruh app berada dalam satu target, perubahan satu file bisa memaksa recompile ratusan file karena dependency graph-nya flat.

swift
// ❌ TANPA modularisasi: ubah NetworkLayer.swift → recompile semua 300 file di target
// Satu target dengan 300 source file, semua saling bergantung secara implisit
swift
// ✓ DENGAN modularisasi: ubah NetworkLayer.swift → hanya recompile modul Network (~20 file)
// Target: NetworkKit (20 file), UIComponents (40 file), AppCore (15 file), MainApp (25 file)
// Perubahan di NetworkKit tidak menyentuh UIComponents

Problem 3: Dependency Rebuild di CI

Setiap CI run melakukan pod install atau SPM resolve dari scratch — menghabiskan 3–10 menit untuk unduh dan build ulang semua dependencies.

swift
# ❌ TANPA caching: setiap CI job download + build ulang semua pods
- run: pod install
- run: xcodebuild ...
# Waktu: 8 menit untuk pod install saja
swift
# ✓ DENGAN caching: restore cache jika Podfile.lock tidak berubah
- restore_cache: Podfile.lock hash
- run: pod install --repo-update (hanya jika cache miss)
- save_cache: Podfile.lock hash
# Waktu: 15 detik jika cache hit