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

  • πŸ”₯ The End of Flutter & React Native? Jetpack Compose Is Now Stable for iOS!
  • My Mini Heart Attack πŸ˜΅β€πŸ’« About Android 19 – A Developer’s Honest Moment
  • How to Use DeepSeek AI Models in Android Apps 🌟
  • πŸŽ₯ Ever Wondered How Netflix Works So Well? Here’s the Secret! πŸš€
  • REST API Cheat Sheet I Wish I Had Created Before πŸš€

Recent Comments

  1. codewithpk@720 on Future of Native Android Development: Trends, Insights, and Opportunities πŸš€
  2. Aanand on Future of Native Android Development: Trends, Insights, and Opportunities πŸš€

Recent Post

  • jetpack compose
    09 May, 2025πŸ”₯ The End of Flutter &
  • android 19
    18 April, 2025My Mini Heart Attack πŸ˜΅β€πŸ’« About
  • How to Use DeepSeek Model in Android Apps
    28 January, 2025How to Use DeepSeek AI Models

category list

  • Android (18)
  • Blog (26)
  • Business News (6)
  • Programming (6)
  • Technology (4)

tags

AI AI Revolution 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 Code Optimization Coding Tips And Tricks Coroutines Basics data structures and algorithms dependency injection Efficient Code electric vehicles Error Handling In Coroutines Google CEO Innovation Jetpack Compose Jetpack Integration Kotlin Kotlin Coroutines Kotlin For Beginners Kotlin Multiplatform Kotlin Tips Kotlin Tutorial Kotlin Tutorials Learn Kotlin Mobile App Development Multithreading Simplified Programming Made Easy Quantum Computing Breakthrough RBI updates startup updates Structured Concurrency Tech News technology news UI Thread Management

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