Section 2/161 menit
2. Masalah yang Dipecahkan
2. Masalah yang Dipecahkan
Debug dengan Print adalah Perubahan Kode yang Merusak Alur
Menambah print() untuk debug memiliki biaya: mengubah kode, recompile, menjalankan ulang, lalu mengingat untuk menghapus print statement sebelum commit. Di project besar ini bisa memakan 30-60 detik per iterasi.
swift
// ❌ TANPA LLDB: debug dengan print yang merusak alur kerja
func processOrder(_ order: Order) {
print("🔍 order received: \(order)") // harus dihapus sebelum commit
print("🔍 items count: \(order.items.count)")
for item in order.items {
print("🔍 processing item: \(item.id)") // bisa ada ratusan item
applyDiscount(to: item)
print("🔍 after discount: \(item.price)")
}
print("🔍 total: \(order.total)")
validatePayment(order)
}
// Kode produksi tercemari debug noise, recompile setiap iterasi
swift
// ✓ DENGAN LLDB: inspeksi tanpa mengubah kode, tanpa recompile
// Tambahkan breakpoint di baris yang diinginkan, lalu:
(lldb) po order
(lldb) po order.items.count
(lldb) p order.total
// Semua tanpa mengubah satu baris kode pun
Tidak Bisa Inspeksi State di Waktu Crash
Ketika app crash, print() sudah tidak bisa menambah informasi. LLDB bisa menginspeksi state tepat di momen crash.
swift
// ❌ TANPA LLDB: crash tanpa informasi berguna
// Thread 1: EXC_BAD_ACCESS (SIGSEGV) — hanya ini yang terlihat
// Tidak tahu variabel mana yang nil, stack frame mana yang bermasalah
// ✓ DENGAN LLDB: saat crash terjadi, jalankan:
// (lldb) bt → lihat full backtrace
// (lldb) frame info → lihat file dan baris tepat
// (lldb) po self → inspeksi objek di frame crash
// (lldb) frame variable → lihat semua variabel lokal
Tidak Bisa Mengubah State Tanpa Recompile
Saat debugging, seringkali perlu mencoba nilai berbeda untuk menguji hipotesis tanpa harus mengubah kode dan recompile.
swift
// ❌ TANPA LLDB: untuk test nilai berbeda, ubah kode → compile → run → ulang
func validateAge(_ age: Int) -> Bool {
return age >= 18 // ingin test: apakah logiknya benar untuk age = 17?
// Harus ubah nilai hardcode, compile ulang, jalankan lagi
}
// ✓ DENGAN LLDB: modifikasi variabel langsung di runtime
// (lldb) expr age = 17 → set nilai age ke 17
// (lldb) expr age = 100 → test edge case tanpa recompile
// (lldb) expression -- validateAge(15) → panggil fungsi apapun