5 tips to make your PHP code beautiful (3)

Avoid unnecessary control structures.

Brackets everywhere. Try to get rid of those as much as you can. Unnecessary brackets add visual complexity and make your code much longer. In real world situation, when you need to quickly browse through your code, they just add clutter

//prints out the structure of a table
function describe( $table )
{
	$query = "SHOW COLUMNS FROM `$table `";
	$result =mysql_query( $query );
	if ( ! $result )
	{
		return $result;
	} else {
		$data = array();
		while ( $record = mysql_fetch_assoc( $result ) )
		{
			$data[] =  $record;
		}
		return $data;
	}
}

In the function above the if-else structure is not needed, just because when $result is false I can just return it, breaking the flow. Also while ( $record = ..) uses brackets for looping over just one statement.

We can make describe() look much more tidy:

function describe( $table )
{
	$query = "SHOW COLUMNS FROM `$table `";
	$result =mysql_query( $query );
	if ( ! $result ) return $result;
	$data = array();
	while ( $record = mysql_fetch_assoc( $result ) ) $data[] =  $record;
	return $data;
}

Seems nicer, uh ? While one could think that’s just a detail, when code complexity grows, this kind of tricks can help you cutting it down.

This seems like a great time to subscribe my RSS !

Leave a Reply

Bills

Don’t forget to Subscribe

Latest Activity

Posts

  • Google this is ridiculous
    Google you’re doing it wrong. Very wrong. This is utterly ridiculous. It’s a screenshot with a standard Firefox browser, in the standard screen resolution (1280×800), on Read More
  • A step back from the open source
    One month ago, I created my first Android app. While the app was a paid one, the reception has been outstanding. I’ve gotten a fair amount Read More
  • Facebook shuts down EventPress development.
    When you’re a big company, especially one that makes its money on free web services and advertising, it’s very easy to say you love open Read More
  • Why Android is laggy
    Great post from Andrew Munn explaining some key differences between Android and iPhone/iOs, especially when it comes to rendering and smoothness of animations and why Read More
  • Google Plus keeps your data as much as Facebook does
    When you delete an account from Google+, Google promises you to delete all the data associated with your G+ profile. Well, I deleted mine some time Read More