From mboxrd@z Thu Jan 1 00:00:00 1970 From: Thorsten Subject: Re: Elisp programming style Date: Fri, 28 Oct 2011 16:09:07 +0200 Message-ID: <86obx1qkcc.fsf@googlemail.com> References: <86obx2gvmd.fsf@googlemail.com> <8762j94dz0.fsf@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Return-path: Received: from eggs.gnu.org ([140.186.70.92]:58753) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RJn7e-0001Ng-IP for emacs-orgmode@gnu.org; Fri, 28 Oct 2011 10:09:27 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1RJn7a-000754-62 for emacs-orgmode@gnu.org; Fri, 28 Oct 2011 10:09:26 -0400 Received: from lo.gmane.org ([80.91.229.12]:45157) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RJn7Z-00074q-KU for emacs-orgmode@gnu.org; Fri, 28 Oct 2011 10:09:22 -0400 Received: from list by lo.gmane.org with local (Exim 4.69) (envelope-from ) id 1RJn7X-0003Eg-Pp for emacs-orgmode@gnu.org; Fri, 28 Oct 2011 16:09:19 +0200 Received: from g231224198.adsl.alicedsl.de ([92.231.224.198]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Fri, 28 Oct 2011 16:09:19 +0200 Received: from quintfall by g231224198.adsl.alicedsl.de with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Fri, 28 Oct 2011 16:09:19 +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: emacs-orgmode@gnu.org Štěpán Němec writes: > On Thu, 27 Oct 2011 20:03:22 +0200 > Thorsten wrote: > >> Hi List, doing some Elisp programming (in an orgmode context) >> recently, the following question with regards to the 'accepted >> programming style' for Elisp concerned me: >> >> How independent and self-sustained should helper functions be? >> >> I found some redundancy in elisp code, e.g. several (main and helper) >> functions that do exactly the same thing to extract the same specific >> args out of an argument list. My first reaction was, to factor out >> this extraction into the main function, and then call the helper >> functions from inside a (let ...) environment where the extracted >> args are stored in a local variable. >> >> But then I recognised, that the helper functions cannot be called >> independently anymore, but only make sense when called from this one >> main function with its local bindings. >> >> Is there a kind of convention in a case like this? Like: "Make every >> function, even a helper function, independent, and don't care about >> redundancy"? Just being curious > > Too bad you didn't give any concrete examples. 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 | ... | )) `------------------------------------------------------------- > 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? cheers -- Thorsten