views:

104

answers:

4
  • 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
+2  A: 

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.

Uri
You make some good cases against avoiding using comments to hide code.
Peter
Thank you. I think that using comments for this in OOP code is bad though I admit I do it too often myself. On the other hand, if I am commenting out too big a block, it often means I could refactor the function into multiple functions and then just not call some.
Uri
I just realised I phrased that comment incorrectly making it a double negative, I meant you make good cases against using comments in this way. Yes, this is why I'm interested in alternatives as I know it's probably not best to do but I always do it.
Peter
I think that having as little of your content as possible hand-coded is always the best. I've suffered a lot from the same problem on static pages.
Uri
+2  A: 
/*
Commented
// */

//*
Not commented
// */
Jason Hall
A: 

I prefer using a text editor that understands the structure of the source text I'm editing, and that can do code folding on the basis of structure as parsed by the editor.

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

bignose
+1  A: 

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.

TokenMacGuy