WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
PHP Web Logs (BLogs)
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Forex Trading Online forex trading platform

Go Back Add a Comment Send this Article to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES SUBMIT AN ARTICLE PRINT
Title : Coding Criteria: Sloppy VS. Clean
Categories : PHP, Beginner Guides
Joe Crawford Jr.
Joe Crawford Jr.
Date : 2004-05-29
Grade : 0 of 5 (graded 0 times)
Viewed : 8399
Search : More Articles by Joe Crawford Jr.
Action : Grade This Article
Tools : My Favotite Articles


  Submit your own code examples 
 


What's the big deal, people are always saying that i need to code clean what the heck are they talking about?

In PHP there is a big difference between clean and sloppy code, the difference is shown in the following examples. Remember PHP doesnt mind whitespace so when you use it, it get's ignored and makes for clean readable code.

Sloppy Code


<?if($x<0){echo "0";}?>


Clean Code


<?
if( $x < 0 )
{
echo
"0";
}
?>




Looking at the two examples above, which do you find learer and easier to read? Everyone listening should *hope* you said the clean version. Yet the sloppy version only used one line it takes more time to read, you cannot just glance at it and know what is going on there. Dont get me wrong this is not the only way to write sloppy code there are a lot of ways to code badly.

What's the big deal with sloppy code?

If you are working alone on a simple project and you dont mind writing/reading sloppy code so be it, however when you are working with a team of developers you should ALWAYS comment your code and write clean code so that everyone can understand what is to be done in a certain section of code.

Below there is another example of sloppy coding, can you see the difference?

Sloppy Code


<?
$arr
= array("cats", "dogs", "fish");
for(
$i=0;$i<=count($arr);$i++)
{
echo
$i;
}
?>


Clean Code


<?
$arr
= array( "cats", "dogs", "fish" );
$cnt = count( $arr );
for(
$i=0; $i <= $cnt; $i++ )
{
echo
$i;
}
?>


The difference above has to do with 2 things, firstly the spacing in the function calls and in the for loop. The second difference is that in the first for loop i had


<?
for( $i = 0; $i <= count($arr); $i++ )
?>


Using the count() function within a for loop is a very bad thing to do, each time the for loop executes, it has to take the time out to count the elements in a "static" array (meaning we are not adding elements to $arr, it never changes) and this eats up processing time.

The second way we did it is much better since the count() function get's called only one time.


When using the define() function to define constants you should always use UPPERCASE for the entire constant, this makes it easy to pin point that it is not a normal variable.


<?
define
("SITE_URL", "http://www.domain.com/");
?>


Function calls are something people almost always make the mistake with. When naming functions you should always make the names very readable rather than use printname() you should use printName or print_name. When you have a function name that combines words either uppercase the first word of every word except the starting word or seperate the words with an _ (underscore). This will lead to much more readable code. Which is more clear to you?

Sloppy Code


<?
function dothisanddothat() {

}
?>


Clean Code


<?
function doThisAndDoThat() {

}

function
do_this_and_do_that() {

}
?>


The way you choose to do this is your decision and is based on preference. I would HIGHLY suggest that you use a clean way though ;)

Below you will find a few guidelines to get you coding cleanly

1.) When using control structures (if, while, do, for) always space them accordingly and put { and } on thier own lines, this adds for clarity.

2.) Try not to make function calls within for, while, if loop defenitions unless absolutely necessary.

3.) Comment your code well, but dont over do it.

4.) When naming constants always use UPPERCASE_NAMES for them.

The best way to learn clean coding is to read online articles like this one and to practice. Think about what you are coding and make sure that anyone that knows PHP will be able to read your code.

There is much more to clean code and this article is in no way a complete guide, this was written to get you started on your way to clean code, i would suggest that if you want to learn more to take a look at this article http://www.weberdev.com/ViewArticle.php3?ArticleID=394

Joe Crawford Jr.









Beginners guide to PHP and MySQL
Categories : PHP, Beginner Guides, Databases, MySQL, Installation
Who's Linking?
Categories : PHP, Beginner Guides, To PHP
PHP 101 Part 8 of 15 : Databases and Other Animals
Categories : PHP, Beginner Guides, Databases
Grabb'n and Pars'n
Categories : Beginner Guides, PHP, To PHP
PHP 101 Part 9 of 15 : SQLite My Fire!
Categories : PHP, Beginner Guides, Databases, SQLite
PHP 101 Part 15 of 15 : No News Is Good News
Categories : PHP, Beginner Guides, Content Management
Smarty Introduction
Categories : PHP, Smarty, Beginner Guides
PHP 101 Part 10 of 15 : A Session In The Cookie Jar
Categories : PHP, Beginner Guides, Cookies, Sessions
PHP 101 Part 2 of 15 : Calling All Operators
Categories : PHP, Beginner Guides, Operators
PHP 101 Part 12 of 15 : Bugging Out
Categories : PHP, Beginner Guides, Errors and Logging
Beginners guide to PHP and MySQL - Creating a simple guest book
Categories : Beginner Guides, To PHP, To MySQL, PHP, MySQL
PHP 101 Part 5 of 15 : Rank And File
Categories : PHP, Beginner Guides, Filesystem
Counting - Creating a simple counter
Categories : PHP, MySQL, Beginner Guides, To PHP, To MySQL
PHP 101 Part 7 of 15 : The Bear Necessities
Categories : PHP, Beginner Guides, Object Oriented, PHP Classes
PHP and OOP
Categories : PHP, Object Oriented, Beginner Guides