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
  • 56 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

  • 🧩 Chapter 4 – Layouts in Jetpack Compose: Row, Column, Box, Arrangement & Lists
  • 🧩 Chapter 3 – Text, Image, Button & TextField Composables (and the Secret Sauce: State πŸ”)
  • 🧩 Chapter 2 – Setting Up Jetpack Compose (From Zero to Running App) πŸš€
  • 🧩 Chapter 1 – What is Jetpack Compose? (The Cleanest Explanation You’ll Ever Need)
  • Massive News for Android Developers Regarding Play Store! πŸš€

Recent Comments

  1. 🧩 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) πŸš€
  2. Aanand on Future of Native Android Development: Trends, Insights, and Opportunities πŸš€

Recent Post

  • Layouts in Jetpack Compose
    11 November, 2025🧩 Chapter 4 – Layouts in
  • Chapter 3 – Text, Image, Button
    10 November, 2025🧩 Chapter 3 – Text, Image,
  • Jetpack Compose Setup
    08 November, 2025🧩 Chapter 2 – Setting Up

category list

  • Android (25)
  • Blog (35)
  • Business News (6)
  • Jetpack Compose (3)
  • Programming (6)
  • Technology (5)

tags

AI Android architecture Android best practices android developer guide Android developer tips Android Development Android interview preparation android interview questions Android performance optimization Android testing Android Tips Async Code Simplified Asynchronous Programming business news Clean Code Code Optimization Code Quality Coding Tips And Tricks Compose tutorial Coroutines Basics data structures and algorithms dependency injection Dirty Code Efficient Code electric vehicles Error Handling In Coroutines Jetpack Compose Jetpack Integration Kotlin 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 2025 codewithpk.com All Rights Reserved by codewithpk.com