Poll

Which of these is easier for [you] to read

fun_1
fun_2

Author Topic: Reading Code  (Read 5013 times)

0 Members and 1 Guest are viewing this topic.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Reading Code
« on: March 12, 2010, 03:48:26 PM »
Which of these is easier for [you] to read.
I don't mean to ask if you can understand either set of code, I mean which formatting lets can you grasp the code intent easier.

Code: [Select]
(defun fun_1 (l n / a b c d)
    (while (Setq d l)
        (while (< (setq b (length a)) n)
            (setq a (cons (nth b d) a) l (cdr l)))
            (setq  c (cons (reverse a) c)  a nil))
            (reverse c))

Code: [Select]
(defun fun_2 (l n / a b c d)
  (while (setq d l)
    (while (< (setq b (length a)) n)
      (setq a (cons (nth b d) a)
            l (cdr l)
      )
    )
    (setq c (cons (reverse a) c)
          a nil
    )
  )
  (reverse c)
)

kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Lee Mac

  • Seagull
  • Posts: 12924
  • London, England
Re: Reading Code
« Reply #1 on: March 12, 2010, 03:50:09 PM »
Is this another dig at my coding style...  :evil:

fun_1  But with the indentation set to how it should be.


Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Reading Code
« Reply #2 on: March 12, 2010, 03:53:21 PM »

No Lee, this is NOT a dig at you.
I've seen several people attempt to use the closing at end of line format style, and wanted to know what our users think.
The first code block is reproduced exactly as it was posted.
The second as formatted by VLIDE.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Lee Mac

  • Seagull
  • Posts: 12924
  • London, England
Re: Reading Code
« Reply #3 on: March 12, 2010, 03:55:41 PM »
No Lee, this is NOT a dig at you.

Just messing with you Kerry  :wink:

I realise that was how it was posted - but it isn't the correct way that the end-of-line style is formatted by the VLIDE and makes it difficult to decipher even when you are used to the end-of-line style... bit of unfair survey IMO..

GDF

  • Water Moccasin
  • Posts: 2081
Re: Reading Code
« Reply #4 on: March 12, 2010, 04:09:34 PM »
I think the same way about dcl code formatting...

for example:

//To load master widgets used by Arch Program© for AutoCAD®
@include "..\\ARCH.dcl"
dcl_settings :default_dcl_settings {audit_level=0;}

////////////////////////////////////////////////////////////////////////////////////

arch_program_multi_layerreactor_toggle
  :row {children_fixed_height=true; children_fixed_width=true;     
    :arch_multi {}
    :arch_curlaysetreactor {arch_curlaysetreactor;}
    :text_part {label="Leader";}
    :toggle {label = "Poly"; key="poly";}
    :toggle {label = "Bloc"; key="bloc";}     
  }   

////////////////////////////////////////////////////////////////////////////////////

ARCH_ANNO :dialog {key = "set-title"; initial_focus="set-title"; width=93.3; height=21.875;
  :row {
    :boxed_row {label="Control Settings"; width=33.875;
      :column {height=4.025; children_alignment=top;
        :row {
          :toggle {label = "Dtx"; key="dtext"; width=3;}               
          :edit_box {key="txdsv"; edit_width=4;}                 
        }
        :row {       
          :toggle {label = "Mtx"; key="mtext"; width=3;}       
          :edit_box {key="txmsv"; edit_width=4;}
        }       
      }         
      :column {
        :row {         
          :toggle {label = "CSI"; key="csi"; width=3;}
          :toggle {label = "GEN"; key="gen"; width=3;}       
        }
        :row {         
          :button {label="Style"; key="txsty"; fixed_width=true;}
          :arch_35x35_image_15 {key="chx";}
        }
      }
    }
    :boxed_column {label="Annotation Commands";
      :arch_cmdname {arch_cmdname;}       
      :popup_list {key="commands-list"; width=51.5; tabs="11"; multiple_select=false;}
      :spacer {}
    }   
  } 
  :arch_program_multi_layerreactor_toggle {arch_program_multi_layerreactor_toggle;} 
  :row {height=11.875;
    :column {                 
      :text_part {key = "clistag"; width=18;}
      :list_box {key = "clist"; width=18; height=9;} 
      :popup_list {key="issue"; width=18; multiple_select=false;}       
    }   
    :row { 
      :column {
        :list_box {key = "nte"; width=56.5; height=12; allow_accept=true;} //71 12       
      }
      :row {
        :column {
          :arch_35x35_image_15 {key="seq";}
          :arch_35x35_image_15 {key="atx";}         
          :arch_35x35_image_15 {key="btx";}
          :arch_35x35_image_15 {key="case";} 
          :arch_35x35_image_15 {key="sbox";}
        }
        :column {
          :arch_35x35_image_15 {key="fat";}
          :arch_35x35_image_15 {key="ddt";}
          :arch_35x35_image_15 {key="mrgtx";}             
          :arch_35x35_image_15 {key="rtx";}
          :arch_35x35_image_15 {key="it";}
        }
        :column {
          :arch_35x35_image_15 {key="nn";}           
          :arch_35x35_image_15 {key="txn";}       
          :arch_35x35_image_15 {key="txtmatch";} 
          :arch_35x35_image_15 {key="txw";}
          :arch_35x35_image_f15 {}
        }           
      }           
    }
  }     
  :arch_program_textstyle {arch_program_textstyle;}
}

/////////////////////////////////////////////////////////////////////////////////////           

  styletable :dialog {label=" Set-Chg Textstyle:"; alignment=centered; initial_focus="cancel";
   :text {key ="T_num";}
   :list_box {key="TextPop"; width=27; is_default=true; allow_accept=true;}
   :row {
     :spacer {}
     :button {label="eXit"; key="cancel"; fixed_width=true; mnemonic="X";}
     :spacer {}
   }   
 }
Why is there never enough time to do it right, but always enough time to do it over?
BricsCAD 2020x64 Windows 10x64

gile

  • Gator
  • Posts: 2520
  • Marseille, France
Re: Reading Code
« Reply #5 on: March 12, 2010, 04:37:31 PM »
fun_2
Speaking English as a French Frog

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Reading Code
« Reply #6 on: March 12, 2010, 05:46:24 PM »
I read my own style best. (fun_2)
I find that I need to reformat the ultra condensed (fun_1) and the expanded style to do any serious investigation of the code.
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: Reading Code
« Reply #7 on: March 12, 2010, 05:52:05 PM »
fun_2
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

Lee Mac

  • Seagull
  • Posts: 12924
  • London, England
Re: Reading Code
« Reply #8 on: March 12, 2010, 05:58:47 PM »
I find that I need to reformat the ultra condensed (fun_1) and the expanded style to do any serious investigation of the code.

That's funny, I have to do the opposite for other people's code  8-)

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: Reading Code
« Reply #9 on: March 12, 2010, 06:02:14 PM »
(setq i 1 a "blah") <- Just not easy to look at.
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

mkweaver

  • Bull Frog
  • Posts: 352
Re: Reading Code
« Reply #10 on: March 12, 2010, 07:25:38 PM »
fun_2

pkohut

  • Guest
Re: Reading Code
« Reply #11 on: March 12, 2010, 07:38:16 PM »
fun_2

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: Reading Code
« Reply #12 on: March 13, 2010, 02:25:36 AM »
fun_2

wizman

  • Bull Frog
  • Posts: 290
Re: Reading Code
« Reply #13 on: March 13, 2010, 04:57:33 AM »
that looks famiiar Kerry.. :-), fun_2 is easier to grasp, i agree, i usually do the visual lisp formatting but this time just tried the other way which i think looks also pleasant.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Reading Code
« Reply #14 on: March 13, 2010, 06:12:53 AM »
wiz,
I changed the name to protect the innocent :)

Pleasant looking is great, but if I (and others) have to copy code to an editor and format it to make the intent understandable (to me) it's not doing what it's supposed to ... communicate the instructions to solve a problem. If I have to reformat code to be sure of the design I won't bother 9 times out of 10 ; and if someone has a problem to be solved they need the code to be as clear and unambiguous as possible.

This of course is my personal opinion and may bear no kinship to reality.



kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

wizman

  • Bull Frog
  • Posts: 290
Re: Reading Code
« Reply #15 on: March 13, 2010, 06:51:57 AM »
Thanks Kerry, I consider this more as a guidance.  :-)

Lee Mac

  • Seagull
  • Posts: 12924
  • London, England
Re: Reading Code
« Reply #16 on: March 13, 2010, 07:45:26 AM »
The VLIDE formats fun_1 with the following end-of-line style:

Code: [Select]
(defun fun_1  (l n / a b c d)
  (while (setq d l)
    (while (< (setq b (length a)) n)
      (setq a (cons (nth b d) a)
            l (cdr l)))
    (setq c (cons (reverse a) c)
          a nil))
  (reverse c))

I think this is clearer for me  :-)

jbuzbee

  • Swamp Rat
  • Posts: 851
Re: Reading Code
« Reply #17 on: March 13, 2010, 07:49:59 AM »
fun_1
James Buzbee
Windows 8

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8786
  • AKA Daniel
Re: Reading Code
« Reply #18 on: March 13, 2010, 07:52:22 AM »
fun_2

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Reading Code
« Reply #19 on: March 13, 2010, 09:10:35 AM »
of the options posted fun_2

my real preference tho is subtly different:

Code: [Select]
(defun fun_3 ( l n / a b c d )
    (while (setq d l)
        (while (< (setq b (length a)) n)
            (setq
                a (cons (nth b d) a)
                l (cdr l)
            )
        )
        (setq
            c (cons (reverse a) c)
            a nil
        )
    )
    (reverse c)
)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

JohnK

  • Administrator
  • Seagull
  • Posts: 10658
Re: Reading Code
« Reply #20 on: March 13, 2010, 12:58:54 PM »
I can see you're point about the (re)formating.

My use of the `end-of-line' style developed when i started to get better with Vim and my studies of Scheme came about. I can "move around" better in most cases with the `end-of-line' style in Vim. Most if not all of scheme code is written in the `end-of-line' style.

If i take your two functions as posted; some days it can go either way but i wold say that fun_2 is easier to read more often than fun_1 because i have more experiences with the fun_2 style (posted, and learning).

When im developing apps on my own i use fun_1 style for several reasons. -e.g. i can either "fold up" regions of code quicker or i can ignore those regions like black-boxes.

So what are we getting at? Would we like to see only code posted in fun_2 type of style (It is the recommended style Autodesk recommends)?
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Reading Code
« Reply #21 on: March 15, 2010, 06:42:45 AM »
< .. >
So what are we getting at? Would we like to see only code posted in fun_2 type of style (It is the recommended style Autodesk recommends)?

Thanks John.
I've tried the endOfLineClosure formatting as well but found I had trouble breathing when reading ... from not having the the code blocks obvious ... particularly on larger files.
BUT, to each his own :)

I'm sure NOT trying to establish a forum standard ... just seeing how many people care enough to post about the issue.

 
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.