Web Design Forum: TUTORIAL: Comment Switches - Web Design Forum

Jump to content

WDF
WDF Premium Memberships Reseller Hosting
Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

TUTORIAL: Comment Switches Possibly the most useful piece of code I know Rate Topic: -----

#1 User is online   Skateside 

  • Advanced Member
  • PipPipPip
  • Group: Members
  • Posts: 464
  • Joined: 09-November 08
  • Reputation: 44
  • Location:Bedford
  • Experience:Advanced
  • Area of Expertise:Coder

Posted 19 January 2010 - 08:33 PM

Comment switches are comments that hide parts of your code while keeping other parts active. They allow simple debugging and allow you to "go back to the drawing board" without having to re-write everything you've had to remove.

I first saw these on another forum and I've named them after the users that first introduced me to them. I don't know if they have another, official, name and I would be fascinated to learn whether they do.

The Jaap Comment Switch
The Jaap Comment Switch toggles two pieces of code. There is no limit to the amount of code this can hide.

Availability
This comment switch works in:
  • JavaScript
  • PHP
  • ActionScript
  • any other language that has both single-line and multi-line comments


Usage
The switch is very straight forward:
/*
	previous_function();
/*/
	new_function();
//*/

previous_function() is completely hidden and will not fire. new_function() is active.

Switching
To switch this code, all you need to do is add a slash to the first line.
//*
	previous_function();
/*/
	new_function();
//*/

Now, previous_function() will fire while new_function() is hidden.


The Phlip Comment Switch
The Phlip Comment Switch allows many pieces of code to be hidden and activated.

Availability
This comment switch works in:
  • CSS
  • JavaScript
  • PHP
  • ActionScript
  • any other language that has multi-line comments


Usage
This comment switch requires the last line to be left alone.
/** /
	function_one();
/** /
	function_two();
/** /
	function_three();
/**/

In this example, all three functions are hidden.

Switching
To switch this code, remove or replace the space in the comment before the piece of code you'd like to activate or hide.
/**/
	function_one();
/** /
	function_two();
/** /
	function_three();
/**/

function_one() will now fire while the other two are hidden.
Multiple code pieces can be active at the same time. For example, the following piece of code will fire function_one() and function_three() while keeping function_two() hidden.
/**/
	function_one();
/** /
	function_two();
/**/
	function_three();
/**/



Conclusion
I always find these switches useful when I'm working on a busy site, allowing me to write new code and test it while being able to replace it with the original (working) code in seconds.

I hope everyone here finds a use for these comment switches. Please feel free to reply with any other switches you've come across
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users