• Home
  • Categories
  • Video Tutorials
    • Angular 5
  • News
  • About us
  • Contact us
  • Login
test
Code4Developers

Code4Developers

Code4Developers
  • Home
  • Categories
  • Video Tutorials
    • Angular 5
  • News
  • About us
  • Contact us
  • Login
  • JavaScript

Debugging With Black Box – Javascript

  • Arif Khoja
  • July 23, 2018
  • 3 minute read
javascript
Total
0
Shares
0
0
0

By the end of 2013 Firefox launched a tool called Black Box to their browser inspector. About a year later, Chrome did the same. If you need to carry out debugging in your code but don’t know about black boxing scripts then you should definitely give this post a read.

What is Black boxing?

black boxing allow you to disable scripts for debugging, so that you can focus on debugging the scripts that are relevant. E.g.  you are debugging JavaScript an Angular/React app which is using underscore and jQuery under the hood. Most likely you have a bunch of other favorite plugins/libraries/frameworks. When we debug, we most likely don’t want to debug all those vendor scripts. Mostly, we just want to debug our own scripts or maybe just some of them. This is when black box comes in handy. We can black list all scripts that are not relevant for this debugging case.

How would we normally do it?

We would Parse through the code with the buttons that are shown in the image below.

Debugging

  • For next function call – step over
  • To step in to next function call – step in
  • To step out of this function call – step out

That’s ok but…

When ever we debug, we have to think – “Should I click step-in-to or step-over?” Also another question that strikes is – “Is this a vendor function or is it a function I wrote?”

After selection/click of “step-in-to or step-over” in next line we have to ask the same question again. So if you’re stepping through 10 lines, you have to choose between step-over or step-in-to 10 times. If you do this debugging procedure 10 times you have to ask the question 100 times. So here i guess what is the problem we’re getting at.  How annoying it is as it is a minor thing that you have to do very often.

With black box we can decide beforehand. Which scripts are not relevant for this debugging? Black box them and then you don’t need to worry about stepping in to irrelevant functions. A common usage would be to black list vendor scripts for instance jQuery, backbone, AngularJs and underscore.

Let’s Understand with Example

Below given example will demonstrate the difference of debugging with and without black box.

On click of the body element, we’re fetching two elements from the DOM. One of them is undefined. We pass the elements to the function foo, and then we calculate the length on them. Since one element is undefined on-line 11 it will cause an error. This is the bug we want to hunt down as quickly as possible.

<body>

<div class="element"></div>

<script src="https://code.jquery.com/jquery-2.1.4.min.js" type="text/javascript"></script>

<script>

	var foo = function( element, undefElement ){		
		element.get(0).length;
		undefElement.get(0).length;
	};

	$('body').click( function() {
		debugger; 
		var element = $('.element');
		var undefElement = $('.undefined');
		foo( element, undefElement );
	});

</script>

</body>

Since we don’t want to debug inside jQuery we will black list it. Now the step-in-to button becomes something we would like to call step-in-to-if-relevant. In this case we can use step-in-to all the time without the worry of ending up in jQuery scripts. Even though we click step-in-to, the debugger will do step-over on-line 16,17 and 10,11 since they are jQuery functions.

How to use Blackbox in regards of different browsers?

Chrome

– Alt 1. Click on the cog to open up the settings view. Click the button “manage framework black boxing”. In the new pop up you can write a regexp as a path. This is great if you for example want to black box the entire vendor folder.

– Alt 2. Go to source in the inspector. Right click the file you want to black list and choose black box script.

Read more here.

Firefox

Go to the debugger tab in the inspector. Mark the script you want to black list. Click on the eye in the very bottom left corner.

Read more here

Conclusion

This feature makes it much easier to keep your debugging in the right scope. I hope you found this article useful and it saves you some time. Do let us know in Comments if you want more of this kind of tips and tricks articles to make your work fun and easy.

Arif Khoja
Arif Khoja

Arif Khoja is a Developer. He is a Javascript Enthusiatic who loves logical programming and has first hand experience in building a cutting edge internet product using Angular. He is also an open source freak and keen about learning and sharing. He writes Javascript both frontend and backend. He loves learning and sharing tech all the time. He also has a hands on experience in SEO and writes articles about latest emerging technologies.

Views: 5,043

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on WhatsApp (Opens in new window) WhatsApp
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on X (Opens in new window) X
  • Click to share on Pinterest (Opens in new window) Pinterest
  • Click to email a link to a friend (Opens in new window) Email
  • Click to print (Opens in new window) Print

Like this:

Like Loading...

Related Posts

Total
0
Shares
Share 0
Tweet 0
Pin it 0

Leave a ReplyCancel reply

Subscribe to Website via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Recent Posts
  • React Hooks Guide: Top Tips for Optimizing Performance in Your React Applications

    React Hooks Guide: Top Tips for Optimizing Performance in Your React Applications

    1 year ago
  • Demystifying JavaScript Tree Shaking: Boosting Performance and Reducing Bundle Size

    Demystifying JavaScript Tree Shaking: Boosting Performance and Reducing Bundle Size

    2 years ago
  • Unlocking the Power of React Hooks: A Comprehensive Guide with Examples

    Unlocking the Power of React Hooks: A Comprehensive Guide with Examples

    2 years ago
  • Celebrating a Decade of Phenomenal Growth: Insights and Reflections on 10 Years of Software Engineering

    Celebrating a Decade of Phenomenal Growth: Insights and Reflections on 10 Years of Software Engineering

    2 years ago
  • Angular Custom Elements: Creating Reusable Components with Angular

    Angular Custom Elements: Creating Reusable Components with Angular

    2 years ago
  • Connect Firebase Realtime NoSQL Database with Angular App from Scratch

    Connect Firebase Realtime NoSQL Database with Angular App from Scratch

    5 years ago
  • How to Build an Inclusive Esports Community

    How to Build an Inclusive Esports Community

    5 years ago
  • Best Digital Icebreakers

    Best Digital Icebreakers

    5 years ago
  • Email alerts when a docker container stopped in AWS ECS CLUSTER

    Email alerts when a docker container stopped in AWS ECS CLUSTER

    5 years ago
  • New Learning Models for Fall 2020

    New Learning Models for Fall 2020

    5 years ago
Subscribe to Website via Email

Enter your email address to subscribe to this website and receive notifications of new posts by email.

Featured Posts
  • javascript 1
    Spread syntax (three dots) in JavaScript
    • March 21, 2018
  • Angular 2
    Angular 6 CRUD – Part 1: Project Setup, Routing, Service
    • May 9, 2018
  • javascript 3
    Local Storage and Session Storage
    • May 22, 2017
  • Angular 4
    Angular 4 Project Structure
    • June 18, 2017
  • AWS 5
    Email alerts when a docker container stopped in AWS ECS CLUSTER
    • July 24, 2020
Code4Developers
Learning is never ending process

Input your search keywords and press Enter.

 

Loading Comments...
 

    %d