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
PHPBuilder.com  
 

 

Go Back   PHPBuilder.com > PHP Help > Code Critique

Code Critique Having 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.

Reply
 
Thread Tools Rate Thread Display Modes
Old 09-30-2003, 05:30 PM   #1
Sxooter
Chamberlain
 
Sxooter's Avatar
 
Join Date: Aug 2002
Location: Denver, CO
Posts: 4,055
Proper commenting

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!
Sxooter is offline   Reply With Quote
Old 09-30-2003, 08:09 PM   #2
Jedi Legend
Member
 
Join Date: Aug 2002
Posts: 73
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!
Jedi Legend is offline   Reply With Quote
Old 09-30-2003, 11:11 PM   #3
dotwebbie
Expert at something . . .
 
Join Date: Aug 2002
Posts: 287
When I saw that subject, I thought you were going to complain about to properly using block comments, line comments, and doc comments. heh.

But yes, I agree. Another habit I myself have is comment coding. That is stuff like:

PHP Code:
for($i = 0; $i < 20; $i++)
{
      echo(
"Hi");
}
//bye
or

PHP Code:
#hello world!
//Loop it, loop it good!
for($i = 0; $i < 20; $i++)
{
      echo(
"Hi");
}
//bye
dotwebbie is offline   Reply With Quote
Old 10-01-2003, 01:57 AM   #4
Moonglobe
Better fan than rebelo!
 
Moonglobe's Avatar
 
Join Date: Apr 2003
Location: brain://localhost:left-side
Posts: 2,381
Re: Proper commenting

Quote:
Originally posted by Sxooter
This is one of my pet peeves.
ya? well one of MY pet peeves is people who comment their code!
__________________
there's no place i can be, since i found serenity.
Moonglobe is offline   Reply With Quote
Old 10-01-2003, 03:20 AM   #5
drawmack
Computers can do that?
 
drawmack's Avatar
 
Join Date: Apr 2003
Location: Pocono Mtns PA
Posts: 3,268
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.
drawmack is offline   Reply With Quote
Old 10-01-2003, 05:54 AM   #6
Jeb.
Titles are overrated.
 
Jeb.'s Avatar
 
Join Date: Jul 2003
Posts: 150
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
Jeb. is offline   Reply With Quote
Old 10-01-2003, 06:39 AM   #7
laserlight
PHP Witch
 
laserlight's Avatar
 
Join Date: Apr 2003
Location: Singapore
Posts: 12,388
Well, sometimes it is good to outline the algorithm used, it may help in maintenance.
__________________
Use Bazaar for your version control system
Read the PHP Spellbook
Learn How To Ask Questions The Smart Way
laserlight is offline   Reply With Quote
Old 10-01-2003, 10:11 AM   #8
Sxooter
Chamberlain
 
Sxooter's Avatar
 
Join Date: Aug 2002
Location: Denver, CO
Posts: 4,055
Quote:
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!
Sxooter is offline   Reply With Quote
Old 10-01-2003, 10:38 AM   #9
Sxooter
Chamberlain
 
Sxooter's Avatar
 
Join Date: Aug 2002
Location: Denver, CO
Posts: 4,055
Quote:
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.
$agree_with_jeb++; // <- self documenting variable
__________________
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!
Sxooter is offline   Reply With Quote
Old 10-01-2003, 10:57 AM   #10
Moonglobe
Better fan than rebelo!
 
Moonglobe's Avatar
 
Join Date: Apr 2003
Location: brain://localhost:left-side
Posts: 2,381
Is it just me, or wouldn't PHP's interpretted nature mean that the parser's stripping out comments would hit performance a bit?
__________________
there's no place i can be, since i found serenity.
Moonglobe is offline   Reply With Quote
Old 10-01-2003, 11:18 AM   #11
Jeb.
Titles are overrated.
 
Jeb.'s Avatar
 
Join Date: Jul 2003
Posts: 150
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
Jeb. is offline   Reply With Quote
Old 10-01-2003, 11:26 AM   #12
Sxooter
Chamberlain
 
Sxooter's Avatar
 
Join Date: Aug 2002
Location: Denver, CO
Posts: 4,055
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!

Last edited by Sxooter; 10-01-2003 at 11:40 AM.
Sxooter is offline   Reply With Quote
Old 10-01-2003, 11:28 AM   #13
Moonglobe
Better fan than rebelo!
 
Moonglobe's Avatar
 
Join Date: Apr 2003
Location: brain://localhost:left-side
Posts: 2,381
Quote:
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.
Moonglobe is offline   Reply With Quote
Old 10-01-2003, 11:40 AM   #14
drawmack
Computers can do that?
 
drawmack's Avatar
 
Join Date: Apr 2003
Location: Pocono Mtns PA
Posts: 3,268
// is faster then /* */

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.
drawmack is offline   Reply With Quote
Old 10-01-2003, 10:05 PM   #15
Merve
black sheep with red wool
 
Merve's Avatar
 
Join Date: Jul 2003
Location: North of the 49th parallel
Posts: 2,579
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

The Three C's
Merve is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT -4. The time now is 10:41 PM.






Acceptable Use Policy

internet.comMediabistrojusttechjobs.comGraphics.com

WebMediaBrands Corporate Info


Advertise | Newsletters | Feedback | Submit News

Legal Notices | Licensing | Permissions | Privacy Policy


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.