Author Topic: Question 37  (Read 8950 times)

0 Members and 1 Guest are viewing this topic.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Question 37
« on: October 25, 2005, 04:29:29 PM »
Question for the LISPERS.

Suppose you needed to pen a couple padding functions that take a string and right or left pad it with n specified characters.

You might code it like this (naming after antiquated basic equivalents):

RSET:

Code: [Select]
(defun rset ( text padding maxlen )
    (substr
        (   (lambda ( )
                (while 
                    (<
                        (strlen (setq padding (strcat padding padding)))
                        maxlen
                    )
                )
                (setq text (strcat padding text))                               
            )
        )
        (- (strlen text) (1- maxlen))
    )
)

Example:

Code: [Select]
(rset "text" " " 10)

==> "      text"

LSET:

Code: [Select]
(defun lset ( text padding maxlen )
    (substr
        (   (lambda ( )
                (while 
                    (<
                        (strlen (setq padding (strcat padding padding)))
                        maxlen
                    )
                )
                (strcat text padding)
            )
        )
        1
        maxlen
    )
)

Example:

Code: [Select]
(lset "text" " " 10)

==> "text      "

Then you notice there is duplicated logic, so you distil the functions by writing an auxiliary function:

SETAUX:

Code: [Select]
(defun setaux ( padding maxlen )
    (while 
        (<
            (strlen (setq padding (strcat padding padding)))
            maxlen
        )
    )
    padding
)

RSET:

Code: [Select]
(defun rset ( text padding maxlen )
    (substr
        (setq text
            (strcat
                (setaux padding maxlen)
                text
            )
        )                               
        (- (strlen text) (1- maxlen))
    )
)

LSET:

Code: [Select]
(defun lset ( text padding maxlen )
    (substr
        (strcat text (setaux padding maxlen))
        1
        maxlen
    )
)

You're thinking it looks pretty good but then you realize, at least theoretically, that it demonstrates poor coding practice.
 
What poor coding practice do the rewritten functions demonstrate?

What would that practice be called?

On the other hand you realize they also demonstrate good coding practice.

What good coding practice do the rewritten functions demonstrate?

What would that practice be called?

Kerry, please hold off responding to this one, at least for a bit.

:)
« Last Edit: October 25, 2005, 05:00:06 PM by MP »
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Question 37
« Reply #1 on: October 25, 2005, 05:07:12 PM »
Time to exercise the wetware < the stuff between the ears>
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.

whdjr

  • Guest
Re: Question 37
« Reply #2 on: October 26, 2005, 10:11:33 AM »
Question for the LISPERS.

Suppose you needed to pen a couple padding functions that take a string and right or left pad it with n specified characters.

You might code it like this (naming after antiquated basic equivalents):
Quote from: MP
What poor coding practice do the rewritten functions demonstrate?

Answer:  Haven't figured this one out yet.  Maybe doing more than one thing in a function.

Quote from: MP
What good coding practice do the rewritten functions demonstrate?

Answer:  You have one function doing one thing.

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Re: Question 37
« Reply #3 on: October 26, 2005, 10:43:42 AM »
Hint: the word 'padding'
TheSwamp.org  (serving the CAD community since 2003)

Chuck Gabriel

  • Guest
Re: Question 37
« Reply #4 on: October 26, 2005, 11:26:45 AM »
I would say that it is good that you used named abstractions and bad that the names chosen aren't particulary expressive.

Some people would say that it is bad that your functions have side effects (i.e. they modify their arguments).

whdjr

  • Guest
Re: Question 37
« Reply #5 on: October 26, 2005, 01:50:44 PM »
I did think it was kind of odd how 'lset' added characters to the right side and 'rset' added characters to the left side. :-o :|

It must be a Canadian thing! :lol:

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Question 37
« Reply #6 on: October 26, 2005, 01:57:07 PM »
I did think it was kind of odd how 'lset' added characters to the right side and 'rset' added characters to the left side. :-o :|

It must be a Canadian thing! :lol:

Perhaps the vb/vba help was authored by a canuck!

Nudge: Read up on lset/rest vb/vba functions.

:)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Question 37
« Reply #7 on: October 26, 2005, 01:58:09 PM »
I would say that it is good that you used named abstractions and bad that the names chosen aren't particulary expressive.

Some people would say that it is bad that your functions have side effects (i.e. they modify their arguments).

Agree on both counts, particularly the latter.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Question 37
« Reply #8 on: October 26, 2005, 01:59:13 PM »
Answer: You have one function doing one thing.

Usually that's a very good thing in my book.

Why might it not be?

:)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Question 37
« Reply #9 on: October 26, 2005, 02:13:01 PM »
Quote
Why might it not be?

passing an abundance of variables back and forwards can be unkind to your performance.

It's interesting that the OOP concepts of abstraction and obfuscation have snuck in.
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.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Question 37
« Reply #10 on: October 26, 2005, 02:18:06 PM »
... and while not generally a lisper's concern, if considering software architecture as a science -- what about cohesion / coupling issues?
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

LE

  • Guest
Re: Question 37
« Reply #11 on: October 26, 2005, 05:37:47 PM »
As I mentioned, I am not a programmer....

But I would use for the auxiliary function something like this:

Code: [Select]
(defun setaux  (padding maxlen / str)
  (setq str "")
  (repeat maxlen (setq str (strcat str padding)))
  str)

And now that heard today about turbo pascal - not here.... here is a translation of ins v1.5 (insert)

Code: [Select]
(defun ins  (src tgt inx)
  (if (/= inx 0)
    (strcat (substr tgt 1 (1- inx)) src (substr tgt inx))
    (eval tgt)))

(defun ins_str (src tgt inx)
  (if (/= inx 0)
    (strcat (substr tgt 1 (1- inx)) src (substr tgt inx))
    (eval tgt)))

(defun pos_str (subcad cad / cnt)
  (setq cnt 1)
  (while
    (and (/= subcad (substr cad cnt (strlen subcad)))
(< cnt (strlen cad)))
     (setq cnt (1+ cnt)))
  (if (= subcad (substr cad cnt (strlen subcad)))
    (eval cnt)
    (eval 0)))

And the answer is................................................ ?

LE

  • Guest
Re: Question 37
« Reply #12 on: October 26, 2005, 05:40:47 PM »
and also would avoid the re-write of var "text"

Code: [Select]
(defun rset ( text padding maxlen )
    (substr
        (setq text
            (strcat
                (setaux padding maxlen)
                text
            )
        )                               
        (- (strlen text) (1- maxlen))
    )
)

I never had changed the value of an argument on my functions.... as far I remember....

Chuck Gabriel

  • Guest
Re: Question 37
« Reply #13 on: October 26, 2005, 05:46:23 PM »
...
I never had changed the value of an argument on my functions.... as far I remember....

I try to avoid it in lisp programs, but there was a time when I considered anything fair game as long as it got the job done.

However, it is a very common paradigm in some languages, particularly VB(A).  As long as it is clearly documented, I don't see a problem with it.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Question 37
« Reply #14 on: October 26, 2005, 05:50:33 PM »
As I mentioned, I am not a programmer....
 ...

Luis this may lose something in translation ..

I was raised on a farm. One of our neighbours was an Irishman who has a good turn of phrase < and a lovely daughter >.
2 of his expressions have stayed in my menory.

I was always in a hurry, and he'd say to me "Take your hurry, boy."

The other expression is fairly common.
"Boy, If it quacks like a duck, chances are it waddles"  taken by me to mean that things are what they are, irrespective of what we call them.

As I mentioned, I am not a programmer....
 ...

Perhaps you mean you are not formally trained. .. I'm not, and few of us are.

QUACK.  


« Last Edit: October 26, 2005, 05:53:56 PM by Kerry Brown »
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.

Chuck Gabriel

  • Guest
Re: Question 37
« Reply #15 on: October 26, 2005, 05:54:30 PM »
Yeah.  You won't find the word Bozo anywhere in my resume, but it doesn't change the facts. :D

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Question 37
« Reply #16 on: October 26, 2005, 05:56:37 PM »
hehehehe,
now, THAT'S funny ..
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.

LE

  • Guest
Re: Question 37
« Reply #17 on: October 26, 2005, 05:58:58 PM »
Perhaps you mean you are not formally trained. .. I'm not, and few of us are.
QUACK.  

 :lmao:

LE

  • Guest
Re: Question 37
« Reply #18 on: October 26, 2005, 06:01:39 PM »
Yeah.  You won't find the word Bozo anywhere in my resume, but it doesn't change the facts. :D

I am reading my resume... right now.... just to make sure.....

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Question 37
« Reply #19 on: October 26, 2005, 06:46:33 PM »
and also would avoid the re-write of var "text"

I never had changed the value of an argument on my functions.... as far I remember....


Not sure I understand (battling the flu)
But the 'text' var in the subroutine is a local var & has no effect on the 'text' var in the calling routine.
So why would you avoid change the argument?
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.

LE

  • Guest
Re: Question 37
« Reply #20 on: October 26, 2005, 06:52:06 PM »
Code: [Select]
(defun rset ( text padding maxlen )
    (substr
        (setq text
            (strcat
                (setaux padding maxlen)
                text
            )
        )                               
        (- (strlen text) (1- maxlen))
    )
)

Here..... get well!

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Question 37
« Reply #21 on: October 26, 2005, 07:19:18 PM »
Quote
So why would you avoid change the argument?

From a purist perspective, which doesn't always seem relevant with Lisp ...

Variable typing and naming is generally considered critical to computer language debugging.

.. but debugging is more than just finding errors after they present themselves, it's also a proactive process to test the flow and integrity of your routine.

One mechanisn for doing this involves the investigation of variable values and testing them against preproven values.

A more consistant strategy for doing this can be determined if the variable values are assigned in more than a seemingly whimsical manner. < sorry, thats my sense of humor taking over>   

 

[added: Does that sound like , as jonesy would say, pretentious nonsense ]
« Last Edit: October 26, 2005, 07:23:45 PM by Kerry Brown »
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.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Question 37
« Reply #22 on: October 26, 2005, 07:59:12 PM »
[added: Does that sound like , as jonesy would say, pretentious nonsense ]

Yes, but I see your point. :-D
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.

LE

  • Guest
Re: Question 37
« Reply #23 on: October 27, 2005, 09:28:16 PM »
Question for the LISPERS.


No solution .... still ?

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Question 37
« Reply #24 on: October 30, 2005, 08:03:49 AM »
Kerry
How ironic, I was just doing some modifications to my TextInsert Routine & that very thing
bit me in the butt. he he he :-)
Looking back through my older code I sure need to re write most of it.
Who has the time to go back though, until it breaks.
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.