Brace Yourself

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Cruzado_Mainfrm
Forum Contributor
Posts: 346
Joined: Sun Jun 15, 2003 11:22 pm
Location: Miami, FL

Post by Cruzado_Mainfrm »

i prefer this way:

Code: Select all

<?php
for ($i=3;$i<12;$i++) {
     echo $i;
     echo "this might or not be a long line but when things go"
            ." crazy you might want to cut that so u can read"
            ." and you will not get sore eyes";
     print_r ($i);
     //for insert queries that have indexes:
     //table:  id-firstname-lastname
     $query = "INSERT INTO table (firstname,lastname) VALUES ("Johnny","Blaer")"; //see? you don;t need to specify the 'id' field, that's redundant, cuz mySQL does that for you anyways!
     mysql_query($query,$somelink);
     include "WhyUseParenthesesIfTheyAreNotNeededAnyways.php";
}
?>
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

I prefer this method:

Code: Select all

<?php

function name ()
{
	while (false) {
		do_something();
	}
}

?>
A consistant style that makes thing easier to read in general.

You may be wondering why my function { is on it's own line, and my while { is not. This is for practical purposes, and to signify that function declarations and block elements are different.

function name () {} doesn't do anything.

Really, it doesn't. It's there simply the define a function. However, a while () {} or if () {} does do something.

The reason I indent and setup the way I do is primarily because of this:

Consider a block of code that is filtered like such:

Code: Select all

1. ###############
2. ###############
3. ###############
4.     ###############
5.     ###############
6. ###############
Now, where does the block start? If I was putting my { on it's own line, then the block would start on row 2. Why is this important? Because when you are looking through a large amount of code, you are usually not reading the code, but rather, following it's paths.

Basically, you should read "Code Complete" for my reasons for structuring my code the way I do.

However, whatever method you choose, consistancy is the key.
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

I use a mix sometimes, because I was taught to write like:

Code: Select all

<?PHP 
if($thing=="seven"){
echo "guess what? thing = seven!!";
}
?>
but i personally like reading code like this:

Code: Select all

<?PHP 
if ($thing == "seven")
{
  echo "thing = seven!!";
}
?>
but sometimes i forget to write like that.. so i end of going back and adding line breaks :P
fractalvibes
Forum Contributor
Posts: 335
Joined: Thu Sep 26, 2002 6:14 pm
Location: Waco, Texas

Post by fractalvibes »

Either style works fine. No matter the style, being consistant will probably save some time and effort in the long run and enable you to catch some
oops much easier! The same applies for variable naming, sqlstrings, etc.etc. etc.

fv
Post Reply