To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
Code CritiqueHaving someone critique your code is always a great way to hone the skills. Stop in and post your code to see what your peers may have done differently.
This is one of my pet peeves. Proper commenting isn't about commenting every single line or small group of lines. If you have a function that's small and well defined, put a simple set of comments at the top describing what it does in simple terms, like so:
PHP Code:
function mt(){
// This function returns time in microseconds
$tmp = split(" ",microtime());
return $tmp[0]+$tmp[1];
}
That's all you need. Well written code should be self documenting.
If your function is large and does a lot, it's ok to put comments near the complex / inobvious parts. for instance:
PHP Code:
function replace_first_str($break,$replace,$str){
// This function replaces the first occurance of $break with $replace, leaving the others alone.
$tmp = split($break,$str);
$str = $tmp[0].$replace;
// We're done with the $tmp[0] so we toss it away
unset($tmp[0]);
// Note the concat operator, and use of implode to reinsert the original break text.
$str.=implode($break,$tmp);
return $str;
}
Large blocks of comments should be enclosed with /* */ like so:
/***************************
** Program: yabba dabba doo
** Version: 1.0
** Author: Sxooter
**
** This is the header for our program
** It has the program name, version numbers,
** Authors, and other important info.
** It's a good idea for the lines to not be overly
** wide, as it gets hard to read comments that
** run off the screen in some editors.
** This section should explain the overall
** functioning of the program without getting
** into too much detail.
****************************/
Note that there's plenty of wiggle room, and everyone has their own style. It's just important to not spend your time explaining things like:
// This is the $i for loop:
for ($i=0;$i<5;$i++){
__________________
PostgreSQL version 8.4 is now in beta! New features in the works: Updateable Views, WITH queries including recursive, On-disk bitmap indexes, and improved partitioning. woot!
I can code in PHP well enough (or so I think), but I never documented my code at all. Now I find myself working on old code and it's really hard to figure it out sometimes. Comments really are nice to have.
__________________
Live, learn, and die by the code!
The way I was taught to comment is that someone should be able to port you stuff to another language based on your comments alone.
The way I really comment is by commenting functions and varialbes but then letting the code document itself unless it get's tricky. List if I use a temp variable for differenet things in different places I'll throw in comments that say x is not blah.
I believe that your comments should say what your code does, not how it does it - unless a section of code is particularly obscure or you're releasing the script for newbie view. Comment the function or code block so that anybody who looks at it will know instantly what it does. They can work out how it does it by themselves.
I base this on the premis that if anybody is going to be looking at your code, you'll be fairly safe in assuming they'll have a decent knowledge of the language you're coding in.
__________________ Once you eliminate the impossible, whatever remains, however improbable, must be the truth. - Sir Arthur Conan Doyle
Originally posted by drawmack The way I really comment is by commenting functions and varialbes but then letting the code document itself unless it get's tricky.
I've always tried to use variables that are self descriptive enough to be self commenting. Since long var names are harder to type, I tend to use longer names on more seldom used / less obvious variables, while using shorter names for more commonly used vars.
I figure the more the var is used the more obvious its purpose usually is.
__________________
PostgreSQL version 8.4 is now in beta! New features in the works: Updateable Views, WITH queries including recursive, On-disk bitmap indexes, and improved partitioning. woot!
Originally posted by Jeb. I believe that your comments should say what your code does, not how it does it - unless a section of code is particularly obscure or you're releasing the script for newbie view. Comment the function or code block so that anybody who looks at it will know instantly what it does. They can work out how it does it by themselves.
I base this on the premis that if anybody is going to be looking at your code, you'll be fairly safe in assuming they'll have a decent knowledge of the language you're coding in.
__________________
PostgreSQL version 8.4 is now in beta! New features in the works: Updateable Views, WITH queries including recursive, On-disk bitmap indexes, and improved partitioning. woot!
Good question, actually. I don't think there would be a performance hit, as PHP doesn't actually "strip" the comments out first. The language scanner simply runs through the PHP file, doing nothing when it hits the comment blocks (except incrementing line numbers and what not).
I'm not too sure about this, I might go and have a closer look at the source...just for geekly kicks
__________________ Once you eliminate the impossible, whatever remains, however improbable, must be the truth. - Sir Arthur Conan Doyle
While I'm sure there's SOME performance hit relating to comments, I'd be willing to bet it's less than the difference of say a 750 versus 800 MHz processor or of a single extra line of executed code etc...
__________________
PostgreSQL version 8.4 is now in beta! New features in the works: Updateable Views, WITH queries including recursive, On-disk bitmap indexes, and improved partitioning. woot!
Originally posted by Sxooter While I'm sure there's SOME performance hit relating to comments, I'd be willing to be it's less than the difference of say a 750 versus 800 MHz processor or of a single extra line of executed code etc...
that's what i thought too, just being picky
__________________
there's no place i can be, since i found serenity.
the reason for this is that // is ignore the rest of the line so the compiler can very quickly skip that section but with /* */ the entire comment needs to be scanned to find the end cause it could be anywhere.
Also it is faster if you only use whole line comments so the preprocessor can pick them up on an earlier itteration.
HOwever I don't think it would be that much of a hit cause your code needs to be parsed at some point.
This forum has made me want to completely change some of my first scripts...completely change! Well they'll still do the same thing, but I'm going indent them, comment then, switch to switch statements (pardon the lame pun), and use censoring in my user input scripts. Also I'm going to obviously use htmlentities() on everything because it's my second favourite function (my favourite is explode).
I belive that proper commenting is essential, not the comment every line kind of commenting, but maybe a comment a the top of a user-defined function so that you know what it does...or stuff like that.
__________________
"A proof is a proof. What kind of a proof? It's a proof. A proof is a proof. And when you have a good proof, it's because it's proven." -- Jean Chrétien