Tutorial :Which coding style is more common?



Question:

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.


Solution:1

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


Solution:2

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.)


Solution:3

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.


Solution:4

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.


Solution:5

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.


Solution:6

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.


Solution:7

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.


Solution:8

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.


Solution:9

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.


Solution:10

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.


Solution:11

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.


Solution:12

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.


Solution:13

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


Solution:14

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.


Solution:15

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 *


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »