Question

  • Do you toggle comments or slabs of code to quickly show or hide content?
  • What are some common methods you use?
  • Is there an accepted standard?
  • Should some methods be avoided? ie could they be misinterpreted by some engines?
  • Is there an alternative or better solution to this?

Standard - This is what I use to cover most languages: CSS, JavaScript, PHP, ActionScript

/**/ visible /**/
/**\/ hidden /**/

HTML

<!----> visible <!---->
<!----/> hidden <!---->

PHP - Defining something like $hide works well, other variables could be production or dev - large slabs can then be hidden and shown together with one simple variable change.

if(0){ hidden }
if(1){ visible }
if(!$HIDE){ content } // $HIDE defined elsewhere, visible if undefined
Was it helpful?

Solution

I think that if you can programmatically control what is being rendered, that is best (like what you did in the PHP). An even better solution that what you wrote (essentially a local preprocessor macro) is to actually break up your rendering code into functions that generate sub parts of the documents. If you don't need it, you don't call it, and you have a clear condition in the code. This is, for example, the way MediaWiki is written. Otherwise, in complex projects, it becomes a mess.

There are many risks to hard coding the comment-out in code. Among them:

  • Very easy to mess up the uncommenting

  • Not clear what was commented.

  • Problem when commented out sections overlap

  • Cut and paste errors

  • Everything that you wanted to hide is still accessible, affects search engines, etc.

OTHER TIPS

/*
Commented
// */

//*
Not commented
// */

I've been told by some people that the proper way to comment out sections of C++, since we apparently don't use the preprocessor any more is to use an if statement.

if(false)
{
    chunk of code;
}

The theory being that the compiler will optimize that dead code out of the final product, and They can be nested, which /* */ style comments cannot do.

That theory is bogus, of course, because you can't use it to comment out arbitrary regions of code, for instance:

class Foo
{
    void Bar();
    if(false)
    {
        int Baz(double); // WRONG!!!
    }
};

On the other hand, this works perfectly for languages like python that allow any statement in any block, and which also lack a standard preprocessor or block comments.

I prefer using a text editor that understands the structure of the source text I'm editing (Vim and Emacs are the venerable favourites), and that can do code folding on the basis of structure as parsed by the editor.

Either of Vim folding or Emacs folding meets this need for me.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top