While this isn’t a language construct (a comment is a comment after all), Visual Studio can key off these stylistic differences to show code comments vs commented code differently. I find it to be a neat trick to improve code readability.
I've never touched C#. But that seems like an approach applicable pretty generally. I like it!
There’s a StyleCop rule to enforce this as well:
https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/ma...
https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/ma...
StyleCop is used for analyzing code style in C# code.
Specifically:
> If the comment is being used to comment out a line of code, begin the comment with four forward slashes rather than two.
StyleCop relies on this to tell the difference between commented code and code comments. If you use ReSharper, it can do some nifty transformation to strike these out in the IDE which is kinda cool too.
I’m a big fan of StyleCop to enforce consistent code styles. (Even if it’s not always what I may aesthetically prefer.)
print "foo"
=pod
print "won't run"
=cut
Ruby basically copied it, although I don't think these have documentation uses: puts "foo"
=begin
puts "won't run"
=end
When I get to choose, I prefer sticking to // in C code so that I can use /* ... */ for hiding large blocks of code . . . although #if false ... #endif work too. #if 1
....new code...
#else
....old code...
#endif
Esp. for tricky stuff, it's then possible to just flip the 1/0 to easily test/compare/profile the new/old code.Some IDE's will even work out which block of code to colour and which to dim.
You can just use a life if-else, most compilers will optimize the test-on-a-constant away. Even when the code is kept in, I doubt it will have a major performance impact during testing in most cases.
#if !defined(this_works_but_it_suck_memory_too_hard)
....
#else
....
#endif
That should give me a clue why I "disabled" it when I come back to it 5 minutes later. //*
old_code();
/*/
new_code();
//*/
And remove the first slash from the first line to switch between blocks.(Of course, if there's a stray /* ... */ comment already in the area to be commented out, this will fail. But an editor with appropriate syntax colouring can make that immediately apparent.)
I think the space looks nicer for text comments, but when I select a block of code and hit Ctrl+/ to comment it out, my IDE does not add a space. So it works out very conveniently for me.
version (all)
{
... new version ...
}
else
{
... old version ...
}
and change `all` to `none` to try the other branch. // FIXME try new way mm/dd/yy
if(1)
{
... new way ...
}
else
{
... old way ...
}Doing this too early feels wrong; like I’m prematurely committing to a division of responsibility that’s going to subtly drive me to a bad design.
I do this all the time in Emacs, usually to see two parts of the file that normally are too far apart to be visible at the same time. Collapsible sections can do similar, but to me it's quicker, more natural and more flexible to just split the window.