0

I like leaving comments in my code so that other people can understand my code easier. However, I dislike having HTML comments, as I don't like <!-- comment --> in my code.

I know that they're comments and they're not read by the page, but I dislike having them in the source and prefer only the code in the page. (I think it's a form of weird OCD, rather than actual preference, it just doesn't feel right).

So I've been doing this <?//This is my comment?>. I know that this is a silly way of doing it, but then it only appears in my code and not the source. (I only do this on PHP pages, if there is no need for PHP, I suck it up and use <!-- comment -->.

But the question is: How 'clean' is using <?//comment?> to insert a comment in code?

What are the disadvantages of using comments like this and is there anything technically wrong with using them like this?

I know this question can be perceived as 'opinion based', but I want to know technical disadvantages rather than personal. I also know about long hand PHP tags, but my server allows them. Shall I use long hand anyway?

11
  • Only potential issue I can think of is the shorthand php tags as they are not 100% supported by servers. Commented Jan 13, 2014 at 15:03
  • Although I think this question is primarily opinion-based, I would definitely not use short open tags for that. Commented Jan 13, 2014 at 15:03
  • @Novocaine88 The servers that I use do support this, however is it still best to use the long hand version? Commented Jan 13, 2014 at 15:03
  • 1
    Regarding speed: don't optimize prematurely. It will be slower than HTML comments, as the code within php tags have to be interpreted some way or another, but you won't notice this. Commented Jan 13, 2014 at 15:08
  • 2
    If you're going to use embedded PHP comments, I'd prefer to use <?php /* .... */ ?> rather than a // comment, because the // makes it ambiguous about whether the comment should end at the ?>. Commented Jan 13, 2014 at 15:45

1 Answer 1

2

There are no technical disadvantages to this approach per se, the only "issue" I see with it is that the parser has to parse contents of <?php and ?> as PHP hence, needs to extract the data and pass it to the PHP interpreter only for it to discover that there's nothing actually to render.

In my view it's foolish to worry about performance issues of doing something like that nowadays but I guess if you're as big as Google you would even be ensuring your HTML class names are as short as possible to make the source smaller.

Regarding cleanliness - it's as clean as you or anyone else reading the code perceives it. If neither you nor your colleagues have anything against them then keep using them. If you have frontend developers complaining about stuff like that then I would suggest you look at something like Twig.

Twig allows you to have inline comments like PHP as well as native HTML comments:

{# this comment will not be rendered to the user #}
{{ print something }}

<!-- this comment will render as usual -->

Also, you can actually have the same issue with CSS where some people might not like leaving comments for others to see.

If that's the case you can look into LESS, which allows you to do the same thing:

/* this comment will be visible to end users */    
.custom-class {}

// this comment won't...
.other-custom-class {}

That's not an issue though if you minimise your stylesheets which you should do anyway. Which you could do with your HTML too before outputting! :)

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much for the reply and also recommending Twig and LESS. I'll accept this as it has given me more or less what I wanted to know, thanks!
Glad to help! On a side note, if you decide to use Less, this tool is very nice: wearekiss.com/simpless. It monitors the file you tell it to and automatically compiles it so you don't have to run any commands prior to refreshing the page you're on. If you have multiple less files, have one called includes.less and add @import file1, @import file2, etc. and point the tool at the includes.less one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.