all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Emacs Lisp coding style question
@ 2014-07-02 12:47 Thorsten Jolitz
  2014-07-02 14:19 ` Grant Rettke
  0 siblings, 1 reply; 7+ messages in thread
From: Thorsten Jolitz @ 2014-07-02 12:47 UTC (permalink / raw)
  To: help-gnu-emacs


Hi List, 

sometimes I wondered about the following coding style question, so I
decided to do it in public:

Often functions do a lot of work to gather some data and then do a
rather simple action with the gathered data:

#+begin_src emacs-lisp
  (defun foo ()
    (let* ((x (create-x))
          (y (create-y))
          (z (create-z x y))
          (u (create-u z))
          (v (create-v))
          (w (create-w u v)))
      (simple-action w)))
#+end_src

Thats the way I would do it, and I find it easy to write, read and
understand. 

But (without being able to give concrete examples right now) I noticed
that advanced Lispers tend to call this 'C-style', consider the let
bindings unnessesary since the local vars are only used once, and
prefer this style:

#+begin_src emacs-lisp
  (defun foo ()
    (simple-action
     (create-w
      (create-u
       (create-z (create-x) (create-y)))
      (create-v))))
#+end_src

This looks more 'lispy' and might have a slight performance
advantage. But when the 'create-xyz' expressions grow in size the
whole thing might start to look very complicated and it becomes hard to
recognize that its just about `simple-action' with some gathered
data. 

What would be the recommended style for Emacs Lisp, or is this just a
matter of taste?

-- 
cheers,
Thorsten




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Emacs Lisp coding style question
       [not found] <mailman.4706.1404305298.1147.help-gnu-emacs@gnu.org>
@ 2014-07-02 12:56 ` Pascal J. Bourguignon
  2014-07-02 13:14   ` Thorsten Jolitz
  0 siblings, 1 reply; 7+ messages in thread
From: Pascal J. Bourguignon @ 2014-07-02 12:56 UTC (permalink / raw)
  To: help-gnu-emacs

Thorsten Jolitz <tjolitz@gmail.com> writes:

> Hi List, 
>
> sometimes I wondered about the following coding style question, so I
> decided to do it in public:
>
> Often functions do a lot of work to gather some data and then do a
> rather simple action with the gathered data:
>
> #+begin_src emacs-lisp
>   (defun foo ()
>     (let* ((x (create-x))
>           (y (create-y))
>           (z (create-z x y))
>           (u (create-u z))
>           (v (create-v))
>           (w (create-w u v)))
>       (simple-action w)))
> #+end_src
>
> Thats the way I would do it, and I find it easy to write, read and
> understand. 
>
> But (without being able to give concrete examples right now) I noticed
> that advanced Lispers tend to call this 'C-style', consider the let
> bindings unnessesary since the local vars are only used once, and
> prefer this style:
>
> #+begin_src emacs-lisp
>   (defun foo ()
>     (simple-action
>      (create-w
>       (create-u
>        (create-z (create-x) (create-y)))
>       (create-v))))
> #+end_src
>
> This looks more 'lispy' and might have a slight performance
> advantage. But when the 'create-xyz' expressions grow in size the
> whole thing might start to look very complicated and it becomes hard to
> recognize that its just about `simple-action' with some gathered
> data. 
>
> What would be the recommended style for Emacs Lisp, or is this just a
> matter of taste?

Taste.

The later is generally better, since it avoids unnecessary (and
possibly misleading) temporary variable names.

Notice that both code might compile to the exact same binary, so there's
no efficiency advantage in either.

Also, if constructors names and signatures are well designed, the whole
subexpression:

      (w (u (z (x) (y))) (v))

can be considered more as data than as code (of course, there's no
difference between code and data, but I mean that when reading it, you
don't  read it as code, trying to understand a control flow and a data
flow, you just take it as a description of some structural data:

   (simple-action
     (vertically (square 10)
                 (circle 20 'red)
                 (horizontally (triangle 3 4 5 'green) (rectangle 20 10 'blue))))

There's no need to try to understand the control flow and the data flow
in the simple-action argument expression: you just understand it
directly as a geometrical description.

If you introduced here variables, it would make it much worse.

(On the other hand, there are often ill-designed APIs that force you to
name subcomponents of such structures, to further build them; this is
bad).

-- 
__Pascal Bourguignon__
http://www.informatimago.com/
"Le mercure monte ?  C'est le moment d'acheter !"


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Emacs Lisp coding style question
  2014-07-02 12:56 ` Emacs Lisp coding style question Pascal J. Bourguignon
@ 2014-07-02 13:14   ` Thorsten Jolitz
  2014-07-02 13:50     ` Stefan Monnier
       [not found]     ` <mailman.4715.1404309100.1147.help-gnu-emacs@gnu.org>
  0 siblings, 2 replies; 7+ messages in thread
From: Thorsten Jolitz @ 2014-07-02 13:14 UTC (permalink / raw)
  To: help-gnu-emacs

"Pascal J. Bourguignon" <pjb@informatimago.com> writes:

> Thorsten Jolitz <tjolitz@gmail.com> writes:
>
>> Hi List, 
>>
>> sometimes I wondered about the following coding style question, so I
>> decided to do it in public:
>>
>> Often functions do a lot of work to gather some data and then do a
>> rather simple action with the gathered data:
>>
>> #+begin_src emacs-lisp
>>   (defun foo ()
>>     (let* ((x (create-x))
>>           (y (create-y))
>>           (z (create-z x y))
>>           (u (create-u z))
>>           (v (create-v))
>>           (w (create-w u v)))
>>       (simple-action w)))
>> #+end_src
>>
>> Thats the way I would do it, and I find it easy to write, read and
>> understand. 
>>
>> But (without being able to give concrete examples right now) I noticed
>> that advanced Lispers tend to call this 'C-style', consider the let
>> bindings unnessesary since the local vars are only used once, and
>> prefer this style:
>>
>> #+begin_src emacs-lisp
>>   (defun foo ()
>>     (simple-action
>>      (create-w
>>       (create-u
>>        (create-z (create-x) (create-y)))
>>       (create-v))))
>> #+end_src
>>
>> This looks more 'lispy' and might have a slight performance
>> advantage. But when the 'create-xyz' expressions grow in size the
>> whole thing might start to look very complicated and it becomes hard to
>> recognize that its just about `simple-action' with some gathered
>> data. 
>>
>> What would be the recommended style for Emacs Lisp, or is this just a
>> matter of taste?
>
> Taste.

Ok

> The later is generally better, since it avoids unnecessary (and
> possibly misleading) temporary variable names.

I often find functions much easier to understand if they use good
temporary variable names and the one-liner in the function body almost
reads like plain English. 

> Notice that both code might compile to the exact same binary, so there's
> no efficiency advantage in either.

But in terms of uncompiled user-code - would the impact of the let
bindings here be worth thinking about performance?

> Also, if constructors names and signatures are well designed, the whole
> subexpression:
>
>       (w (u (z (x) (y))) (v))
>
> can be considered more as data than as code (of course, there's no
> difference between code and data, but I mean that when reading it, you
> don't  read it as code, trying to understand a control flow and a data
> flow, you just take it as a description of some structural data:
>
>    (simple-action
>      (vertically (square 10)
>                  (circle 20 'red)
>                  (horizontally (triangle 3 4 5 'green) (rectangle 20 10 'blue))))
>
> There's no need to try to understand the control flow and the data flow
> in the simple-action argument expression: you just understand it
> directly as a geometrical description.
>
> If you introduced here variables, it would make it much worse.

In this case I fully agree.

-- 
cheers,
Thorsten




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Emacs Lisp coding style question
  2014-07-02 13:14   ` Thorsten Jolitz
@ 2014-07-02 13:50     ` Stefan Monnier
  2014-07-02 14:04       ` Thorsten Jolitz
       [not found]     ` <mailman.4715.1404309100.1147.help-gnu-emacs@gnu.org>
  1 sibling, 1 reply; 7+ messages in thread
From: Stefan Monnier @ 2014-07-02 13:50 UTC (permalink / raw)
  To: help-gnu-emacs

>>> But (without being able to give concrete examples right now) I noticed
>>> that advanced Lispers tend to call this 'C-style', consider the let

I've never seen it referred to as "C-style".  To me "C-style" would be

   (let (a b c d e)
     (setq a (foo-a))
     (setq b (foo-b))
     ...)

>>> What would be the recommended style for Emacs Lisp, or is this just a
>>> matter of taste?

Mostly taste, and it depends on the specifics.  I.e. it depends on
whether the intermediate names can be useful as code documentation, and
indentation issues may also tip the balance between the two.

>> Notice that both code might compile to the exact same binary, so there's
>> no efficiency advantage in either.

The Emacs Lisp implementation (both interpreted and compiled) is not
sophisticated enough to get the same efficiency out of the let-binding
version, actually.

> But in terms of uncompiled user-code - would the impact of the let
> bindings here be worth thinking about performance?

No the difference should not be noticeable anyway.


        Stefan




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Emacs Lisp coding style question
  2014-07-02 13:50     ` Stefan Monnier
@ 2014-07-02 14:04       ` Thorsten Jolitz
  0 siblings, 0 replies; 7+ messages in thread
From: Thorsten Jolitz @ 2014-07-02 14:04 UTC (permalink / raw)
  To: help-gnu-emacs

Stefan Monnier <monnier@iro.umontreal.ca> writes:

>>>> But (without being able to give concrete examples right now) I noticed
>>>> that advanced Lispers tend to call this 'C-style', consider the let
>
> I've never seen it referred to as "C-style".  To me "C-style" would be
>
>    (let (a b c d e)
>      (setq a (foo-a))
>      (setq b (foo-b))
>      ...)

I cannot give a concrete url but I vaguely remember having seen such a
reference. 

>>>> What would be the recommended style for Emacs Lisp, or is this just a
>>>> matter of taste?
>
> Mostly taste, and it depends on the specifics.  I.e. it depends on
> whether the intermediate names can be useful as code documentation, and
> indentation issues may also tip the balance between the two.

Better indentation is probably one of the main reasons I find the (let
...) style easier to read in many cases.

>>> Notice that both code might compile to the exact same binary, so there's
>>> no efficiency advantage in either.
>
> The Emacs Lisp implementation (both interpreted and compiled) is not
> sophisticated enough to get the same efficiency out of the let-binding
> version, actually.
>
>> But in terms of uncompiled user-code - would the impact of the let
>> bindings here be worth thinking about performance?
>
> No the difference should not be noticeable anyway.

Ok, thanks, so there is no need to worry about this style-question, just
use what seems to be the better option for the problem at hand. 

-- 
cheers,
Thorsten




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Emacs Lisp coding style question
  2014-07-02 12:47 Thorsten Jolitz
@ 2014-07-02 14:19 ` Grant Rettke
  0 siblings, 0 replies; 7+ messages in thread
From: Grant Rettke @ 2014-07-02 14:19 UTC (permalink / raw)
  To: Thorsten Jolitz; +Cc: Emacs Help

let* is included for a reason and using it is definitely Lispy being
that it is important enough to have its own special form.

It indicates to the reader that the following computations must occur
on the specified order. That is why it exists because as you pointed
out there are other ways of achieving the same thing.

As previously stated, it is definitely a personal preference thing.
Grant Rettke | ACM, ASA, FSF, IEEE, SIAM
gcr@wisdomandwonder.com | http://www.wisdomandwonder.com/
“Wisdom begins in wonder.” --Socrates
((λ (x) (x x)) (λ (x) (x x)))
“Life has become immeasurably better since I have been forced to stop
taking it seriously.” --Thompson


On Wed, Jul 2, 2014 at 7:47 AM, Thorsten Jolitz <tjolitz@gmail.com> wrote:
>
> Hi List,
>
> sometimes I wondered about the following coding style question, so I
> decided to do it in public:
>
> Often functions do a lot of work to gather some data and then do a
> rather simple action with the gathered data:
>
> #+begin_src emacs-lisp
>   (defun foo ()
>     (let* ((x (create-x))
>           (y (create-y))
>           (z (create-z x y))
>           (u (create-u z))
>           (v (create-v))
>           (w (create-w u v)))
>       (simple-action w)))
> #+end_src
>
> Thats the way I would do it, and I find it easy to write, read and
> understand.
>
> But (without being able to give concrete examples right now) I noticed
> that advanced Lispers tend to call this 'C-style', consider the let
> bindings unnessesary since the local vars are only used once, and
> prefer this style:
>
> #+begin_src emacs-lisp
>   (defun foo ()
>     (simple-action
>      (create-w
>       (create-u
>        (create-z (create-x) (create-y)))
>       (create-v))))
> #+end_src
>
> This looks more 'lispy' and might have a slight performance
> advantage. But when the 'create-xyz' expressions grow in size the
> whole thing might start to look very complicated and it becomes hard to
> recognize that its just about `simple-action' with some gathered
> data.
>
> What would be the recommended style for Emacs Lisp, or is this just a
> matter of taste?
>
> --
> cheers,
> Thorsten
>
>



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Emacs Lisp coding style question
       [not found]     ` <mailman.4715.1404309100.1147.help-gnu-emacs@gnu.org>
@ 2014-07-02 15:20       ` Barry Margolin
  0 siblings, 0 replies; 7+ messages in thread
From: Barry Margolin @ 2014-07-02 15:20 UTC (permalink / raw)
  To: help-gnu-emacs

In article <mailman.4715.1404309100.1147.help-gnu-emacs@gnu.org>,
 Stefan Monnier <monnier@iro.umontreal.ca> wrote:

> >>> But (without being able to give concrete examples right now) I noticed
> >>> that advanced Lispers tend to call this 'C-style', consider the let
> 
> I've never seen it referred to as "C-style".  To me "C-style" would be
> 
>    (let (a b c d e)
>      (setq a (foo-a))
>      (setq b (foo-b))
>      ...)
> 
> >>> What would be the recommended style for Emacs Lisp, or is this just a
> >>> matter of taste?
> 
> Mostly taste, and it depends on the specifics.  I.e. it depends on
> whether the intermediate names can be useful as code documentation, and
> indentation issues may also tip the balance between the two.
> 
> >> Notice that both code might compile to the exact same binary, so there's
> >> no efficiency advantage in either.
> 
> The Emacs Lisp implementation (both interpreted and compiled) is not
> sophisticated enough to get the same efficiency out of the let-binding
> version, actually.

Root of all evil. Unless this code is in a heavily used inner loop, it 
probably won't make any significant difference.

-- 
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2014-07-02 15:20 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <mailman.4706.1404305298.1147.help-gnu-emacs@gnu.org>
2014-07-02 12:56 ` Emacs Lisp coding style question Pascal J. Bourguignon
2014-07-02 13:14   ` Thorsten Jolitz
2014-07-02 13:50     ` Stefan Monnier
2014-07-02 14:04       ` Thorsten Jolitz
     [not found]     ` <mailman.4715.1404309100.1147.help-gnu-emacs@gnu.org>
2014-07-02 15:20       ` Barry Margolin
2014-07-02 12:47 Thorsten Jolitz
2014-07-02 14:19 ` Grant Rettke

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.