Multiline Comment Workarounds

Multiline Comment Workarounds?

This does come up on the mailing list fairly regularly, see for example this recent thread on r-help. The consensus answer usually is the one shown above: that given that the language has no direct support, you have to either

  • work with an editor that has region-to-comment commands, and most advanced R editors do
  • use the if (FALSE) constructs suggested earlier but note that it still requires complete parsing and must hence be syntactically correct

What are the common workarounds for multi-line comments in Perl?

The downside of the "if" solution is that the commented out code still has to be compiled (and therefore still has to be syntax checked).

The downside of your pod solution is that your comments will appear in any documentation generated from the pod.

I use a version of the pod solution that doesn't have that problem. Pod supports =begin format ... =end format paragraphs that are handled by specific formatters. I just invent a "comment" format that isn't handled by any of the formatters I use.

#!/usr/bin/perl

print "This line is executed\n";

=begin comment

print "This line isn't\n";

=end comment

=cut

print "This line is\n";

How to comment multiple lines in Visual Studio Code?

Windows: Shift+Alt+A

Linux: Shift+Ctrl+A

Here you can find all the keyboard shortcuts:

Windows VSCode Shortcuts

Linux VSCode Shortcuts

PS: I prefer Ctrl+Shift+/ for toggling block comments because Ctrl+/ is shortcut for toggling line comments so it's naturally easier to remember. To do so, just click on the settings icon in the bottom left of the screen and click 'Keyboard Shortcuts' and find "toggle block...". Then click and enter your desired combination.

Is it possible to have a multi-line comments in R?

You can, if you want, use standalone strings for multi-line comments — I've always thought that prettier than if (FALSE) { } blocks. The string will get evaluated and then discarded, so as long as it's not the last line in a function nothing will happen.

"This function takes a value x, and does things and returns things that
take several lines to explain"
doEverythingOften <- function(x) {
# Non! Comment it out! We'll just do it once for now.
"if (x %in% 1:9) {
doTenEverythings()
}"
doEverythingOnce()
...
return(list(
everythingDone = TRUE,
howOftenDone = 1
))
}

The main limitation is that when you're commenting stuff out, you've got to watch your quotation marks: if you've got one kind inside, you'll have to use the other kind for the comment; and if you've got something like "strings with 'postrophes" inside that block, then there's no way this method is a good idea. But then there's still the if (FALSE) block.

The other limitation, one that both methods have, is that you can only use such blocks in places where an expression would be syntactically valid - no commenting out parts of lists, say.

Regarding what do in which IDE: I'm a Vim user, and I find
NERD Commenter an utterly excellent tool for quickly commenting or uncommenting multiple lines. Very user-friendly, very well-documented.

Lastly, at the R prompt (at least under Linux), there's the lovely Alt-Shift-# to comment the current line. Very nice to put a line 'on hold', if you're working on a one-liner and then realise you need a prep step first.

How do I enter a multi-line comment in Perl?


POD is the official way to do multi line comments in Perl,
  • see Multi-line comments in perl
    code
    and
  • Better ways to make multi-line comments in Perl for more
    detail.

From faq.perl.org[perlfaq7]

  • How can I comment out a large block
    of Perl code?

The quick-and-dirty way to comment out more than one line of Perl is
to surround those lines with Pod directives. You have to put these
directives at the beginning of the line and somewhere where Perl
expects a new statement (so not in the middle of statements like the #
comments). You end the comment with =cut, ending the Pod section:

=pod

my $object = NotGonnaHappen->new();

ignored_sub();

$wont_be_assigned = 37;

=cut

The quick-and-dirty method only works well when you don't plan to
leave the commented code in the source. If a Pod parser comes along,
your multiline comment is going to show up in the Pod translation. A
better way hides it from Pod parsers as well.

The =begin directive can mark a section for a particular purpose. If
the Pod parser doesn't want to handle it, it just ignores it. Label
the comments with comment. End the comment using =end with the
same label. You still need the =cut to go back to Perl code from the
Pod comment:

=begin comment

my $object = NotGonnaHappen->new();

ignored_sub();

$wont_be_assigned = 37;

=end comment

=cut

Multi line comment issue in Python 3

I show the problem by an arrow in your code:

"""

tabby_cat = "\tI'm tabbed in."
persian_cat = "I'm split\non a line."
backslash_cat = "I'm \\ a \\ cat."

fat_cat = """
I'll do a list
\t* Cat food
\t* Fishies
\t* Catnip\n\t* Grass
""" <-----------------------------------------DELETE THIS ONE OR ADD ANOTHER ONE

print(tabby_cat)
print(persian_cat)
print(backslash_cat)
print(fat_cat)

"""

R: Comment out block of code

Most of the editors take some kind of shortcut to comment out blocks of code. The default editors use something like command or control and single quote to comment out selected lines of code. In RStudio it's Command or Control+/. Check in your editor.

It's still commenting line by line, but they also uncomment selected lines as well. For the Mac RGUI it's command-option ' (I'm imagining windows is control option). For Rstudio it's just Command or Control + Shift + C again.

These shortcuts will likely change over time as editors get updated and different software becomes the most popular R editors. You'll have to look it up for whatever software you have.

What is the reason behind not having a simpler multi-line comment in Perl?

C-style comments come with a variety of difficult problems, including the inability to comment-out comments. Additionally, convenient syntax for one-line encourages concise writing, which makes your code easier to read. The absence of 10-line "Author: ... Input: ... Output: ... Favorite Color: ..." blocks that you see from time to time in C and C++ is another benefit.

Multi-line comments encourage long writing that is better expressed more concisely or as documentation, so this is what Perl encourages with its # and =pod operators (respectively).

Finally, if you are having trouble with Perl's style of commenting, you should configure your editor to edit programs, not text. In Emacs, if you start writing something like # this is a comment that is getting longer and longer and longer and oh my goodness we are out of space on this line what to do now and type M-q, Emacs will automatically insert the # at the beginning of each line that it creates.

If you just want to comment-out a block of code, then you need look no further than M-x comment-region and M-x uncomment-region, which will comment out the region in pretty-much any language; including Perl.

Don't stress about the syntax of comments; that's the computer's job!

Why doesn't Python have multiline comments?

I doubt you'll get a better answer than, "Guido didn't feel the need for multi-line comments".

Guido has tweeted about this:

Python tip: You can use multi-line strings as multi-line comments. Unless used as docstrings, they generate no code! :-)

Multiline comment in Elixir

Elixir does not have multiline comments.

However, one very common use case for multiline comments is documenting modules and functions, for which you can use the module attributes @doc and @moduledoc together with heredocs.

defmodule MyModule do
@moduledoc """
This module is great at X
"""

@doc """
Frobnicates the given string.
"""
def frobnicate(s) do
end
end


Related Topics



Leave a reply



Submit