TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 05-22-2008, 06:24 PM   #1 (permalink)
The Wanderer
 
Join Date: Dec 2007
Posts: 18
Thanks: 2
xperience is on a distinguished road
Default Commenting code

How do you comment your code ? If it's one thing I really suck at is understanding how to correctly comment my code. I've read some things over the internet, but with still no luck.

How do you comment variables

PHP Code:
    /*
    * 
    * @var array
    *
        */
    
public $getinfo;
    
    
/*
    *
    * @var array 
    *
        */
    
private $connection
Is something like that feasible? What else would you include ? What would you change? How about a class, a function, etc ?
xperience is offline  
Reply With Quote
Old 05-22-2008, 06:27 PM   #2 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,298
Thanks: 17
Village Idiot is on a distinguished road
Default

There is no correct way to comment your code. Your comments should state what the code below it does in plain English. Don't make a novel or anything, just tell the next programmer what is going on. When making a variable, just comment on what its purpose is.
__________________

Village Idiot is offline  
Reply With Quote
Old 05-22-2008, 11:12 PM   #3 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

I'm crazy when it comes to commenting, and not because I'm worried about other programmers, since other programmers generally don't have their hands on my work at this point - it's for my own purposes that I comment, because no matter how into a particular method I am at the time, where I can see everything visually working together in my head, and it flows like this mornings coffee, two weeks later I can come back to something and sit there cross eyed for half an hour trying to figure out what I did if I didn't comment it.

There's three types of comment constructors (delimiters? hmm) you can use in PHP, just to rehash, there's /* -- */, // and #. Typically I use the /* version for the large descriptive comments I use at the beginning of all my library files, and for method comments (where I list the function name, the arguments it takes and what it should return, along with an area for notes as I'm in the development process) and then I'll use // for comments that are littered about elsewhere.

This is just one way of going about it. The actual correct way to do it... is... wait for it... however you want. What looks best? What's most readable? If it winds up adding more clutter and making your readability go down, then chances are you should try another way.
-m
delayedinsanity is offline  
Reply With Quote
Old 05-23-2008, 12:45 AM   #4 (permalink)
The Frequenter
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
xenon is on a distinguished road
Default

I'm going the 'standard way', if PHPDoc has become a documenting standard. Why? Well, because I use a fully compatible IDE that recognizes the PHPDoc syntax and helps me in development (mainly), because you can generate a thorough HTML documentation without having to actually go through the code manually, and that's about it for me.
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
xenon is offline  
Reply With Quote
Old 05-23-2008, 08:17 AM   #5 (permalink)
Jim
The Addict
 
Jim's Avatar
 
Join Date: Nov 2007
Location: the Netherlands
Posts: 281
Thanks: 2
Jim is on a distinguished road
Default

I'm fairly simple with comment, I use it allot but just to make it quicker to understand for myself if i havent touched the code in months.

This is how I commented my blog frontpage:

PHP Code:
<?php

////////////////////////////////////////////////////////////////////
//
// Jimnet blogging system
// By Jimmy Bokhove, Minna Media <xxx>
//
// Create a blog by template
//
////////////////////////////////////////////////////////////////////

// Get Configs
require_once('config/sql.conf.php');
require_once(
'config/default.conf.php');

// Get functions
require_once('geshi.php');
require_once(
'function/default.func.php');
require_once(
'function/bbcode.func.php');

// Get classes
require_once('class/template.class.php');
require_once(
'class/blog.class.php');

// Loop blogs
$blogContent loop_posts(configNoPostFP());

// Create new template
$template = new template('main.tpl');

// Set template contents
$template->set('i_title''Jim\'s blog');
$template->set('i_description''Jim Bokhove\'s weblog');
$template->set('i_keywords''jim, jimmy, bokhove, weblog, blog');
$template->set('i_language''english');

// Main content (blogs)
$template->set('i_content'$blogContent);

echo 
$template->parse();

?>
__________________
Nunchaku! Who doesn't like martial arts? =)
Send a message via MSN to Jim Send a message via Skype™ to Jim
Jim is offline  
Reply With Quote
Old 05-23-2008, 10:44 AM   #6 (permalink)
The Frequenter
Zend Certified 
 
Join Date: Sep 2007
Location: Denmark
Posts: 352
Thanks: 8
Kalle is on a distinguished road
Default

I use PHPDoc blocks and all my comments are the multiline comment and always having an extra * in the beginning like:

PHP Code:
/** This is an example */ 
__________________
Send a message via MSN to Kalle Send a message via Skype™ to Kalle
Kalle is offline  
Reply With Quote
Old 05-23-2008, 09:09 PM   #7 (permalink)
The Wanderer
 
Highway of Life's Avatar
 
Join Date: May 2008
Location: Beware of programmers carrying screwdrivers
Posts: 21
Thanks: 0
Highway of Life is on a distinguished road
Default

I also use the PHPDoc blocks method of commenting code. Using this for functions and class variables primarily.
Normal comments on the rest of the code as people will always need to read the code, and commenting helps in situations which could be confusing to a new programmer.

If you use an IDE such as Eclipse, ZDE or PHPEd, these comment blocks can come in handy when you are working with a project. -- You can even use a plugin in most PHP IDE's to generate code documentation based on your PHPDoc blocks.
__________________
- Highway of Life
[ Software Engineer | PHP Developer | phpBB.com Team Member ]
phpBB Academy at StarTrekGuide
Send a message via AIM to Highway of Life Send a message via MSN to Highway of Life
Highway of Life is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 09:17 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design