Coding habits that drive you nutts...
Moderator: General Moderators
- AKA Panama Jack
- Forum Regular
- Posts: 878
- Joined: Mon Nov 14, 2005 4:21 pm
There are only two things that I am really anal about when it comes to programming.
1. Indentation
I get really cheesed off when I have to go through a program file and properly indent everything so I can read it. Half the time the code you download looks like it was written by someone copying and pasting from whatever they found. I find it so damned hard to follow code that isn't indented properly. I can't just look at it and know what is executed within the various functions, if, whiles, ect unless it has been properly indented.
2. Using spaces for indentation.
I absolutely HATE it when people use 4 spaces instead of a single tab for an indentation. It un-necessarily makes the program file larger. Then half the time you will get a file that is a MIX of tabs and spaces used for indenting. ARRRRGGGG!!!!!
I usually end up performing a search and replace on any program file I get to convert all instances of 4 spaces to 1 tab.
I did this to one popular program file and it replaced over 6000 instances of 4 spaces being used for each indentation and reduced the file size from 89k to 74k. BTW, PHP is a little faster processing a program file using tabs to indent instead of spaces. It's very, very minimal but it is there.
1. Indentation
I get really cheesed off when I have to go through a program file and properly indent everything so I can read it. Half the time the code you download looks like it was written by someone copying and pasting from whatever they found. I find it so damned hard to follow code that isn't indented properly. I can't just look at it and know what is executed within the various functions, if, whiles, ect unless it has been properly indented.
2. Using spaces for indentation.
I absolutely HATE it when people use 4 spaces instead of a single tab for an indentation. It un-necessarily makes the program file larger. Then half the time you will get a file that is a MIX of tabs and spaces used for indenting. ARRRRGGGG!!!!!
I usually end up performing a search and replace on any program file I get to convert all instances of 4 spaces to 1 tab.
I did this to one popular program file and it replaced over 6000 instances of 4 spaces being used for each indentation and reduced the file size from 89k to 74k. BTW, PHP is a little faster processing a program file using tabs to indent instead of spaces. It's very, very minimal but it is there.
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
- R4000
- Forum Contributor
- Posts: 168
- Joined: Wed Mar 08, 2006 12:50 pm
- Location: Cambridge, United Kingdom
I think he knows that, just most people use 4 spaces to indent (i do, because tab isnt really that good from what i have seen, things dont line up if you use different editors and alsorts of things)d11wtq wrote:A tab is not 4 spaces, it's not 8, nor 10 nor 16 nor any fixed number for that matterAKA Panama Jack wrote:I usually end up performing a search and replace on any program file I get to convert all instances of 4 spaces to 1 tab.
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
LOL... You got me therefeyd wrote:Of course it's a fixed number: 9. (ordinal value of the character.)d11wtq wrote:A tab is not 4 spaces, it's not 8, nor 10 nor 16 nor any fixed number for that matter
Nah but seriosuly that's what I was saying. Tabs aren't a fixed number of spaces in size, they are white-space delimiters which most editors build into columns at points every X spaces, so if you converted all tabs to spaces your indentations will mess up. Different editors use different logic to calculate their positions too and thus you'll likely see slight differences in how far a tab indents something depending what else is on that line.
I'm rambling again... this is happening to me a lot recently
- AKA Panama Jack
- Forum Regular
- Posts: 878
- Joined: Mon Nov 14, 2005 4:21 pm
I know that but people and most editors that allow indentation using spaces default to 4 spaces equaling a single tab or indentation. Quite frankly of all the php files I have looked at over the years the vast majority of them that use spaces instead of tabs use 4 spaces for each indentation step. It has been rare to find any that use a different formula. Of those that don't use 4, they tend to use 3 or 5 and that is extremely rare.d11wtq wrote:A tab is not 4 spaces, it's not 8, nor 10 nor 16 nor any fixed number for that matterAKA Panama Jack wrote:I usually end up performing a search and replace on any program file I get to convert all instances of 4 spaces to 1 tab.
Actually, in most editors indentation IS a fixed size.d11wtq wrote:Tabs aren't a fixed number of spaces in size, they are white-space delimiters which most editors build into columns at points every X spaces, so if you converted all tabs to spaces your indentations will mess up. Different editors use different logic to calculate their positions too and thus you'll likely see slight differences in how far a tab indents something depending what else is on that line.
Bottom line is "Stop using spaces for indentation.". It just ends up creating messy code. Especially if one person was using 3 spaces and then the next person to edit the code used 5 space to indent. Everything just starts to look sloppy and poorly formatted. If everyone used tabs for indentation then it will look the same all of the time.
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
I'm not going to go off topic here but it's not a fixed size at all. Yes OK, at the far left margin it's X spaces (4 ?) but if you typed two characters and then hit tab you only move the caret by two characters in position, not 4. So if you Typed:AKA Panama Jack wrote:I know that but people and most editors that allow indentation using spaces default to 4 spaces equaling a single tab or indentation. Quite frankly of all the php files I have looked at over the years the vast majority of them that use spaces instead of tabs use 4 spaces for each indentation step. It has been rare to find any that use a different formula. Of those that don't use 4, they tend to use 3 or 5 and that is extremely rare.d11wtq wrote:A tab is not 4 spaces, it's not 8, nor 10 nor 16 nor any fixed number for that matterAKA Panama Jack wrote:I usually end up performing a search and replace on any program file I get to convert all instances of 4 spaces to 1 tab.
Actually, in most editors indentation IS a fixed size.d11wtq wrote:Tabs aren't a fixed number of spaces in size, they are white-space delimiters which most editors build into columns at points every X spaces, so if you converted all tabs to spaces your indentations will mess up. Different editors use different logic to calculate their positions too and thus you'll likely see slight differences in how far a tab indents something depending what else is on that line.Sure some editors will allow you to change the number spaces for an indentation but if you DO change it they do not automatigcally change the indentation of the document. They just start using the new number of spaces for new indentations. Of all the php files that have used spaces for indentation converting them to tabs doesn't mess them up at all. They end up looking the same only tabs are used instead of 4 spaces.
Bottom line is "Stop using spaces for indentation.". It just ends up creating messy code. Especially if one person was using 3 spaces and then the next person to edit the code used 5 space to indent. Everything just starts to look sloppy and poorly formatted. If everyone used tabs for indentation then it will look the same all of the time.
a,a,<space>,<space>,<space>,<space>,b,b,c
That's NOT the same as:
a,a,<tab>,b,b,c
Here's a function I wrote last year that converts tabs into spaces
Code: Select all
function tabs2spaces($source, $ents=false, $s=4) {
$space = ($ents)? ' ' : ' '; //Space or entity?
$lines = explode("\n", $source); //Array of lines
$mod = array();
foreach ($lines as $l) {
while (false !== $pos = strpos($l, "\t")) { //Remember position 0 equates to false!
$i = substr($l, 0, $pos);
$t = str_repeat($space, ($s - $pos % $s)); //Width of the tab
$e = substr($l, $pos+1);
$l = $i.$t.$e; //Rebuild the line
} //End while
$mod[] = $l;
} //End foreach
$source = implode("\n", $mod);
return $source;
}- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
- AKA Panama Jack
- Forum Regular
- Posts: 878
- Joined: Mon Nov 14, 2005 4:21 pm
I guess you don't use any of the major editors and let them do the indentation but do it all manually by pressing the spacebar. That really is a sloppy way of doing things.
Most programming editors that allow spaces to be used in place of tabs will calculate how many spaces are needed to indent similar to using a tab.
Using an editor like Homesite, if I pressed the tab key with the use spaces instead of tabs enabled then using your example Homesite would only add TWO spaces to simulate a REAL tab indentation.
aa<space><space>bbc
It would not just toss in 4 spaces because it was set to 4 spaces for an indentation. It calculates how many spaces are needed in relation to how many characters are on the line to maintain proper indentation alignment. Most programming editors that allow space based indentation do this.
Manually hitting the space bar 4 times is inherently sloppy and tends to lead to misaligned indentation. It is always best to let the editor do it for you. Then there is no need for using a program such as you had to create.
Most programming editors that allow spaces to be used in place of tabs will calculate how many spaces are needed to indent similar to using a tab.
Using an editor like Homesite, if I pressed the tab key with the use spaces instead of tabs enabled then using your example Homesite would only add TWO spaces to simulate a REAL tab indentation.
aa<space><space>bbc
It would not just toss in 4 spaces because it was set to 4 spaces for an indentation. It calculates how many spaces are needed in relation to how many characters are on the line to maintain proper indentation alignment. Most programming editors that allow space based indentation do this.
Manually hitting the space bar 4 times is inherently sloppy and tends to lead to misaligned indentation. It is always best to let the editor do it for you. Then there is no need for using a program such as you had to create.
- AKA Panama Jack
- Forum Regular
- Posts: 878
- Joined: Mon Nov 14, 2005 4:21 pm
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
- AKA Panama Jack
- Forum Regular
- Posts: 878
- Joined: Mon Nov 14, 2005 4:21 pm
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
- Maugrim_The_Reaper
- DevNet Master
- Posts: 2704
- Joined: Tue Nov 02, 2004 5:43 am
- Location: Ireland
I blogged a few of the coding habits I dislike the most - http://blog.quantum-star.com/index.php? ... ectly.html
Overall I find if it takes away from 1) readability and 2) understanding then its going to wind up a bad practice. The most recent app/lib I came across that drive me nuts was ADOdb. Some of its files are pure torture to go through. They have up to 3 alias functions for the same effect with another 4th by directly accessing a class property, inconsistent tabs, lack of braces around IF statements, no commenting worth mentioning, plus it is really is not well organised. A lot of it looks like someone started adding bandaids and kept at it until they were all overlapping and obscuring the code which actually does something useful...
Personal favourites are not using full sentences or grammar, lack of braces, and using short-tags or even asp-tags (very rare I know). As for whether some things are worth complaining about (spacing) they can be in the right circumstances. I gave up long ago though on tabs vs spaces - I wish everyone would just use tabs, but not all editors support them so I live with it.
Overall I find if it takes away from 1) readability and 2) understanding then its going to wind up a bad practice. The most recent app/lib I came across that drive me nuts was ADOdb. Some of its files are pure torture to go through. They have up to 3 alias functions for the same effect with another 4th by directly accessing a class property, inconsistent tabs, lack of braces around IF statements, no commenting worth mentioning, plus it is really is not well organised. A lot of it looks like someone started adding bandaids and kept at it until they were all overlapping and obscuring the code which actually does something useful...
Personal favourites are not using full sentences or grammar, lack of braces, and using short-tags or even asp-tags (very rare I know). As for whether some things are worth complaining about (spacing) they can be in the right circumstances. I gave up long ago though on tabs vs spaces - I wish everyone would just use tabs, but not all editors support them so I live with it.