Permalink
Browse files

pedantic-if

IFs with no else clause or a NIL else clause should always be replaced
with a WHEN.

Here we shadow the standard IF with our own that warns when we violate
these style rules.

> (defun foo (a) (if (eq a :foo) :baz))
; in: DEFUN FOO
;     (ACADEMY::IF (EQ ACADEMY::A :FOO) :BAZ)
;
; caught WARNING:
;   A single legged IF should be replaced with a WHEN.
;
; compilation unit finished
;   caught 1 WARNING condition
FOO

> (foo :foo)
:BAZ

> (foo 123)
NIL

> (if :foo :baz nil)
WARNING: An if's else of NIL should be replaced with a WHEN.

This commit demonstrates

  * Shadowing core CL functionality with our own cooked up versions.
  * Using the optional SUPPLIED-P-PARAMETER to distinguish between a
    supplied NIL and no argument.
  • Loading branch information...
whalliburton committed Dec 14, 2011
1 parent dde183b commit 1a93c881b14781fae5a64bde2eab7c7119ee80b4
Showing with 9 additions and 0 deletions.
  1. +1 −0 academy.asd
  2. +1 −0 packages.lisp
  3. +7 −0 pedantic-if.lisp
View
@@ -2,6 +2,7 @@
:serial t
:components ((:static-file "academy.asd")
(:file "packages")
+ (:file "pedantic-if")
(:file "help")
(:file "classic-utilities")
(:file "debugging")
View
@@ -1,5 +1,6 @@
(defpackage academy
(:use common-lisp)
+ (:shadow if)
(:export help
petals-around-the-rose
hilbert-space-filling-curve
View
@@ -0,0 +1,7 @@
+(in-package :academy)
+
+(defmacro if (test then &optional (else nil else-defined))
+ (unless else-defined (warn "A single legged IF should be replaced with a WHEN."))
+ (when (and else-defined (null else))
+ (warn "An if's else of NIL should be replaced with a WHEN."))
+ `(cl:if ,test ,then ,else))

0 comments on commit 1a93c88

Please sign in to comment.