[Tutor] 2 & 4 or more spaces per indentation level..

Alan Gauld alan.gauld at btinternet.com
Fri Jan 16 10:05:09 CET 2009


"Eric Dorsey" <dorseye at gmail.com> wrote

> Working in IDLE on Windows Vista, I have one program that I set to 
> have 2
> character spacing (due to the levels of if's and while's going on --  
> later
> my brother called this a bit of a code smell, ie. logic shouldn't go 
> that
> deep, it should be broken out into separate functions instead. 
> Thoughts on
> that are welcome to, do any of you feel logic should only go so many 
> layers
> deep?),

Deeply nested logic tends to be hard to debug and hard to terst 
thoroughly
so yes, this is considered a bad smell. Refactoring the inner parts of 
a loop
into a function is often a good way to deal with this but it may have 
a
performamnce impact if its a critical bit of code so its not a cure 
all.

Usually its possioble to reduce nesting by using a different logic 
flow
(De Morgans rules often help here) or by using higher level structures
like list comprehensions instead of loops. In fact functional 
programming
techniques in general can help minimise nesting of code - this is one 
of
the claimed advantages of functional languages, that they lead to less
deeply nested programs...

> I copied some code from the 2 spacing program to another I'm writing
> currently which has the default 4, and it got things kind of screwy 
> with
> spacing.

This can happen if you don't have the code in functions. Its easy to
copy a function but copying arbitrary lines out of a function is much
more risky.

> but I was wondering, if this happened on a much bigger scale, or you 
> were
> say, pasting code in from some example where they used a different 
> spacing
> than you, is there a simple/good/smart way to get it all back to the 
> 4
> spacing default?

There are many tools around that can do this kind of thing - indent,
tab nanny, untabify, format etc... Pick your own favourite.

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 




More information about the Tutor mailing list