CodeWithPKCodeWithPK
CodeWithPK
  • Home
  • Blog
  • About
  • Services
  • Portfolio
  • Contact
  • Contact Us?

    praveen@codewithpk.com
CodeWithPK

🚫 Stop Using console.log() for Debugging! (Do This Instead) πŸ›‘

  • Home
  • Blog
  • 🚫 Stop Using console.log() for Debugging! (Do This Instead) πŸ›‘
codewithpk.com/programming-tips
  • codewithpk@720
  • December 5, 2024
  • 74 Views

Have you ever found your code littered with endless console.log() statements like:

  • console.log("here")
  • console.log("why???")
  • console.log("please work 😭")

We’ve all been there! But there’s a smarter, cleaner way to debug. Let’s dive into the magic of Chrome DevTools and other modern debugging tools that work for both JavaScript and Kotlin developers. 🌟


πŸ§‘β€πŸ’» Better Debugging Tools to Replace console.log()

1. debugger Statement πŸ›‘

The debugger keyword is a game-changer. Just add it to your code where you suspect something might be off, and the browser will pause right there when you run it in DevTools.
πŸ“ Example:

function calculateTotal(a, b) {
  debugger; // Stops here in DevTools
  return a + b;
}
fun calculateTotal(a: Int, b: Int): Int {
    // In Kotlin, use your IDE's breakpoint or debugger tools here
    return a + b
}

πŸ’‘ Why It’s Better:

  • It gives you access to all variables, the call stack, and the ability to step through your codeβ€”no need for guesswork.

2. console.trace() πŸ”

Want to find out how your code ended up in a specific function? Use console.trace(). It prints the entire call stack leading up to that point.

πŸ“ Example:

function fetchData() {
  console.trace("Fetch data called");
  // Other code
}
fun fetchData() {
    // Use breakpoints or logging libraries for similar tracing in Kotlin
    println("Trace: Fetch data called")
}

πŸ’‘ Why It’s Better:

  • Shows the exact path of execution, saving you time from adding console.log() in multiple places.

3. console.table() πŸ“Š

If you’re working with arrays or objects, make your data readable with console.table().

πŸ“ Example:

const users = [
  { id: 1, name: "Alice", role: "Admin" },
  { id: 2, name: "Bob", role: "User" },
];
console.table(users);
data class User(val id: Int, val name: String, val role: String)

fun displayUsers() {
    val users = listOf(
        User(1, "Alice", "Admin"),
        User(2, "Bob", "User")
    )
    println(users.joinToString("\n"))
}

πŸ’‘ Why It’s Better:

  • Displays tabular data in a clean, readable format. No more [Object object] mess.

4. Breakpoints in DevTools or IDEs 🚦

Instead of adding debugging code, set breakpoints directly in your IDE (like Android Studio for Kotlin) or in the Chrome DevTools Sources tab for JavaScript.

πŸ“ Steps for Chrome DevTools:

  1. Open the Sources tab.
  2. Click the line number where you want the code to pause.
  3. Refresh your app, and it will pause execution right there.

πŸ’‘ Why It’s Better:

  • No changes to your actual code.
  • Lets you inspect variables and step through code directly.

πŸ“ Example in Kotlin (Android Studio):
Set breakpoints in your IDE by clicking on the gutter next to the line number.


5. console.time() & console.timeEnd() πŸ•’

Need to figure out why something is slow? Use this pair to measure the exact time taken for a block of code to execute.

πŸ“ Example:

console.time("fetchData");
fetchData();
console.timeEnd("fetchData");
fun fetchData() {
    val start = System.currentTimeMillis()
    // Perform some operations
    val end = System.currentTimeMillis()
    println("Time taken: ${end - start}ms")
}

πŸ’‘ Why It’s Better:

  • Great for performance testing without adding extra tools.

πŸš€ Pro Tips for Debugging

  • Always remove logs before pushing to production to avoid exposing sensitive information.
  • Use libraries like Timber in Kotlin for structured logging.
  • For JS, try advanced debugging tools like Sentry for error tracking.

πŸ€” What’s Your Favorite Debugging Trick?

Debugging doesn’t have to feel like fighting with your code. πŸ₯Š Try these tools and save yourself the frustration of endless console.log()! Got a favorite tip or trick? Share it in the comments! πŸ“

Share:

Previus Post
Simplified DSA
Next Post
πŸ“’ Today’s

Leave a comment

Cancel reply

Recent Posts

  • How Android Device Security Works: Bootloader, FRP, and Verified Boot (Complete Developer Guide)
  • Android 15 FRP Security Changes: What Developers Should Know (2026 Guide)
  • Samsung FRP Bypass (2026): Understanding Google Lock and Android Security
  • ADDROM FRP Bypass APK: Complete Setup Guide for Android Devices (2026)
  • The Complete Guide to Android FRP Bypass: How Factory Reset Protection Works

Recent Comments

  1. Samsung FRP Bypass (2026): Understanding Google Lock and Android Security - CodeWithPK on Addrom Bypass – A Complete Guide for Android Developers
  2. ADDROM FRP Bypass APK: Complete Setup Guide for Android Devices (2026) - CodeWithPK on Addrom Bypass – A Complete Guide for Android Developers
  3. The Complete Guide to Android FRP Bypass: How Factory Reset Protection Works - CodeWithPK on Addrom Bypass – A Complete Guide for Android Developers
  4. 🧩 Chapter 1 – What is Jetpack Compose? (The Cleanest Explanation You’ll Ever Need) – CodeWithPK on 🧩 Chapter 2 – Setting Up Jetpack Compose (From Zero to Running App) πŸš€
  5. Aanand on Future of Native Android Development: Trends, Insights, and Opportunities πŸš€

Recent Post

  • Android Device Security
    12 March, 2026How Android Device Security Works: Bootloader,
  • Android 15
    12 March, 2026Android 15 FRP Security Changes: What
  • Samsung FRP Bypass
    11 March, 2026Samsung FRP Bypass (2026): Understanding Google

category list

  • Android (32)
  • Blog (44)
  • Business News (6)
  • Jetpack Compose (3)
  • Programming (6)
  • Technology (12)

tags

Addrom AI Android architecture Android best practices android developer guide Android developer tips Android Development android device security architecture Android interview preparation android interview questions Android performance optimization Android security Android testing Android Tips Async Code Simplified Asynchronous Programming business news Code Optimization Coding Tips And Tricks Compose tutorial Coroutines Basics data structures and algorithms dependency injection Efficient Code electric vehicles Error Handling In Coroutines google account verification android Jetpack Compose Jetpack Integration Kotlin Coroutines Kotlin For Beginners Kotlin Multiplatform Kotlin Tips Kotlin Tutorial Kotlin Tutorials Kotlin UI Learn Kotlin Mobile App Development Multithreading Simplified Programming Made Easy RBI updates startup updates Structured Concurrency technology news UI Thread Management

Copyright 2026 codewithpk.com All Rights Reserved by codewithpk.com