DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Debugging Tips and Tricks for Python Structural Pattern Matching
  • Effective Engineering Feedback: Software Testing
  • The LLM Selection War Story: Part 2 - The Six LLM Failure Archetypes That Will Wreck Your Production System
  • Agentic Development: My Invisible Dev Team

Trending

  • Every Cache Miss Is a Tiny Tax on Your Performance
  • The Missing `bandit` for AI Agents: How I Built a Static Analyzer for Prompt Injection
  • Event-Driven Pipelines With Apache Pulsar and Go
  • Identity in Action
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Testing, Tools, and Frameworks
  4. Eliminating Bugs Using the Tong Motion Approach

Eliminating Bugs Using the Tong Motion Approach

Delve into a two-pronged strategy that streamlines debugging, enabling developers to swiftly pinpoint and resolve elusive software glitches.

By 
Shai Almog user avatar
Shai Almog
DZone Core CORE ·
Sep. 12, 23 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
2.1K Views

Join the DZone community and get the full member experience.

Join For Free

Software debugging can often feel like a never-ending maze. Just when you think you're on the right track, you hit a dead-end. But, by employing the age-old technique of the process of elimination and using the analogy of the 'Tong Motion,' we can navigate this maze more effectively.

As a side note, if you like the content of this and the other posts in this series, check out my Debugging book that covers this subject. If you have friends who are learning to code, I'd appreciate a reference to my Java Basics book. If you want to get back to Java after a while, check out my Java 8 to 21 book.

Understanding the Process of Elimination in Debugging

The Basics

The process of elimination in debugging is straightforward in principle: continuously rule out non-problematic components until the root cause reveals itself. This can be achieved either by commenting out lines of code or using debugging techniques, such as the 'force return,' which bypasses specific code paths.

Using External Tools

Replicating the problem using tools like Curl or Postman is valuable for front-end issues. It helps us determine if the bug is within the front-end code or elsewhere. This way, we can quickly narrow our focus, not merely addressing the symptoms but locating the actual bug.

The Power of Unit Tests in Debugging

Unit tests are our best allies when it comes to debugging. By focusing on isolated units, they hone in on potential problem areas.

Benefits of Mocking Frameworks

Mocking frameworks like Mockito come in handy as they can simulate large parts of the application. This way, we can drill down on the exact problem, circumventing potential disturbances. Moreover, using mocks can prevent regression and make our test cases cleaner.

However, while there are best practices regarding the extent of mocking when debugging a specific problem, it's more pragmatic to mock as much as necessary to distill the problem to its essence.

The Challenges with Flaky Issues

The elimination technique is less straightforward with flaky issues — those bugs that appear irregularly or whose behavior changes as code is eliminated. The key strategy here is to focus on the negatives. In simpler terms, if removing a certain block doesn't cause the problem to appear, it doesn't automatically indict that block. The absence could be due to the bug's unpredictable nature. Hence, it's crucial only to trust instances where the problem consistently reproduces.

The Concept of the 'Tong Motion'

Think of tongs. They grasp from both sides. Similarly, almost all software has at least two primary interfaces or points of input/output. For instance:

  • Enterprise web apps: Web UI on one side and the database on the other.
  • Operating system kernel: User space app on one end and computer hardware on the other.
  • Video games: The joystick and screen API on one side and the game database on the other.

Applying the Tong Motion to Debugging

Using the example of an enterprise web app:

  1. Mocking the web tier: Begin by using tools like Curl or Postman to eliminate front-end issues.
  2. Mocking the database: Replace the actual database with mock data.
  3. Narrowing down further: If the problem persists, move to test the presentation tier directly, thereby eliminating the database from the equation.
  4. Digging deeper: Invoke the business method directly and mock its dependencies. This way, you are narrowing down on the actual method causing the issue while excluding the rest of the application.

One common pitfall is neglecting one prong of the tongs or misplacing the other. It's crucial to ensure both sides are appropriately positioned; otherwise, it might skew the results. If stuck, consider investigating from the opposite side and then revert when needed.

An Illustrative Case: Debugging a Server Performance Issue

In a real-world scenario, while tackling a server performance issue, I employed the 'Tong Motion' technique. By replacing web calls with curl requests, I shifted focus to the problematic area. At the same time, I enhanced database logging to monitor its output as problematic SQL was replicated through curl. This dual-sided approach helped unearth a bug in the Object Relational Mapping layer.

debugging

This concrete example comprises of the following stages:

  1. The tongs start by mocking the web tier with curl or postman. This eliminates front-end-related issues. The other side of the tong motion replaces the database with mock data.
  2. If the issue can be reproduced, we can further squeeze the tongs by invoking the presentation tier method directly in a test case. We can then eliminate the database entirely from the equation by mocking it in a test case.
  3. Finally, we can invoke the business method directly, eliminating the presentation tier aspect.
    We can mock its dependencies, which means we narrow down on a specific method that’s at fault while eliminating the rest of the application.

Wrapping Up

Debugging can be a daunting process. However, with the right techniques, like the process of elimination and the 'Tong Motion' approach, it becomes a more manageable task. Always remember to tackle issues methodically and from all angles to find and fix the root cause effectively.

Abstract: Once we press the merge button, that code is no longer our responsibility. If it performs sub-optimally or has a bug, it is now the problem of the DevOps team, the SRE, etc. Unfortunately, those teams work with a different toolset. If my code uses up too much RAM, they will increase RAM. If the code runs slower, it will increase CPU. If the code crashes, they will increase concurrent instances.

If none of that helps, they will call you up at 2 a.m. Many of these problems are visible before they become a disastrous middle-of-the-night call. Yes. DevOps should control production, but the information they gather from production is useful for all of us.

Debug (command) unit test

Published at DZone with permission of Shai Almog. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Debugging Tips and Tricks for Python Structural Pattern Matching
  • Effective Engineering Feedback: Software Testing
  • The LLM Selection War Story: Part 2 - The Six LLM Failure Archetypes That Will Wreck Your Production System
  • Agentic Development: My Invisible Dev Team

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook