< Die Webkrauts wollen es wissen! Webworker-Umfrage 2008 | Codeteile mit Kommentaren schnell ein- und ausschalten >

2008-02-27 Switch comments - enabling and disabling Codeparts fast and easy

Deutsche Version auch verfügbar

I always thought everybody knows this little Trick already because I'm using this for many years now. Apparently not.

So here's the Hint that helps a lot during everyday coding. Because I don't know if there's a proper naming already I call it "Switch Comments".

While working on Code it happens a lot you need to switch parts of Code on and off for testing. To do so you can comment and uncomment the Lines using // or /**/ Comments.

With longer code parts this can become a real pain. But using Switch Comments you can easily do it by changing just one Character.

Switch Comments are a Combination of Single and Multi line Comments. They start with a: //* and end again with // */. Code that is wrapped that way is fully functional. By simply removing one of the / from the starting Comment the whole Block will be deactivated.

Here's the Example:

Active Comment:


demo = {
    /*
    someTest.Javascript();
    toDemonstrate = 'how Easy';
    // */

    it.is('to make Code appear');
    and = ['disappear','again'];
    this.isVeryUseful('indeed');
};
 
Inactive Comment:

demo = {
    //*
    someTest.Javascript();
    toDemonstrate = 'how Easy';
    // */
    it.is('to make Code appear');
    and = ['disappear','again'];
    this.isVeryUseful('indeed');
};
 

Sadly CSS fails because of the missing Support for // Comments. But Javascript, PHP and many more work nicely with this helpful little thingy.

Update:
My Collegue Steve Webster came up with the logic alternative for CSS:

Comment active:


.test{
    border:1px solid red;
    /*/
    background-color:blue;
    /**/

    margin:1em;
}
 
Comment inactive:

.test{
    border:1px solid red;
    /**/
    background-color:blue;
    /**/
    margin:1em;
}
 

Trackbacks
No Trackbacks

Comments

No comments
Add Comment

Standard emoticons like :-) and ;-) are converted to images.
E-Mail addresses will not be displayed and will only be used for E-Mail notifications

To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.
CAPTCHA

Gravatar/Favatar/Pavatar/MyBlogLog author images supported.
You can use [geshi lang=javascript/css/html4strict/php]your code[/geshi] tags to embed source code snippets