Today's Posts Follow Us On Twitter! TFL Members on Twitter  
Forum search: Advanced Search  
Navigation
Marketplace
  Members Login:
Lost password?
  Forum Statistics:
Forum Members: 24,256
Total Threads: 81,169
Total Posts: 566,839
There are 48 users currently browsing (tf).
 
  Our Partners:
 
  TalkFreelance     Business and Website Management     Articles From The Experts :

Writing Clean Code

Thread title: Writing Clean Code
Closed Thread    
    Thread tools Search this thread Display Modes  
01-05-2008, 06:43 PM
#1
Village Genius is offline Village Genius
Village Genius's Avatar
Status: Geek
Join date: Apr 2006
Location: Denver, CO
Expertise: Software
Software: Chrome, Notepad++
 
Posts: 6,892
iTrader: 18 / 100%
 

Village Genius will become famous soon enough

  Old  Writing Clean Code

Its been a while since I've written an article, lack of inspiration. If you have any ireas on a subject, please do contact me.

Lately, other people’s code has been getting on my nerves. It is so messy it takes me an hour to just decrypt it. Don’t get me wrong, I can work with other people’s styles if they are clean. This inspired me to make this article on writing clean code. This is not a sure fire guide to clean code, but they are good guidelines.

1. Indentation

What style you use doesn’t really matter, just stick to it. I find the allman indent style the easiest, but it is fully up to you. Just stick with it in the entire application. If you have a team, make sure they all use the same style. The only style you should never use is none.
If you want a list of indent styles, go to Indent style - Wikipedia, the free encyclopedia


2. Brackets
PHP lets you have a single line under an if, while, or for statement without using brackets. Don’t use it. It makes things harder to read and leaves an open door for a bug should you accidentally add second line. Reason being it wont create an error, it will execute the command as normal outside of that condition or loop.


3. Comment your code
Even good code can be complicated. Comment everything you do. This does not mean every line, but every basic function. If you have a complicated process (well, hard to read) comment every step so the next programmer knows what is going on. And remember Eagleson’s law
“Any code of your own that you haven’t looked at for six or more months might as well have been written by someone else.”


4. Make everything as simple as possible, but not simpler.
This Albert Einstein's quote sums up how your coding should be. Simple as possible. Some things simply take complicated, hard to read code. Don’t be afraid to use complicated code where needed, just comment them. But when possible, make it simple. One reason is as Brian W. Kernighan said
“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. “

Other reason is because adding things to applications may make it more complicated. Why not start from easy and work your way up as slow as possible opposed to start at the top and have no room to move.



5. Use descriptive variable names
With good indentation, comments and simple code, bad variable names can still make an application near impossible to modify. In no more then three words (there is a good reason they are not called novels), describe what it does and what it is. Lets say we have a mysql query getting all the post to the forum, a good name would be $posts_query. $query would not be good for the task, all it tells you is what it is, it could be any query. The same goes for functions.


6. Good looking variables
Descriptive naming is essential, but it serves almost no point if your variable name is $postsquery. It makes it harder to read because the next programmer has to separate the words himself. It is better to either capitalize the first letter of each word ($PostsQuery) or use an underscore ($posts_query).




7. If you don’t use a templating system, embed PHP in HTML, not HTML in PHP
PHP gives you the ability to end the PHP and start html, if its within an if statement it will honor it. It is easier to read HTML with embedded PHP then echo’d HTML with all the quotes escaped. For example

PHP Code:
if($condition = “value)
 {
             ?>
             <input type=”text” name=”<?php echo $name?>” />
             <?php
 
}
Opposed to

PHP Code:
If($condition “value)
 {
             echo 
<input type=\”text\” name=\”$name\” />;
 } 
It doesn’t make a huge difference in that instance. But when outputting large blocks of HTML, it is far easier to read if you are putting the PHP in the HTML.

01-05-2008, 07:01 PM
#2
Tom is offline Tom
Tom's Avatar
Status: I Code Things
Join date: Aug 2005
Location: UK
Expertise:
Software:
 
Posts: 1,998
iTrader: 14 / 100%
 

Tom is on a distinguished road

Send a message via MSN to Tom

  Old

<3 u Village. Another great article from village. Keep em coming.

01-05-2008, 07:04 PM
#3
Village Genius is offline Village Genius
Village Genius's Avatar
Status: Geek
Join date: Apr 2006
Location: Denver, CO
Expertise: Software
Software: Chrome, Notepad++
 
Posts: 6,892
iTrader: 18 / 100%
 

Village Genius will become famous soon enough

  Old

Thanks.

01-05-2008, 10:28 PM
#4
Gaz is offline Gaz
Gaz's Avatar
Status: Request a custom title
Join date: Apr 2007
Location: UK
Expertise: Code & Programming
Software: Coda, TextMate, Sublime 2
 
Posts: 2,096
iTrader: 26 / 100%
 

Gaz will become famous soon enough Gaz will become famous soon enough

Send a message via Skype™ to Gaz

  Old

Amazing! I have really taken to reading your articles over at TalkPHP, I find them informative, interesting and extremely well written.

Thank you!

02-02-2008, 10:40 AM
#5
RaZoR^ is offline RaZoR^
RaZoR^'s Avatar
Status: Member
Join date: Feb 2006
Location:
Expertise:
Software:
 
Posts: 191
iTrader: 1 / 100%
 

RaZoR^ is on a distinguished road

  Old

Good article.

You know the Microsoft Windows team don't actually comment the source code for Windows? A mate is good friends with someone who used to work coding the OS, and they only very small parts of the code were commented. He's probably the first Microsoft worker I've ever heard say anything "bad" about the company though lol

Closed Thread    


Thread Tools
Display Modes

  Posting Rules  
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump:
 
  Contains New Posts Forum Contains New Posts   Contains No New Posts Forum Contains No New Posts   A Closed Forum Forum is Closed