Difference between // and /*...*/

Asked

Viewed 830 times

3

As far as I knew, use // to comment on a line and /*...*/ was the same thing, with the difference that the second covered more than one line if needed.

Is there ANY difference between doing

 // var a = 'teste'

and

/* var a = 'teste' */

?

I wonder because I came across a strange case, where I commented code with a problem inside a template string (which created a script tag with a code with a syntax error inside it) and as I commented the line with problem using the /* */ I did not receive syntax error, but when commenting the same line with //, gave me syntax error while running.

Follow the code where I had the problem:

$('#dadosBeneficiario').html(`
    <script type="application/javascript">
        function dropDown(node) {
            var drop = $(node).next();
            drop.slideToggle('slow');
        }

        function toggleObs(botao) {
            var campo = $(botao).prev();
            if (campo.val() !== '') {
                if($('#descricaoOpcional').val() !== '') {
                    /* $('#descricaoOpcional').val($('#descricaoOpcional').val() + "\n______________________________________________________\n" + campo.val()); */
                    $(botao).hide();
                } else {
                    $('#descricaoOpcional').val($('#descricaoOpcional').val() + campo.val());
                    $(botao).hide();
                }
            } else if ($(botao).val() === "Salvar") {
                return;
            }
            campo.toggle('fast');
            $(botao).val('Salvar');
        }
    </script>`);

When commenting on the line this way, the code ignored the line and ran. But when commenting with double bar, I received syntax error:

// $('#descricaoOpcional').val($('#descricaoOpcional').val() + "\n______________________________________________________\n" + campo.val());

Why did this occur?

1 answer

6


From the semantic point of view of the code, zero, after all comment does not change anything in it, it is only visual information for the programmer, and that should not be abused as many do.

There are two types because they can be come for different motivations. Some will say that only the second is really necessary because it can be used everywhere that the first can be used. The first is a facilitator, you indicate the beginning of the comment and the end will be the end of the line. But there is a tendency to prefer the use of the // whenever possible.

The use of /* ... */ has a comment start and end marker, so it can contain multiple lines or it can be in the middle of a line.

It’s used a lot for what we call comment out, that is, that code comment even if we take it temporarily to test something, to change something without losing what we had done before. The problem is when one leaves it beyond the test time, especially when it sends it to the repository and becomes an integral part of the code base. Code that is no longer used should always be removed.

Use in multiple lines is only appropriate when it will explain something complex, how it arrived at that formula or things like that, it is not to comment code or to put information of authorship, date, and information that are variable at each moment. There are controversies whether to put the code license.

The use in the middle of a line (for temporary use) should only be to explain that stretch, but almost always that it needs it should break into lines, and then the // may be fitting as well.

I have seen use when you will not pass an argument in function call, put what should be passed there, or at least indicate that you would have an argument there.

In the shown example could use any of them, than used right. As was not shown the use of // we have no way of knowing what you did wrong. Note that there is no error:

console.log("teste");
// $('#descricaoOpcional').val($('#descricaoOpcional').val() + "\n______________________________________________________\n" + campo.val());
console.log("fim");

I put in the Github for future reference.

However, later in the comments it was better explained that there is problem of rendering jQuery. The less using jquery the better, it is slow and brings problems. But as used had one of them. The comment ended complicated the parser within the string and he did not find the end of the comment in this case.

  • 1

    Maniero, I appreciate the information, but I still can’t figure out why the code gave me a syntax error while commenting on double bars. I made a mistake commenting? What happened?

  • 1

    As I wrote we have no way of knowing, you just posted right and not wrong.

  • I edited the question with the form I commented with double bar, sorry.

  • Without context, so it is obvious that there is no error, within a context could give, but only in it would give to see the error. You didn’t even post the mistake, it might not even be what you’re talking about.

  • I believe the error was to be inside the template string, when I try outside I get no error. The error was a normal syntax error stating that there was an unexpected character on a line

  • 1

    Now that I’ve seen the huge gambiarra there. It’s not even a code, it’s a text that will then be injected by jQuery. Well, almost all use of jQuery is wrong. Today it is almost unnecessary, and what people use is to type less, which in itself is a mistake. There’s an easy and simple way to do it and a crazy way, that’s the crazy way, that can give all kinds of problems. To tell you the truth is luck works even without comment.

  • I had to do this because this html div was ignoring external code for some reason that even the full programmers here at my company couldn’t explain. So I had to inject the JS into the div.

  • 3

    The solution to fix one gambiarra is never to create another.

  • The system where I work has so much gambiarra within gambiarra within another gambiarra since I joined the company that to tidy everything would be easier to develop again from scratch. Anyway, to finish this, in practice it is no difference to comment on a line of the two different ways as I asked at the beginning, at the time of reading, right?

  • Under normal conditions, no.

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.