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

3

As far as I understood, using // to comment on a line and /*...*/ was the same, with the difference that the second spanned more than one line if necessary.

There is SOME difference between doing

 // var a = 'teste'

and

/* var a = 'teste' */

?

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

Here is 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>');

Commenting the line in this way, the code ignored the line and ran. But when commenting with double slash, I got syntax error:

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

Why did this happen?

    
asked by anonymous 28.11.2018 / 15:31

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 both types because they can be come for different motivations. Some will say that only the second really is needed because it can be used everywhere the former 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 to use // more whenever possible.

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

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

The use in several lines is only appropriate when it will explain something complex, how it came in that formula or things of the type, it is not to comment code or to put information of authorship, date, and information that are variable at all times . There is controversy over whether to license the code.

Use in the middle of a line (for very temporary use) should only be to explain that stretch, but almost always you need it should break in rows, and then // may be fitting too. >

I've seen usage when not going to pass an argument in function call, put what should be passed there, or at least indicate that it would have an argument there.

In the example shown you could use any of them, provided they are used right. As we did not see the use of // we can not know what he did wrong. See that it does not give an error:

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

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

    
28.11.2018 / 15:40