Nested multi-line comments in java

When writing experimental code, it is very useful for me to comment on all blocks of code at a time. However, I cannot find a reasonable way to do this in Java, because I often end up with nested blocks being commented out.

In C or C #, this is easily achieved using #if 0 or #if false

I cannot find such an equivalent in Java - any help would be appreciated. Thanks!

+6
source share
4 answers

I assume you want to comment on the embedded code for debugging or testing, right? Because leaving huge blocks of code with comments in production code is usually considered very bad.

Nested comments are not originally a java function. So what can you do? Here are a few options:

Insert comments Slash Slash . The best I could come up with was to use an editor that has a hotkey so that slashslash comments out the entire block of code, since // can be nested. Eclipse and Netbeans have such support, although there are slight differences in how it works between the IDE, as far as it decides to switch comments against their nesting. Usually nested // comments can be executed until the selection is identical.

third-party preprocessor . However, J2ME uses the pre-processor in eclipse, but I don't think that would help with java desktop. It seems like other preprocessors written for or compatible with eclipse if you are looking for them. However, this will definitely impede portability, as this is not a standard Java feature.

edit comment Another thing that I often did for a quick nested need for comments (moreso in css that doesn't // make comments than java though) is to add a space between the last two characters ending the inner comment */ , so java will not complete the outer comment at the inner end of the comment. It is not too difficult (but a little manual) to restore the original comment. The nested /* is usually ignored by the parser based on how it matches its pattern. Eg / * code / * nested comment * / more code * / `

use version control . You can also easily use most version control systems to keep a copy of your code with remote code. And then later use the restore / diff / merge version control tool to return your remote code. As a rule, this decision is considered the best style. Especially if this commented code is needed for this for a long time.

if (false) If your code that you want to use does not allow you to run both compilers and is within the same function, you can easily disable it by adding an if statement to it, you will never be right, for example if (false) { ... } Obviously, this will never pass any tool for checking code like lint, but its quick and easy ;-)

+5
source

Comments are not intended to display dead code - they are intended to describe code in real time.

For the rare cases where this is necessary, just use the IDE to comment out the lines you want ( CTRL + / in eclipse).

If this is common practice, rethink your process and your code. And feel free to delete the code and pass it to SCM. He will not be lost if you need it.

+2
source

You can simply specify (false) {}. Java doesn’t really reconfigure how C or C ++ is, but the Java compiler must be smart enough to skip this entire section (once it has determined that this if statement will never evaluate to true). This will only work for code comments inside functions.

Or, if you have a good IDE, such as eclipse or notepad ++, you can use the built-in keyboard shortcuts to comment on multiple lines using //.

Eclipse = ctrl + / (suppress to uncomment) N ++ = ctrl + k (shift + crtl + k to terminate)

+2
source

For Eclipser only:

  1. select the rows you want to commit
  2. press Ctrl + / as soon as you confirm them (with start // in each line)
  3. select the rows (including the rows committed in step 1) that you want to commit
  4. press Ctrl + / again, you will complete all of them

ps: press Ctrl + / , as soon as the lines are fixed, press again to cancel them.

0
source

Source: https://habr.com/ru/post/895533/


All Articles