WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDESPHP CLASSESCODE SEARCHARTICLES SEARCHPHP FORUMSPHP MANUALPHP FUNCTIONS LISTWEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Join us on FaceBook
Submit a code Example / Snippet Submit Your Code
Poker Tournaments Poker Tournaments
Poker Guide for Developers Poker Guide for Developers
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
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 Resources
Web Development Content
Internet Security Software
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
פרייסז - הכח לקנות עובר לידיים שלך
Texas Holdem Poker Evangelists

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 : 9545
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 - Creating a simple guest book
Categories : Beginner Guides, To PHP, To MySQL, PHP, MySQL
Smarty Introduction
Categories : PHP, Smarty, Beginner Guides
PHP 101 Part 13 of 15 : The Trashman Cometh
Categories : PHP, Beginner Guides, Data Validation
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
How To add paging (Pagination) with PHP and MySQL
Categories : PHP, Beginner Guides, Databases, MySQL, HTML and PHP
PHP Classes And Objects: A Guide To Development
Categories : PHP, PHP Classes, Object Oriented, Beginner Guides
Beginners Guide to PHP - Introduction to cookies
Categories : Beginner Guides, Cookies, To PHP, PHP
PHP 101 Part 8 of 15 : Databases and Other Animals
Categories : PHP, Beginner Guides, Databases
PHP 101 Part 9 of 15 : SQLite My Fire!
Categories : PHP, Beginner Guides, Databases, SQLite
Beginners guide to PHP and MySQL
Categories : PHP, Beginner Guides, Databases, MySQL, Installation
Counting - Creating a GIF based counter using PHP and MySQL
Categories : Beginner Guides, PHP, To PHP, To MySQL, MySQL
How TO Install PHP, Apache and MySQL on Linux or Unix
Categories : PHP, MySQL, Apache, Installation, Beginner Guides
Counting - Creating a more sophisticated GIF based counter using PHP and MySQL
Categories : Beginner Guides, MySQL, PHP, To PHP, To MySQL
PHP 101 Part 10 of 15 : A Session In The Cookie Jar
Categories : PHP, Beginner Guides, Cookies, Sessions