Using the Python Interpreter as a Reference

Devin Jeanpierre jeanpierreda at gmail.com
Fri Dec 2 15:58:23 EST 2011


> In my opinion, if your code is indented four or more levels, you should
> start to think about refactorising your code; if you reach six levels,
> your code is probably a mess.

Here's some code I encountered while grading assignments from
first-year CS students:

        if 'not' in temp_holder:
            for item in (range(len(ial))):
                for key in (range(len(ial[item]))):
                    if type(ial[item][key]) == str:
                       if temp[term].find(ial[item][key]) > 0:
                          for value in range(len(ial[item][1])):
                              if ial[item][1][value] in images:
                                 while ial[item][1][value] in images:
                                       images.remove(ial[item][1][value])

I think after some point of nesting, not only can we conclude that the
code is difficult to read, we can probably conclude the author wasn't
thinking very clearly about what he or she was doing.

Devin

On Thu, Dec 1, 2011 at 7:43 PM, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:
> On Thu, 01 Dec 2011 10:03:53 -0800, DevPlayer wrote:
>
> [...]
>> Well, that may be a little hyperbolic. But with 2 spaces you can
>> encourage coders to get very deep, indentially, and still fit 80 chars.
>
> Why would you want to encourage coders to write deeply indented code?
>
> In my opinion, if your code is indented four or more levels, you should
> start to think about refactorising your code; if you reach six levels,
> your code is probably a mess.
>
> class K:
>    def spam():
>        if x:
>            for a in b:
>                # This is about as deep as comfortable
>                while y:
>                    # Code is starting to smell
>                    try:
>                        # Code smell is now beginning to reek
>                        with z as c:
>                            # And now more of a stench
>                            try:
>                                # A burning, painful stench
>                                if d:
>                                    # Help! I can't breathe!!!
>                                    for e in f:
>                                        # WTF are you thinking?
>                                        try:
>                                            # DIE YOU M***********ER!!!
>                                            while g:
>                                                # gibbers quietly
>                                                ...
>
>
> The beauty of languages like Python where indentation is significant is
> that you can't hide from the ugliness of this code.
>
> class K: {
>  # Code looks okay to a casual glance.
>  def spam():{
>   if x: { for a in b:{
>     while y:{ try:{ with z as c:{
>       try:{ if d:{ for e in f:{ try:{
>         while g:{ ... }}}}
>       }}}}
>     }}}}
>
> Deeply indented code *is* painful, it should *look* painful.
>
>
> --
> Steven
> --
> http://mail.python.org/mailman/listinfo/python-list


More information about the Python-list mailing list