The Prettiest Code
An often ignored aspect of programming is formatting and upkeeping the code. It’s great and all if your program works but with a poor coding style, just the thought of editing the code will be a nightmare, not to mention if someone else is going to edit your code, which could possibly make that person want to bash his head into the nearest solid object. And we don’t want that, do we?
Design practices is the more important aspect of writing manageable code. However, in this article, you shall learn about keeping your code pretty with formatting and naming conventions.
TIP OF THE ARTICLE: Be consistent and JFDI
Choose your own style and stick to it. This applies for formatting and design practices. Everyone has their own preferences and that’s good as long as it’s consistent. Make all your if-blocks and tabs look the same. Don’t waste your creative juice here. Be boring and monotone. You’ll appreciate it later.
What I recommend is literally writing down your conventions. Keep a page of sample code that covers all aspects of formatting. Print it out and put it next to your monitor. You’ll refer to it in the beginning but soon enough, your conventions will become a part of you. (very scary)
JFDI. I’ll talk about JDI here (you can figure what JFDI is).
JUST DO IT. Format your code while you are programming THE FIRST TIME. Don’t leave it for later when you’ll have your “formatting” sessions. It barely takes any thought so just keep your code formatted as you go so you’re not left with a half-formatted project later. This will literally benefit all aspects of programming and life so remember: JFDI
Now on with my personal programming style. I’m thinking in context of PHP but these techniques could easily apply to all sorts of programming. These are some of my personal tips.
Naming
- $variable_names
- CONSTANTS_NAMES
- Class_Names
- function_name()
I like to keep my variable names underscored instead of $camelBacked. I find them easier to read. Also, this removes the importance of capital letters, which should be used for…
This is pretty much a given. I like to keep all my constants at the beginning of the code flow and fully capitalized. Again, I like to separate words using underscores.
First letter capitalized is pretty much a given.
I like to name these like variable_names. There is an accepted style for method functions to be camelbacked (i.e. getName()). Choose what you like but I like to keep everything the same.
Formatting
- Four spaces instead of a tab
- Always use braces around a block even if it is a one-liner
I like to format my code using spaces instead of tabs. Most editors have a setting to do this. Four spaces is about right. By avoiding tabs, your code is universal for all sorts of editors and will look more uniform because different platforms/editors treat tabs differently.
The most common example is the following.
if(true) echo "I'm amazing!";
Instead, I will always format it as the following.
if(true)
{
echo "I'm amazing";
}
Reasons include that it is easier to edit in the future, in case you need to insert more commands into the block. Also, this ties in with the uniform style of the rest of the code.
echo 'Hello'.$world.'!';
echo 'Hello' . $world . '!';
Personally, I prefer the second one. I feel it looks cleaner and makes it easier to separate the segments.
Miscellaneous
- Limit the number of echo/print statements
One of the strengths of PHP is its ability to tie itself right into HTML code. If possible, write output as HTML (outside of PHP tags). You can do some PHP logic around HTML code which gives even less reasons to use echo.
<table>
<?php foreach($songs as $song): ?>
<tr>
<td>Title</td>
<td><?php echo $song; ?></td>
</tr>
<?php endforeach; ?>
Echo is fine for outputting variables and short phrases. However, when possible, keep output outside of PHP code.
The main point here is that PHP has to process anything within double quotes while it doesn’t within single quotes. This includes variable names and slashed characters. It never hurts to save couple processing cycles.
echo "Hello!";
echo 'Hello!';
If your code doesn’t contain anything that needs to parsed, using single quotes is almost a no-brainer.
echo "Hello $user!";
echo 'Hello' . $user . '!';
First of all, coding in such a way will save some processing (not having to go through the ‘Hello’ and ‘!’. Also, it helps separate what is static and what is dynamic.
zen perfect does not accept donations but would appreciate it if you
About this entry
You’re currently reading “The Prettiest Code,” an entry on zen perfect
- Published:
- 07.12.07 / 3am
- Category:
- PHP
1 Comment
Jump to comment form | comments rss [?] | trackback uri [?]