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 Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. JavaScript
  4. Know Your Debugger Part 2: Ok, I'm Broken. Now What?

Know Your Debugger Part 2: Ok, I'm Broken. Now What?

Dane Morgridge user avatar by
Dane Morgridge
·
Aug. 27, 10 · Interview
Like (0)
Save
Tweet
Share
2.89K Views

Join the DZone community and get the full member experience.

Join For Free
So now you know how to set breakpoints and pause execution of your application.  There are a lot of really cool things you can do in this state.  You can inspect variable values and even change them (more on that part later). If you continue to step through your code you can follow execution line by line.  This can be very helpful if your application is not behaving as you expect or if you need to just confirm that a certain if statement is evaluated properly.  My biggest use for the debugger has been to verify execution and hunt down issues.

I'm sure that all of us at some point have needed to find out the value of an object or variable in code and put maybe a Console.WriteLine or maybe a javascript alert to give you the value.  While this technically works, the debugger can help you so much more.  Below is a method that I will debug to demonstrate the awesomeness of the Visual Studio debugger. 
int CrazyMath(int value)
{
value += 2;

value *= 5;

string description = string.Format("The value is: {1}", value);

value = 7;

return value;
}
There a few things going on here, mainly the changing of the "value" int that is passed in to the CrazyMath method.  If we set a breakpoint on line {line number} we can inspect the value at this point by mousing over the variable:



You can see that the value is 3 because that's what we passed into the method.  Note that the breakpoint defines the line that is about to get executed so the addition of 2 to the value hasn't happened yet.  If I hit F10 to step over and mouse over value again, you can see it is now 5:



The debugger is now on the line that will take the value and multiply it by 5.  If I hit F10 again, you can see that the value is now 25 as expected:



If you continue to step through this you will see how the value changes and how that the string description is getting set properly.  While mousing over variables certainly works, you may want to use the watch windows if there is a variable or statement that you want to monitor over time.  When your code is broken, you can right click on value and select either "Add Watch" or "Add Quick Watch".  "Add Watch" will add the variable to the watch window for the life of the Visual Studio session and "Add Quick Watch" will pop up a smaller watch window that will go away once you are done with the debugging session:

Add Watch Window:


Quick Watch Popup:


You don't have to click on the variables and add them to get them in a watch window, you also can just type them in a new line.  There is even intellisense when doing so.  What is really cool about the watch windows is that you can watch any variable or even statements.  I can go ahead and add a watch of (value == 25) and you can see that if the value is not 25 it will show false:



And as soon as the value is 25, it will show true:



The debugger is very powerful and can give you some additional tools to make sure that your application is behaving as expected or can help you track down issues.  One thing to note however, if you are inspecting an IQueryable<T> with the debugger, the debugger will cause it to be queried and also any IEnumerable will be enumerated.  This can lead to some unexpected results as you may not be ready to query or enumerate over the collection.  If you haven't worked with watch windows before, Give it a try, you won't be disappointed.
application Execution (computing) Session (web analytics) Data Types Debug (command) Strings Monitor (synchronization) JavaScript

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Microservices 101: Transactional Outbox and Inbox
  • Strategies for Kubernetes Cluster Administrators: Understanding Pod Scheduling
  • DevOps for Developers — Introduction and Version Control
  • A Deep Dive Into AIOps and MLOps

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: