From mboxrd@z Thu Jan 1 00:00:00 1970 From: Eric Schulte Subject: Re: Elisp programming style Date: Fri, 28 Oct 2011 08:35:37 -0600 Message-ID: <87fwiduqti.fsf@gmail.com> References: <86obx2gvmd.fsf@googlemail.com> <8762j94dz0.fsf@gmail.com> <86obx1qkcc.fsf@googlemail.com> Mime-Version: 1.0 Content-Type: text/plain Return-path: Received: from eggs.gnu.org ([140.186.70.92]:44645) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RJnX6-0000p8-Ua for emacs-orgmode@gnu.org; Fri, 28 Oct 2011 10:35:46 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1RJnX4-00063r-S7 for emacs-orgmode@gnu.org; Fri, 28 Oct 2011 10:35:44 -0400 Received: from mail-pz0-f47.google.com ([209.85.210.47]:53221) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RJnX4-00063f-IT for emacs-orgmode@gnu.org; Fri, 28 Oct 2011 10:35:42 -0400 Received: by pzd13 with SMTP id 13so10582208pzd.6 for ; Fri, 28 Oct 2011 07:35:41 -0700 (PDT) In-Reply-To: <86obx1qkcc.fsf@googlemail.com> (Thorsten's message of "Fri, 28 Oct 2011 16:09:07 +0200") List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: Thorsten Cc: emacs-orgmode@gnu.org > > The problem can be described easily: > > problem-specific helper-funcions (some redundancy avoided) > ,----------------------------------------------------------- > | (defun main-function (args) > | (let ((var (assoc :key1 args))) ; extracting var once > | ... > | (helper-function1 ...) ; inside let using var > | (helper-function2 ...) ; inside let using var > | )) > | > | (defun helper-function1 () > | ... > | ) > | > | (defun helper-function2 () > | ... > | ) > `----------------------------------------------------------- > > vs > > standalone helper-functions (but redundancy) > ,------------------------------------------------------------- > | (defun main-function (args) > | (let ((value (assoc :key1 args)) ; extracting var 1st time > | ... > | ) > | (helper-function1 ...) ; outside let > | (helper-function2 ...) ; outside let > | ) > | > | (defun helper-function1 (args) > | (let ((var (assoc :key1 args))) ; extracting var 2nd time > | ... > | )) > | > | (defun helper-function2 (args) > | (let ((var (assoc :key1 args))) ; extracting var 3rd time > | ... > | )) > `------------------------------------------------------------- > Hmmm, this looks suspiciously like the case in some Babel functions :) in which we originally has instances of the first style and then had to manually transition to the second. IMO the first is very poor form, the variables are technically "free variables" when defined in the helper, and just through undocumented variable capture does the execution work out correctly, this can lead to unpleasant bugs. The means the helper may only be used when variables of certain names are in scope. If you do want to use a helper which uses variables in scope which aren't passed as arguments (again just my opinion) you should defined the helper function using `flet' *inside* of the main function and the scope of the variables. > > >> In general, if the helper function is only used in a single place and >> isn't likely to be more generally useful, there might be no reason for >> it to be a standalone function at all (other than modular programming, >> which is always a good idea IMO). >> >> OTOH, if the helper function _is_ standalone and used in multiple >> places, then there is no "redundancy", so I'm really not sure what you >> mean. > > Is there a policy? Lets say, an org-library author thinks his helper > functions will be of no use for nobody else and designs them > non-standalone. A few years later somebody wants to write a similar > library and trys to reuse some of the old helper functions, to no avail. > > Would that be considered bad style from the original author, or is that > up to personal choice and not considered a problem? > I don't believe we have an official canon of such rules, but personally I would consider this to be bad style. If the helper function is only used in one main function then it should be defined using flet, if it is used across multiple functions then the in-scope variables should be passed as explicit arguments (preferably with names other than those which it has in scope so you can be sure you are actually using the arguments). Finally, I believe the emacs-lisp compiler would complain about such free variables. Hope this helps, Best -- Eric > > cheers -- Eric Schulte http://cs.unm.edu/~eschulte/