Code Bank
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsProgrammingCode Bank

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old January 11th, 2008, 12:53 PM
mattgb1 mattgb1 is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: May 2007
Location: Brooklyn, NY
Posts: 133 mattgb1 User rank is Second Lieutenant (5000 - 10000 Reputation Level)mattgb1 User rank is Second Lieutenant (5000 - 10000 Reputation Level)mattgb1 User rank is Second Lieutenant (5000 - 10000 Reputation Level)mattgb1 User rank is Second Lieutenant (5000 - 10000 Reputation Level)mattgb1 User rank is Second Lieutenant (5000 - 10000 Reputation Level)mattgb1 User rank is Second Lieutenant (5000 - 10000 Reputation Level)mattgb1 User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Day 7 h 6 m 58 sec
Reputation Power: 57
Lightbulb Making Javascript act more like VB in the IDE

Hi fellow coders

I would like to share a little technique I've developed that helps me develop software in JavaScript. Being a VB programmer from way back, I have grown thoroughly spoiled when it comes to IntelliSense and the ability to step through a program's execution at runtime. Such an advantage is not available to me in JavaScript programming, especially in a browser environment where script debugging is disabled (often a required condition at business sites.

So I do the next best thing: I use the alert() function throughout my code.

However, doing so ends up either littering my code with gazillions of commented-out alert() statements, or it requires me to keep inserting them. Additionally, it can be hard to find an alert's home in the code when it has so many look-alikes.

My first approach to this problem was to include line numbers in my alerts. For example, if the code line 1100 sets a value that I want to test, I might follow it up with an alert that looks like this:
PHP Code:
 document.getElementById("hdnAdminRowID").value=adminRowID;  
alert('1101. adminrow = 'document.getElementById("hdnAdminRowID").value); 

In this case, the alert() will not only inform me of the value in some hidden control, it will also tell me where to look in the code in case I need to check my logic any further.

But my code was getting cluttered, so I decided to encapsulate the alert() function into something of my own that would manage the process more effectively.
PHP Code:
function DebugAlert(LineNumberDebugMessageRestrictionCode)
        {
        
// zero means all, blank means none. else means that one value
            
if (document.getElementById("hdnDebug").value!="")
            {
                if (
document.getElementById("hdnDebug").value=="0")
                    
alert(LineNumber +'. 'DebugMessage);
                else
                    {
                    if(
RestrictionCode==document.getElementById("hdnDebug").value)
                        
alert('DebugAlert:\n'+LineNumber +'. 'DebugMessage);
                    }                            
            }
        } 
My first paremeter is the line number in code, the second parameter is the comment I want to raise, but the real benefit is in the third parameter, the RestrictionCode. Notice the commented line that explains this. It will make sense if we take a look at the hidden value called hdnDebug.

Code:
    <%--values to use: blank for none, 0 for all, other values for specific--%>
    <input type="hidden" id="hdnDebug" value=""/> <%--next unused value is 10--%>


At design time, I might set it to "" (as in this case), which is my way of telling my app to ignore all alerts.

Now let's say that I'm testing some complex process in my code where I want alerts in several different places. What I'll do is assign a number, say 5, as the RestrictionCode for all of these alerts, and only these.

PHP Code:
var msg='SetVsErrorDialogPermission, with choice = '+choice;
    
msg +='\nhdnAllowVsErrorDialog.value has been set to ';
    
msg+=document.getElementById("hdnAllowVsErrorDialog").value
    DebugAlert
(118msg,5); 

Above is an example where I gather up data for a rather verbose message that I want to raise at line 118 in code. I set the RestrictionCode parameter to 5, store that value in hdnDebug, and run the app. Any DebugAlert() command that doesn't have a RestrictionCode of 5 will be ignored, and the messages of interest will pop into view when needed.

Conversely, if I want to go all-out debug crazy, all I have to to is put "0" into the hdnDebug control, and every DebugAlert() command will cause an alert() to occur.

Finally, as you may have guessed, it might be tough to keep track of the RestrictionCode values in use. In the example above, my HTML has a comment telling me about the next unused value, which I simply update as needed.
Code:
<%--next unused value is 10--%>


I could also construct an extended comment that specifies what each RestrictionCode is for:
Code:
        <%--values to use: blank for none, 0 for all, other values for specific--%>
        <input type="hidden" id="hdnDebug" value=""/> <%--next unused value is 10--%>
        <%--Codes in use:
            1 - Testing login process
            2 - Comparing string values returned by Employee Data
            3 - Testing SQL data source behavior
            4 - For comparing visual feedback in UI to underlying logical values
                in connection with the UpdateFinancials() process--%>


Oh, and since DebugAlert() is a function I declare on my web page, I get to use IntelliSense while writing calls to it.

This whole business evolved as I taught myself to develop in JavaScript and wanted it to behave more like VB. I'm sure that the evolution will continue, but I thought some of you might find it useful in the mean time.

Enjoy!

- Matt
Comments on this post
jmurrayhead agrees!
AOG123 agrees!
mehere agrees!
sbenj69 agrees!
richyrich agrees: Nice Code...
Lauramc agrees: Very Nice Work!
Salman8506 agrees: Keep up the Good Work
micky agrees!

Last edited by mattgb1 : January 11th, 2008 at 12:59 PM. Reason: thought of something else

Reply With Quote
Reply

Viewing: ASP Free ForumsProgrammingCode Bank > Making Javascript act more like VB in the .net IDE


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT