From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: "Pascal J. Bourguignon" Newsgroups: gmane.emacs.help Subject: Re: if vs. when vs. and: style question Date: Tue, 24 Mar 2015 01:15:06 +0100 Organization: Informatimago Message-ID: <87mw3345f9.fsf@kuiper.lan.informatimago.com> 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: 8bit X-Trace: ger.gmane.org 1427156640 24305 80.91.229.3 (24 Mar 2015 00:24:00 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Tue, 24 Mar 2015 00:24:00 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Tue Mar 24 01:23:48 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 1YaCdG-0005Tw-Bg for geh-help-gnu-emacs@m.gmane.org; Tue, 24 Mar 2015 01:23:46 +0100 Original-Received: from localhost ([::1]:58540 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YaCdF-0001zG-Pp for geh-help-gnu-emacs@m.gmane.org; Mon, 23 Mar 2015 20:23:45 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:49688) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YaCcp-0001iQ-Vf for help-gnu-emacs@gnu.org; Mon, 23 Mar 2015 20:23:21 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1YaCck-0004Xb-To for help-gnu-emacs@gnu.org; Mon, 23 Mar 2015 20:23:19 -0400 Original-Received: from plane.gmane.org ([80.91.229.3]:36659) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YaCck-0004X7-Nn for help-gnu-emacs@gnu.org; Mon, 23 Mar 2015 20:23:14 -0400 Original-Received: from list by plane.gmane.org with local (Exim 4.69) (envelope-from ) id 1YaCcj-0005BQ-1W for help-gnu-emacs@gnu.org; Tue, 24 Mar 2015 01:23:13 +0100 Original-Received: from amontsouris-654-1-210-49.w82-123.abo.wanadoo.fr ([82.123.121.49]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Tue, 24 Mar 2015 01:23:13 +0100 Original-Received: from pjb by amontsouris-654-1-210-49.w82-123.abo.wanadoo.fr with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Tue, 24 Mar 2015 01:23:13 +0100 X-Injected-Via-Gmane: http://gmane.org/ Original-Lines: 111 Original-X-Complaints-To: usenet@ger.gmane.org X-Gmane-NNTP-Posting-Host: amontsouris-654-1-210-49.w82-123.abo.wanadoo.fr Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwAQMAAABtzGvEAAAABlBMVEUAAAD///+l2Z/dAAAA oElEQVR4nK3OsRHCMAwF0O8YQufUNIQRGIAja9CxSA55AxZgFO4coMgYrEDDQZWPIlNAjwq9 033pbOBPtbXuB6PKNBn5gZkhGa86Z4x2wE67O+06WxGD/HCOGR0deY3f9Ijwwt7rNGNf6Oac l/GuZTF1wFGKiYYHKSFAkjIo1b6sCYS1sVmFhhhahKQssRjRT90ITWUk6vvK3RsPGs+M1RuR mV+hO/VvFAAAAABJRU5ErkJggg== X-Accept-Language: fr, es, en User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) Cancel-Lock: sha1:ZjUyMmU0ZDZhMTRkZGE3NGJlM2JjNWYwYjk5YjI3ZTA5YWM4NjhhZA== X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 80.91.229.3 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:103277 Archived-At: Marcin Borkowski writes: > Hi there, > > assuming (e1) and (e2) are some expressions, all three forms: > > (if (e1) (e2)) > > (when (e1) (e2)) > > (and (e1) (e2)) > > are semantically equivalent. Which one is better style (and when)? > I would guess that =when= is better iff (e2) is a =progn= (since we can > drop the =progn= altogether, and this seems to be the point of =when=), > and =and= might be considered better (well, maybe) by some people when > both (e1) and (e2) are very short (though I personally would avoid that, > since =if= seems easier for a human to understand at a first glance). > Am I right? In general, WHEN is better than IF. Also, (unless (e1) (e2)) is better than (if (not (e1)) (e2)). Notably, WHEN and UNLESS have an implicit progn: (when (e1) (e21) (e22) … (e2n)) I use IF only in ternary form: (if (etest) (ethen) (eelse)) AND is better when the result is boolean (and any result is always a generalized boolean, right?). I would use WHEN rather than AND in general, with a preference for WHEN for procedural forms, and AND for resulting expressions: (defun get-it (key) (when (full-moon-p) (error "full moon")) (when (invalid-key-p key) (error "invalid key")) (and (exist-key-p key) (find-key key))) Notably if you consider OR, it's quite idiomatic to use it even for non-boolean (and notice that in CL, AND and OR return multiple values too): So you can typically have functions such as: (defun find-thingy (name) (or (find-thingy-in-whatcha name) (find-thingy-in-macall name) (find-thingy-in-it name) (error "not found"))) which is much more concise and clear than: (let ((thingy-found-in-whatcha (find-thingy-in-whatcha name))) (if thingy-found-in-whatcha thingy-found-in-whatcha (let ((thingy-found-in-macall (find-thingy-in-macall name))) (if thingy-found-in-macall thingy-found-in-macall (let ((thingy-found-in-it (find-thingy-in-macall name))) (if thingy-found-in-it thingy-found-in-it (error "not found"))))))) So much so that if I had to wrap those steps in other expressions, I would write a macro similar to OR to do it and keep the find-thingy function that simple. So by analogy, using AND can be justified: (defun find-thingy (name) (and (valid-name-p name name) (exists-thingy-named name) (find-thing-or-create name))) compared to the less concise: (defun find-thingy (name) (when (valid-name-p name name) (when (exists-thingy-named name) (find-thing-or-create name)))) or (defun find-thingy (name) (when (and (valid-name-p name name) (exists-thingy-named name)) (find-thing-or-create name))) -- __Pascal Bourguignon__ http://www.informatimago.com/ “The factory of the future will have only two employees, a man and a dog. The man will be there to feed the dog. The dog will be there to keep the man from touching the equipment.” -- Carl Bass CEO Autodesk