Coding habits that drive you nutts...

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

User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post by AKA Panama Jack »

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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

AKA 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.
A tab is not 4 spaces, it's not 8, nor 10 nor 16 nor any fixed number for that matter ;)
User avatar
R4000
Forum Contributor
Posts: 168
Joined: Wed Mar 08, 2006 12:50 pm
Location: Cambridge, United Kingdom

Post by R4000 »

d11wtq wrote:
AKA 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.
A tab is not 4 spaces, it's not 8, nor 10 nor 16 nor any fixed number for that matter ;)
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)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

d11wtq wrote:A tab is not 4 spaces, it's not 8, nor 10 nor 16 nor any fixed number for that matter ;)
Of course it's a fixed number: 9. (ordinal value of the character.) :P
User avatar
R4000
Forum Contributor
Posts: 168
Joined: Wed Mar 08, 2006 12:50 pm
Location: Cambridge, United Kingdom

Post by R4000 »

feyd wrote:
d11wtq wrote:A tab is not 4 spaces, it's not 8, nor 10 nor 16 nor any fixed number for that matter ;)
Of course it's a fixed number: 9. (ordinal value of the character.) :P
heh, good one :P
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

feyd wrote:
d11wtq wrote:A tab is not 4 spaces, it's not 8, nor 10 nor 16 nor any fixed number for that matter ;)
Of course it's a fixed number: 9. (ordinal value of the character.) :P
LOL... You got me there :P

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 :?
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post by AKA Panama Jack »

d11wtq wrote:
AKA 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.
A tab is not 4 spaces, it's not 8, nor 10 nor 16 nor any fixed number for that matter ;)
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: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.
Actually, in most editors indentation IS a fixed size. :) 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

AKA Panama Jack wrote:
d11wtq wrote:
AKA 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.
A tab is not 4 spaces, it's not 8, nor 10 nor 16 nor any fixed number for that matter ;)
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: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.
Actually, in most editors indentation IS a fixed size. :) 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.
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:

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)? '&nbsp;' : ' '; //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;
	
}
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

White space, inconsistent variable and file names, tabs or spaces ... why do such trivial things bug some people so much?
(#10850)
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post by AKA Panama Jack »

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.
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post by AKA Panama Jack »

arborint wrote:White space, inconsistent variable and file names, tabs or spaces ... why do such trivial things bug some people so much?
It's like having a small pebble in your shoe. :) Annoying as hell when you have to walk.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

AKA Panama Jack wrote:It's like having a small pebble in your shoe. :) Annoying as hell when you have to walk.
But is the problem the pebble? Or that you are wearing the wrong shoes?
(#10850)
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post by AKA Panama Jack »

Nope, nice soft comfy shoes that put a spring in my step. ;)
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

I go with sandals and the pebbles just fall by the wayside. :)
(#10850)
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

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.
Post Reply