From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Drew Adams Newsgroups: gmane.emacs.help Subject: RE: if vs. when vs. and: style question Date: Mon, 23 Mar 2015 17:22:59 -0700 (PDT) Message-ID: References: <87sicvwckx.fsf@wmi.amu.edu.pl> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Trace: ger.gmane.org 1427156621 23831 80.91.229.3 (24 Mar 2015 00:23:41 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 24 Mar 2015 00:23:41 +0000 (UTC) To: Marcin Borkowski , Help Gnu Emacs mailing list Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Tue Mar 24 01:23:28 2015 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1YaCcq-0005EJ-O7 for geh-help-gnu-emacs@m.gmane.org; Tue, 24 Mar 2015 01:23:20 +0100 Original-Received: from localhost ([::1]:58537 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YaCcp-0001X0-Pg for geh-help-gnu-emacs@m.gmane.org; Mon, 23 Mar 2015 20:23:19 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:49658) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YaCcf-0001W2-59 for help-gnu-emacs@gnu.org; Mon, 23 Mar 2015 20:23:10 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1YaCcZ-0004Pr-3f for help-gnu-emacs@gnu.org; Mon, 23 Mar 2015 20:23:09 -0400 Original-Received: from userp1040.oracle.com ([156.151.31.81]:50748) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YaCcY-0004Ov-Sv for help-gnu-emacs@gnu.org; Mon, 23 Mar 2015 20:23:03 -0400 Original-Received: from userv0021.oracle.com (userv0021.oracle.com [156.151.31.71]) by userp1040.oracle.com (Sentrion-MTA-4.3.2/Sentrion-MTA-4.3.2) with ESMTP id t2O0N0Ft027401 (version=TLSv1 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Tue, 24 Mar 2015 00:23:00 GMT Original-Received: from userv0121.oracle.com (userv0121.oracle.com [156.151.31.72]) by userv0021.oracle.com (8.13.8/8.13.8) with ESMTP id t2O0Mxm1023880 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=FAIL); Tue, 24 Mar 2015 00:23:00 GMT Original-Received: from abhmp0017.oracle.com (abhmp0017.oracle.com [141.146.116.23]) by userv0121.oracle.com (8.13.8/8.13.8) with ESMTP id t2O0MxoR028178; Tue, 24 Mar 2015 00:22:59 GMT In-Reply-To: <87sicvwckx.fsf@wmi.amu.edu.pl> X-Priority: 3 X-Mailer: Oracle Beehive Extensions for Outlook 2.0.1.8.2 (807160) [OL 12.0.6691.5000 (x86)] X-Source-IP: userv0021.oracle.com [156.151.31.71] X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] X-Received-From: 156.151.31.81 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:103276 Archived-At: > assuming (e1) and (e2) are some expressions, all three forms: > (if (e1) (e2)) > (when (e1) (e2)) > (and (e1) (e2)) Plus `cond', `unless', `or', and `not'. > are semantically equivalent. Which one is better style (and when)? > I would guess that =3Dwhen=3D is better iff (e2) is a =3Dprogn=3D (since = we can > drop the =3Dprogn=3D altogether, and this seems to be the point of =3Dwhe= n=3D), > and =3Dand=3D might be considered better (well, maybe) by some people whe= n > both (e1) and (e2) are very short (though I personally would avoid that, > since =3Dif=3D seems easier for a human to understand at a first glance). > Am I right? >=20 > Notice: by =E2=80=9Cbetter=E2=80=9D I mean =E2=80=9Cmore idiomatic=E2=80= =9D, or =E2=80=9Ceasier/faster to read > for a human=E2=80=9D, or =E2=80=9Cmore likely to be used by an experience= d Elisp > hacker=E2=80=9D, etc. FWIW - I use a convention that you will find mentioned in "Common Lisp The Language" (1 & 2): I use `if', `and', and `or' when the return value is significant (i.e., it is used; it makes a difference to the behavior). I use `when' and `unless' _only_ when the return value is not significant (not used). IOW, they signify side effects only, when I read them (in code I wrote). --- Beyond that - I use `cond' in both contexts (whether or not the return value is used). I use `if' _only_ for the if-then-else case (3 args, not 2). For the if-then case (only two args) I use `and' or `or' (if the return value is significant) or `when' or `unless' (if the return value is not used). For `(if test nil val)' I typically use `(and (not test) val)'. But sometimes I use (if test nil other-val), if I really want to point out the nil return possibility (esp. if I want to put a comment on its line). Similarly, for `(if test val nil)' I typically use `(and test val)'. I typically use `(cond (test foo1 foo2...) (t bar1 bar2...))' instead of `(if test (progn foo1 foo2...) bar1 bar2...)'. I typically use `(if test val sexp1 sexp2...)' instead of `(cond (test val) (t sexp1 sexp2...))'. But sometimes I don't. ;-) And yes, I typically use a single `setq' for sequential assignments. And that means that I can use `(if test (setq...) val)' instead of `(if test (progn (setq...) (setq...)...) val)'. Because I use a single `setq' for multiple assignments, seeing two `setq's "next to" each other makes clear that they are at different levels; they are not siblings. E.g. (setq...)) (setq...) In a `setq' with multiple assignments (and in a `let' with multiple bindings) I typically align the values too, not just the variables: (setq toto kelp bar rhinoceros whoops nematode) --- I also use `()' instead of nil when it represents the empty list. And I use an explicit initialization to `nil' or `()' in a `let', if it is a true initialization. That is, I put a variable in a `let' without any initial value _only_ if its initial value is assigned in the `let' body, i.e., without making any use of its implicit initial nil value. =20 And I put any variables that have no initial binding at the end of the list of bindings (not at the beginning, and not in between). And yes, these conventions mean that I have to rewrite code from one form to another as it evolves. Not a big deal, to me. Reading code is more important, to me. I make no claims about others sharing such conventions.