Jump to content


Coding Style


18 replies to this topic

#1 Kuulest

    Advanced Member

  • Members
  • PipPipPip
  • 326 posts

Posted 18 May 2002 - 10:35 AM

Hello all!
I strongly support the idea that 'good' code is 'well-formated' code as well.
What I want to stress here is that general appereance of the code is not less
important then it's content. So I decided that my first post in this section would
be about code conventions and standards I use during my sleepless nights of fighting
with PHP.
Few potints I want to make before going deep inside the code:
1. I always separate OO and Procedural code.
2. I don't use shell-like comments ('#')
3. I always use full start-end tags i.e. <?PHP ?>
4. Edit plus has cool option of substituding tabs by spaces and
I always use it.

General:

So here is my template for PHP file (I use Edit+ where it's possible to set default
appereance of your template files):

<?PHP
/*
*   +------------------------------------------------------------------------------+
*       Project Name                                                               
*   +------------------------------------------------------------------------------+
*       Copyright Notice(s)                                                        
*   +------------------------------------------------------------------------------+
*       Disclaimer Notice(s)                                                         
*       ex: This code is freely given to you and given "AS IS", SO if it damages     
*       your computer, formats your HDs, or burns your house I am not the one to 
*       blame.                                                                    
*       Moreover, don't forget to include my copyright notices and name.              
*   +------------------------------------------------------------------------------+
*       Author(s): Victor Farazdagi (Kuulest)                                      
*   +------------------------------------------------------------------------------+
*/
 error_reporting(E_ALL);//For debugging purposes

    /*code goes here*/

/*
*tab-width=4
*indent=4
*width=90
*/
?>

Operators:

I have several basic rules on operator formating:
1. I always add spaces before and after assigning, logical
and comparision operators:
                //assignment operators:
                    $a = 5;
                    $my_str .= "bla bla bla";
                    $i += 2;
                //logical and comparision operators:
                    if (($name == 'Viktor') or ($name == 'Andrei') ) {
                        echo 'Record found!';
                    }
            
2. I also try to use (but almost always fail) the same logic for
for arithmetic operators:
                //Looks better
                $a = (10 * $b) / 20;
                //then this:
                $a = (10*$b)/20;
           
3. Sometime I find the following structure usefull (just to increase readability):
$var_name_1  = 5;
$b           = 7;

Control Structures:

Again rules I use:
1. I let one space after control structure and its brakets i.e if ()
2. I always use {} for indicating control structure body regardless of number of
statments control structure contains.
3. I intendate by one tab (actually four spaces) code statements inside the
control structure.
4. Not merely a rule but I often put additional line between different
(not nested) control structures.
5. I never use alternative syntax for control structures
(ex: if (cond):statements endif;)

if; if..else; if..elseif;
            if ($a == $b) {
                /*code goes here*/
            }
            
            if ($a == $b) {
                /*code goes here*/
            }else {
                /*code goes here*/
            }

            if ($a == $b) {
                /*code goes here*/
            }elseif ($a == $c) {
                /*code goes here*/
            } 
        

for; foreach; while;
I let additional space after ';' in 'for' cond. structure.
            for ($i; $i < 10; $i++ ) {
                /*code goes here*/
            }
            foreach ($arr as $k => $v ) {
                /*code goes here*/
            }
            while ($i < 10) {
                /*code goes here*/
            }
        

I specially omitted do..while structure, simply because I don't use it much.

switch;
            switch ($age) {
                case 19:
                    echo 'You are in the army now!';
                    break;
                case 100:
                    echo 'Incredible!!';
                    break;
                default:
                    echo 'Nothing to say!';
            }
        


Functions and Classes
Although there is nothing special about functions and classes with respect to
formating since they merely utilize the techniques discussed above, I want
to make several remarks:
1. It's a golden rule for me to documentate functions as well as possible.
2. Same thing about classes but even in more rigorous terms.
3. Look at {} in classes and functions below. I place them different from
same in control structures.

    /**  Function: void login($user_name = 'nobody', $user_pass = 'nobody')
    *    ----------------------------------------------------------------
    *    Purpose:           Member function handling user login process
    *    Arguments:         $user_name - string, user name
    *                       $user_pass - string, user password
    *    Returns/Assigns:   Sets session variable $logged to 1 if function 
    *                       call succeeds and 0 if otherwise.
    */
    function login($user_name = 'nobody', $user_pass = 'nobody')
    {
        /*code goes here*/
    } 
    


Classes:
    /**
    *   HTML Form elemetns generator for PHP4.
    * Explanation: class purpose is to generate, strore and transfere btw pages
    *     form elements and their respective values.
    * Supported HTML Form elements:
    *                 text; password; radio; checkbox; image; file; hidden; button;
    *                   submit; reset;
    */
    
    
    class Form
    {
        /*code goes here*/
    } 
    

Not that for classes I also prepare distict documentation file covering topics like
following:

/**
* Classname: Form
* Description:Class for generationg HTML form elements on fly.
* Supports all form elements:
* 'text', 'password', 'checkbox', 'radio', 'textarea',
* 'button', 'submit', 'reset', 'select', 'hidden',
* 'image','file'.
* Version: 0.5
* Author: Victor Farazdagi(Kuulest).
*/

/**
*Instance variables
*/
classname - 'Form'
debug - switches on/off debugging mode(not really implemented)

/**
*Instance methods
*/
All user-methods present in class.

/**
*Internal methods
*/
All system-methods (internal) present in class.

/**
*TODO List:
*/


That is all!

Now, I believe that it would be cool experience to know about your coding style.
Yours Kuulest
P.S. Sorry if text is too long, I worked hard to cut as many as posible..

#2 ck

    Advanced Member

  • Members
  • PipPipPip
  • 556 posts

Posted 18 May 2002 - 11:44 AM

Good start!...

That's why this topic is sticky after that... :king:

#3 Kuulest

    Advanced Member

  • Members
  • PipPipPip
  • 326 posts

Posted 18 May 2002 - 12:44 PM

Thank you ;D!
But what does 'sticky topic' mean??

I hope it's not too silly question.

#4 ck

    Advanced Member

  • Members
  • PipPipPip
  • 556 posts

Posted 18 May 2002 - 12:54 PM

Sticky topic means, topics stays always on top in that category. See "A Unix Tutorial" in articles section.

#5 mikeq

    Advanced Member

  • Members
  • PipPipPip
  • 43 posts

Posted 20 May 2002 - 09:26 AM

Hi Kuulest,

Good article, pretty much how I code also. ;D

Only one thing I add to function parts are a revision history

/**  Function: void login($user_name = 'nobody', $user_pass = 'nobody')
    *    ----------------------------------------------------------------
    *    Purpose:          Member function handling user login process
    *    Arguments:        $user_name - string, user name
    *                      $user_pass - string, user password
    *    Returns/Assigns:  Sets session variable $logged to 1 if function 
    *                      call succeeds and 0 if otherwise.
    *
    *Name      Date                 Version          Change
    *Mikeq      20 May 2002    1.1                 Description of change
    *
    */
    function login($user_name = 'nobody', $user_pass = 'nobody')
    {
        /*code goes here*/
    } 


#6 Guest_bmhm_*

  • Guests

Posted 20 May 2002 - 04:13 PM

this is how I code (unless I get real lazy and want to get the job done real quick).

I liked the revised history posted by mikeq

#7 jstanden

    Member

  • Members
  • PipPip
  • 24 posts

Posted 09 September 2002 - 06:12 AM

We pull a lot of our revision history right out of CVS.

Good start on this thread!

#8 ck

    Advanced Member

  • Members
  • PipPipPip
  • 556 posts

Posted 10 September 2002 - 11:45 PM

We will display such well-explained scripts in seperate place in near future. Just a note. :bday:

#9 Kuulest

    Advanced Member

  • Members
  • PipPipPip
  • 326 posts

Posted 07 October 2002 - 08:59 AM

Updated version may be found at:
http://www.phpfreaks...orials/35/0.php

#10 Gustavo el Geranie

    Advanced Member

  • Members
  • PipPipPip
  • 716 posts

Posted 14 July 2003 - 04:11 PM

I feel also that well formatted source code is very important. Sadly I start most of the time with a quick and dirty solution. This tool helps a lot:
http://www.phpedit.n...CodeBeautifier/

#11 cgapperi

    Newbie

  • Members
  • Pip
  • 9 posts

Posted 12 February 2005 - 12:44 AM

I appreciate your conventions. I am adding someone to my staff in the next week and this has been a concern. Your document provides a good start to standardization. Will use it. I also add an example in my function comments. Makes for less ?'s later by other users (and myself when I come back to it after 6 months...)

C

#12 Gustavo el Geranie

    Advanced Member

  • Members
  • PipPipPip
  • 716 posts

Posted 12 February 2005 - 02:28 PM

There is program I use to create good documentation, phpDocumentor: http://www.phpdoc.org/

This creates easy to use docs for your code. For example it took me less than 1 hour to do the docs for this class: http://have2.com/sms...atell_SMS.htmll

#13 kumar

    Newbie

  • Members
  • Pip
  • 1 posts

Posted 20 April 2005 - 07:01 AM

The coding practices discussed above are for PHP. What about HTML ? What are the coding styles for HTML code contained in the  PHP file ?

#14 Gustavo el Geranie

    Advanced Member

  • Members
  • PipPipPip
  • 716 posts

Posted 20 April 2005 - 07:39 AM

I think for any major project you should use templates and not mix php with html.

#15 kumar

    Newbie

  • Members
  • Pip
  • 1 posts

Posted 20 April 2005 - 07:54 AM

For the present application, we are not using Templates. Anyway, what are the coding styles or practices for HTML ?

#16 Guest_lampraja_*

  • Guests

Posted 24 March 2006 - 07:35 AM

Hi!
Its really gre8 from ur part...
u focused at ur best 2 guide all of us new guys interacting with new technologies..
Thx frm all the side of new bies..

Regards,
Raj

#17 Guest_lamp_rams_*

  • Guests

Posted 01 February 2008 - 03:58 AM

Hi All,
Its very nice coding standards thanks admin

#18 sweet

    Newbie

  • Members
  • Pip
  • 0 posts

Posted 18 March 2008 - 06:51 PM

Hey thanx a lot .Its tooo good job..........
I really like this ...helping documentation .........keep it upp
It will very  helpful to  us!!!!!!!!!!!!!!
thank u very much

#19 Kuulest

    Advanced Member

  • Members
  • PipPipPip
  • 326 posts

Posted 03 June 2008 - 05:13 AM

Quote

Hey thanx a lot .Its tooo good job..........
I really like this ...helping documentation .........keep it upp
It will very  helpful to  us!!!!!!!!!!!!!!
thank u very much

Well, actually nowadays I am using Zend Studio (it was really nice times with EditPlus) + Zend Framework for all my developer needs. That effectively renders this article out of date - Zend Studio allows you auto completion only if you provide commentary to functions, classes, interfaces in phpdoc format. Plus as any ZF user, for any particular project further extension of framework is required, thus it's a good idea to follow the ZF guidelines on code formating.

Overall, I really like the Studio and (now gaining popularity) Zend Framework - after all if you are writing in PHP5 (and you should, because PHP4 is out of support August this year), it's one of the best cocktails you might get.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users