views:

348

answers:

15

In no way shape or form am i advertising/promoting my programming style, but as far as 'multiple variable declarations' are concerned, which case is more acceptable professionally and commonly:

case 1:

private $databaseURL   = "localhost"  ;
private $databaseUName = "root"       ;
private $databasePWord = ""           ;
private $databaseName  = "AirAlliance";

case 2:

private $databaseURL = "localhost";
private $databaseUName = "root";
private $databasePWord = "";
private $databaseName = "AirAlliance";

The reason i like case 1 is because i can skim though it and see that all is correct way faster than case 2. Also i can visually get familiar with variable names witch makes it faster to work with them l latter on the program.

+15  A: 

Whichever style the project is already using, so you dont end up spending all day "fixing" every file you touch.

Joe Koberg
Like I have. :)
Joe Koberg
+7  A: 

Case 2. When you add a new variable to your "group", or add/remove a character on one of those lines, you won't have to spend time fiddling around trying to make things line up.

harto
+3  A: 

Interesting question. As far as I'm concerned, there's no good reason to use case 1. I've never seen that style before, except possibly in config files, and it costs extra time to format your code properly. As soon as you change the contents of the variable, you have to reset your semi-colon as well.

It only works if you use spaces as well... anyone using tabs might have different tab stops set, so it will look completely different opened in another editor.

I think the advantage of readability you mention is somewhat offset by the extra effort required to write it.

zombat
+13  A: 

case 1.5:

private $databaseURL   = "localhost";
private $databaseUName = "root";
private $databasePWord = "";
private $databaseName  = "AirAlliance";

I don't think aligning the semicolons makes it any more readable, it just makes it annoying to change the value of one of the strings and then have to add or delete spaces to line up the semicolons all over again.

In this case, it looks like the variable names are unlikely to change, so it should be OK to line up the equals signs. If it were possible that the variable names would change, however, I would go with case 2 because (again) it would be annoying to have to line everything up again. (Imagine the work involved in simply adding a new variable called $databaseLongVariableName.)

yjerem
Exactly, I don't see how aligning semicolons would make any difference, besides being a pain to do!
alex
Ah, emacs and the join of M-X align-regexp-region
Dominic Rodger
In TextMate ⌘⌥] (Command + Option + Right Bracket) will do this.
GloryFish
A: 

Aligning the semicolons seems like wasted time to me, unless your IDE/text editor can do it for you. Especially if you later add another entry longer than the previous ones; you'll have to realign all of them.

pyrochild
A: 

If there is an accepted "Pretty Print" function, I use that. Although it is seldom 100% what you like, it means that the coding style remains consistent, which in the end makes it more maintainable.

If there is no such function, I follow the existing style.

Esti
A: 

I just fired up my editor and it just hit me. all the reasons i like case 1 for are in syntax highlighting without wasting time.

Babiker
Your editor hit you? I suppose that's what you get for firing it up :-)
paxdiablo
+5  A: 

If you change all their alignments when you add a new variable, then any diff tool will show up multiple changes there. Just a thought.

Also, I've done this once. It became a pain in the ass when I had to add a variable that was longer. Padding out all the existing ones with spaces to match turned me off this technique.

alex
Good point about the diff. I wonder if there is an intelligent diff tool out there that will ignore changes to formatting.
Calvin
@Calvin ... perhaps there is, but I do not know of one that exists.
alex
A: 

case 2. with anything else your version control will go crazy everytime you re-adjust the spacing. besides with IDEs that has highlighting and some also show a variable listing, its really a waste of time to align '=' and ';' in my opinion.

Sujoy
A: 

Case 2. If I leave out a semicolon, I have a program that tells me so. It's the compiler.

gbarry
+1  A: 

Use case 2. That is far more standard: Google C++ Style

Really you shouldn't even concern yourself with this. Your editor should automatically autoformat your code. Try ctrl+shift+F in Eclipse.

+3  A: 

It's very tempting to nicely align stuff, I used to align my accessors in PHP as so

function getName()  { return $this->name;   }
function getAge()   { return $this->age;    }
function getHeight(){ return $this->height; }

The problem comes when you add in a longer line:

function getName()  { return $this->name;   }
function getAge()   { return $this->age;    }
function getNiNumber(){ return $this->ni_number; }
function getHeight(){ return $this->height; }

If I edit the three other lines, then when I commit my change it makes it harder to see who wrote a particular line, and in which revision they did it.

Ciaran McNulty
+1, which just tempts people to add even more useless spaces to keep the existing alignment.
Tim Post
A: 

Going with what ever the project you are working on is the best option. (If the project doesn't have a set of coding guidelines, spending five minutes to define them is highly recommended).

A lot of the larger programs have tools which scan the code looking for things to sort and clean. Adding whitespace at the end of the variable could affect some of them.

Also regarding tabs, I've never worked on a project that didn't determine the tab spacing for the editing. You can always use your personal one and have a precommit hook to edit the file before it goes in, but messing with tab spacing will annoy people no end.

Ryaner
+1  A: 

Aligning stuff like = and ; just invites a whole big mess of tab-space screw ups that get checked in, then have to be fixed later.

Most people will use tabs to the left of a line for indentation, then use spaces to align stuff to the right.

Beyond adding bloat to files, it just becomes a big, inconsistent soupy mess. To me, it does not make the code any more readable. To some editors, it makes the code even less readable.

Tim Post
A: 

You can have a look at the book "Expert PHP 5 Tools" for better understanding the code styling in more detail way. *There is e-book in net *

Bakhtiyor