Programming Basics
  Home arrow Programming Basics arrow Page 4 - Coding
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Download TestComplete 
Forums Sitemap 
Weekly Newsletter 
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PROGRAMMING BASICS

Coding
By: lig
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 8
    2006-01-17

    Table of Contents:
  • Coding
  • Thinking before Coding
  • Commenting
  • Coding Style
  • Error Reporting
  • Error Handling
  • Security
  • Reinventing the Wheel
  • Advanced
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Coding - Coding Style


    (Page 4 of 10 )

    Whether for good or for bad, the days of spaghetti code (a slang expression used to describe any badly designed or poorly structured program that is hard to unravel or understand as a bowl of spaghetti) are gone, so how you code is just as important as what you code. Now I don't want to start a flame war over which set of standards are better then others--for example tabs or spaces. It is irrelevant what particular style or system you use when writing your code as long as you consistently follow it throughout the entire script. An example of one is PEAR's coding standard..

    Indention

    One area in particular I want to be sure to touch on is indention. Indention is used to visually group logic blocks for easier reading and understanding and also helps for matching parenthesis. Use 2, 3, or 4 spaces, tabs or whatever you prefer to indent--just stick with it.

    <?php
    if (isset($_SERVER["REMOTE_ADDR"])) {
      
    $octet substr($_SERVER["REMOTE_ADDR"], 0strpos($_SERVER["REMOTE_ADDR"], "."));
      switch (
    $octet) {
        case 
    127:
          
    $network_class "Reserved Loopback";
          break;
        case (
    $octet &lt;= 126):
          
    $network_class "Class A";
          break;
        case (
    $octet &lt;= 191):
          
    $network_class "Class B";
          break;
        case (
    $octet &lt;= 223):
          
    $network_class "Class C";
          break;
        case (
    $octet &lt;= 239):
          
    $network_class "Class D";
          break;
        case (
    $octet &lt;= 255):
          
    $network_class "Class E";
          break;
        default:
          
    $network_class "Unknown Class";
          break;
      }
    } else {
      
    $network_class "Unknown Class";
    }
    echo 
    "Your IP address is: $network_class";
    ?>

    Variable Naming

    In PHP variables are required to start with a letter or underscore, followed by any number of letters, numbers or underscores. It should also be noted that PHP variables are case-sensitive, so $var and $Var are 2 separate variables. Also, make sure you also use variable names that provide information for what the variable will be used for or where it came from. There is no real limit on how long a variable name can be; the only limits are how much typing you want to do.

    <?php
    $x        
    // no idea what it is for.  
    $num      // better â€“ at least you now know it is a number, 
              // but a number for what?  
    $cc_num   // better yet â€“ at least now we know it is a credit 
              // card number being held in the variable 
              // (or is it a cheddar cheese stock number?)  
    $cred_card_num        //best variable name
    $chedChezStockNum  //best variable name
    ?>

    More Programming Basics Articles
    More By lig


       · The link to Chris Shiflett's blog is broken.The blog can be found at...
       · Be careful when you log stuff to rotate the logfile, otherwise it'll fill up and...
       · Sorry about that.
       · Thanks very much for taking the time to write this. Although most was over my head,...
       · That was pretty convincing Gospel. I hope this stuff is gonna work for...
     

    PROGRAMMING BASICS ARTICLES

    - PHP: Hypertext Preprocessor: What is it?
    - Loops and PHP Decision Making
    - Operators, Conditionals, and PHP Decision-Ma...
    - PHP Decision-Making
    - Coding
    - Server Statistics
    - Looping in PHP
    - Cookies in PHP
    - Working with text files
    - Beginning Object Oriented Programming in PHP
    - A Tour of Decision Making Structures in PHP
    - PHP Strings Primer
    - PHP Control Structures
    - Intro to Vim
    - Reading Directorys with PHP





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek