Simple web dev. debugging methods
PostPosted: Thu Jun 05, 2014 5:50 am
Was stuck on some code at work today so thought to share some simple debugging methods I use for web development (applies to Javascript and CSS, but the logic can be applied to pretty much any computer language).
1. Test whether code enters certain loops / conditionals by placing 'alert' statements within.
Example:
if (answer == 'yes')
{
alert('answer is yes');
.......
}
If the alert does not appear it means somehow the code has failed to enter that conditional statement, which means it's a hint to check what the value of 'answer' is.
2. Comment / un-comment code to look for statements that break the rest of your code.
I was pretty sure my syntax was correct, but when I ran my code in Chrome, it kept throwing me an undefined error.
I then commented out the conditional that evaluated the variable, and the code ran fine. That made me realise Google Chrome is not knowing about the variable's value.
After reading around I found that Chrome has a modalDialog bug in that window.returnValue isn't passed back to the calling function. It also looks this bug isn't getting fixed anytime soon (https://code.google.com/p/chromium/issu ... ?id=324392).
So I ended up writing a workaround for it (ditched modalDialog and went for the JS confirm option - cheap, but it does the job).
3. If the issue is with display / alignment of HTML elements on a web page, try to give all the elements an outline, like so:
<style>
.testDiv { outline: #f00 solid 1px; }
</style>
then apply like so:
<body>
.....
<div class="testDiv">Some text here</div>
.....
</body>
The thing about outlines are that they do not take up extra space (unlike the CSS border property), so you can apply them to check where the elements start and end without being concerned that the outline is responsible for the breaking (since there is no width getting pushing them out by the extra defined width).
4. Last but probably one of the most effective non-tech debugging method - is to go get some fresh air, wash your face, or drink some water / juice. It can do wonders for you.
1. Test whether code enters certain loops / conditionals by placing 'alert' statements within.
Example:
if (answer == 'yes')
{
alert('answer is yes');
.......
}
If the alert does not appear it means somehow the code has failed to enter that conditional statement, which means it's a hint to check what the value of 'answer' is.
2. Comment / un-comment code to look for statements that break the rest of your code.
I was pretty sure my syntax was correct, but when I ran my code in Chrome, it kept throwing me an undefined error.
I then commented out the conditional that evaluated the variable, and the code ran fine. That made me realise Google Chrome is not knowing about the variable's value.
After reading around I found that Chrome has a modalDialog bug in that window.returnValue isn't passed back to the calling function. It also looks this bug isn't getting fixed anytime soon (https://code.google.com/p/chromium/issu ... ?id=324392).
So I ended up writing a workaround for it (ditched modalDialog and went for the JS confirm option - cheap, but it does the job).
3. If the issue is with display / alignment of HTML elements on a web page, try to give all the elements an outline, like so:
<style>
.testDiv { outline: #f00 solid 1px; }
</style>
then apply like so:
<body>
.....
<div class="testDiv">Some text here</div>
.....
</body>
The thing about outlines are that they do not take up extra space (unlike the CSS border property), so you can apply them to check where the elements start and end without being concerned that the outline is responsible for the breaking (since there is no width getting pushing them out by the extra defined width).
4. Last but probably one of the most effective non-tech debugging method - is to go get some fresh air, wash your face, or drink some water / juice. It can do wonders for you.