Code comments and execution time

Indeed, I would love to as well. It’s not my claim, but apparently using PHP comments means you ‘FAIL HARD’ - see the first posts in this thread.

Ha, ironically, the article deathshadow linked to earlier on the IBM website even says this:

Tip 5: “Premature optimization is the root of all evil.” - Donald Knuth

I didn’t make up the above sentence. But you can find it on Wikipedia, so it must be really smart.

Unless you are trying to make people suffer, your first goal, when writing code, should be clarity. Simple code is faster to write, faster to understand when you return to it later, and faster to debug.

That article was a really good read, and has many jewels of wisdom that need to be proselytized on a more frequent basis.

Yup, I agree, it’s generally the way I try and code anyway. I also have the Clean Code book you mentioned earlier, it’s a good read.

Yes, because every job has unlimited time with unlimited budgets. :rolleyes:

Refactoring is great when its an option - but in the real world with deadlines adding features to less than well thought out code then documentation by comments is often your only choice.

Also, quite often, refactoring will cause added complexity, sacrificing clarity for flexibility and better separation of concerns. The added complexity doesn’t necessarily make the code easier to follow.

That seems to be going in the opposite direction. The whole reason for refactoring in the first place is to increase clarity so that the need for comments is reduced.

If If you’re finding that your code becomes complex and opaque, a step back clearly needs to be taken.

That is not always the case.

For Example: Introducing a factory to handle object creation instead of calling “new Object” directly. The logic has been abstracted away to improve flexibility, testability and dependency issues. However, it’s no longer obvious exactly what type of object has been created, sacrificing clarity.

That it slows it down at all means that POINTLESS comments should be avoided – that’s all I’m saying… DO comments imbue a penalty on the parser time? YES. Do comments make it take longer to load from disk? YES.

It is about balancing useful against the penalty; answer me this, do you consider this a meaningful comment:


// HTML
    <?php /* ===================== START BLOCK ======================== */ ?>
 <div id='new_left'>

Even if the comment itself were useful (opening a div is the start of a block, I’m shocked) all those equal signs are a waste of code… It doesn’t say what block is being started making it LESS useful than the DIV, the closing one doesn’t say what block is being ended, making you have to scroll back up to find it – net result is a POINTLESS comment that was a waste of time to type, waste of time to load from disk, waste of time for the parser to have to ignore when converting it to bytecode.

That’s pretty much my point. I wouldn’t have a problem with it if the comments were USEFUL. (which is why I’m not entirely happy with the title chosen for the thread split – it’s a MINOR part of what I was responding to)

Go futher down his code, and you have comments like this:


header("Content-type: text/css;charset:UTF-8"); http://www.barelyfitz.com/projects/csscolor/

What on earth does that URL have to do with sending content-type?!? Pointless meaningless comment.


if(0){include'style.class_colors.php';} http://www.typefolly.com/

Again, huh?!? Meaningless comment that makes no sense. Of course, this is the OTHER thing I took it to PM over; the condition is always false so this code would NEVER run; why is this line even there?

if(1){include’style.style_fonts.php’;}
if(1){include’style.style_dimensions.php’;}

Always true, why do these have if statements?

if(0){include’user_1.php’;}// blood,tangerine,banana,ocean,grass,coal
if(1){include’user_2.php’;}// blood,tangerine,banana,ocean,grass,coal

What on earth do those comments have to do with the user includes – are those usernames or something?

Comments should answer questions, not create more of them.

That it’s using php to output CSS (say goodbye to client-side caching) is just a further head scratcher since IMHO there’s no legitimate reason to EVER do that… god forbid the end user have to edit CSS… or write your changes to a static file instead of wasting processing time creating it on every request. That’s just as bad as presentational markup at that point.

I’m seeing the EXACT SAME ATTITUDE on this I encounter with HTML in terms of valid semantic markup; “Oh it’s so insignificant an impact” and “Oh it’s too much effort for the return”… too much effort to NOT write wasteful/pointless comments… Too much effort to NOT do something? It’s akin to the “It’s too much effort to follow a few simple rules to continue to make my pages useful on IE6 and even IE 5.5”

It does NOT take extra effort to go “hey, maybe I shouldn’t fill a line with 90% equal signs or asterisks”… it does not take extra effort if you bother following coding conventions that prevent you from making mistakes in the first place. It shouldn’t take extra effort to put meaningful names on things or to bother using the enter and tab key to make blocks of code logic clearer.

But you always have the people who claim it do, and just “vomit up code any old way”… Just look at the source code for turdpress for proof enough of that.

… and “simple code” doesn’t mean pointless code with vague/misleading/nonsensical comments. Premature optimization and bothering to follow a style guide making clear code are two separate things. Simple practices that reap MULTIPLE benefits (not just optimization) are NOT evil JUST becuase a slight code speed increase are a SMALL part of it. (Have we mentioned how the thread split title pisses me off and how you guys are focusing on a SMALL part of what I was originally saying?!?)

I should have just posted the PM in the original thread and to hell with dragging it off topic.

READ MY ORIGINAL POST

Now, PHP is a two stage bytecode interpreter; which is to say that it does a byte-code style compile before executing, but the inital parse still takes overhead for comments which is why stupid pointless comments (like a row of nothing but asterisks) should be avoided… Certainly a code caching accellerator (APM, eAccellerator) helps by caching the byte-coded version, but if your code ends up flushed from the cache those comments will incur their penalty again.

All I’m saying is don’t put stupid pointless comments in – is that SO HARD or TOO MUCH EFFORT?!? Sheesh.

Hate to break it to you ds, but computers have more than 1K of memory these days. There’s saving space, and then there’s useless pedantry

No, but there are overloaded servers from the “cheap” hosting companies that slap far too many sites on each server to keep their overhead low. So, simple optimization is never a bad idea.

I think DS’s point is simple - if the comment makes sense, then by all means, keep it. But if the comment is something that can be figured out with half a brain, then why keep a comment that doesn’t add value.

The start and stop of blocks he posted were a prime example - though to be honest, Ive used this if there was complex looping or if logic occurring until I had time to refactor it later…

Another would be commented out code - in the era of source control, why would you ever place code in production that’s commented out unless it was only going to be gone for a day or two? I’ve seen code that’s been commented out that is still in production code 4-5 years later - needless to say, when I’m done, that code is no longer there…

Comments cost nanoseconds. Badly written queries cost seconds. Billionth of a second versus seconds.

Yes, let’s all focus on pointless optimization so that we don’t have time for any real optimization.

That pretty much sums up all my thoughts about this thread :slight_smile:

If you are OCD enough to worry about php comments affecting compile time, there is a 100% chance you will be running an opcode cache anyway.

bingo

Most comments are more helpful than harmful if you work with a ~relatively~ competent group of developers. Even comments that just exist to “separate” concepts using a long dash of lines of other characters can very much aid in readability. Yes, it may be a sign of the need to refactor/reorganize but that isn’t always possible with time constraints and/or addition to a mature code base. As for opening and closing tags my opinion about that is if you find it difficult to read you probably don’t know the language as well as you think, in most cases. There is appropriate times to switch in and out of php in my opinion.

Except for some wierd reason you all think my statements are that is the ONLY reason not to make wasteful comments…

Good commenting and code formatting practices are not pointless optimization, and my original statement that started all this just mentioned it as ONE of the advantages that can – if you write it properly from the start – can be leveraged for no extra effort.

I feel like screaming “COMPREHENSION, TRY IT!!”

My code formatting works quite well. I’ve typed novels and done layouts for 300+ page technical manuals with over a hundred illustrations and tables. I know a little bit about formatting things.

But I don’t take my ability to format things as a reason to think I’m superior to anyone. You seem to, which is odd at the least.

Code legibility is important, but not the end all be all of the code. That would be whether or not the sucker runs. At the end of the day it doesn’t matter how pretty the source code looks if it doesn’t run.

And, for the record, I’ll take “wasteful” comments over no comments any day.

What is a useful or “good” comment for one person may not be useful to another. For example, while you stated the op FAILED REALLY HARD with his comments, I don’t personally have a problem with them. I do the same thing all the time to break up sections and make it faster to find a particular chunk of code.

Ok, I think I’m seeing where you guys are having the disconnect, as I wasn’t referring to JUST his comments with the fail hard part – as I explained in the PM rather than drag the original topic pre-split further OT.

I just snipped out the copy of his code I was responding to. The comments were just PART of the problem.

If you bother to READ the rest of the post you just linked to, you’ll see it’s not just commenting on the comments, but the constant opening/closing of php which I always consider sloppy (probably because it’s PHP’s the only language I’ve used in three decades to even ALLOW that type of coding) … and then there’s all the stuff I pointed out about the code after.

The entire code has a bunch of small issues, that IMHO add up to /FAIL/. From the comments that are confusing/don’t add anything useful, to the difficult to follow logic flow caused by all the <?php ?>, to the always true/false if statements, to the use of PHP to output static CSS.

BUT YOU GUYS KEEP HARPING LIKE the small easy to do just code it that way from the start difference of using meaningful comments and stripping out useless ones is the be-all end-all of the topic; OR that it’s difficult to bother doing in the first place.

WHISKEY TANGO FOXTROT.

Not doing something that is easy, can be done from the start… just because… ok, why exactly? You can’t be bothered? LAME.

You would be taken more seriously if you communicated in a more mature way rather than using some sort of emotional venting - especially that you don’t appear to be very knowledgable about the technical things you are writing about.

I agree with those who say that comment execution time is nothing to worry about. To me it’s a waste of time to even think about it. Good concise comments are best. Too many comments are better than no comments. Lines of asterisk comments are fine for some occasional separation - they don’t hurt most of the time. Thinking about <?php ?> tags slowing down execution is useless to say the least.

Thinking should be directed to real optimization tactics instead, like db queries, loop operations and other bottlenecks. If I can achieve a micro-optimization very easily I will go for it - for example I will use (int) most of the time instead of intval(), but thinking about shortening comments is absurd. They need to provide readability and clarity to me and other humans, nothing else matters here.

Comments often show very individual style of the programmer and preferences may vary a lot. Saying that someone ‘FAILED HARD’ because of a comment style may indicate simply inability to accept that someone else has a right to their own style and a way of thinking.

JUMPED UP… CAN YOU PEOPLE NOT…

Gah, whatever. I heard quality of literacy was at an alltime low. Screw it, whatever. Nobody takes any pride in their work any more. Just go about sleazing out your code any old way and don’t even bother reading or understanding anything you’re responding to…