Re: Ensure path exists before using it

tpeplt <[email protected]>
Newsgroups gmane.emacs.help
Message-ID <[email protected]>
Heime <[email protected]> writes:

> I want to improve this bit of elisp code ensuring that bash-path 
> exists.
>
>     (let* ( ;; Relative subdirectory that doesn't start with / or ~
>             (subdir (or subdir "orellana"))
>
>             ;; Determine the base path: argument, directory, or env var
>             (base-path
>                 (or bspt
>                     (and (file-directory-p
>                            (substitute-in-file-name
>                              "$HOME/Opstk/src/hist-1.0/"))
>                          (substitute-in-file-name
>                            "$HOME/Opstk/src/hist-1.0/"))
>                     (expand-file-name "~/Opstk/src/hist-1.0/")
>                     (getenv "ORELLANA_PATH")))
>
> 	    ;; Construct the full path to feature
>             (ftpath (expand-file-name subdir base-path)) )
>

1. Start by writing an expression that has all information
   that it needs to compile without any errors or warnings.

   - Copy the code to a .el buffer
   - Add lexical binding: M-x e-e-l-b RET
   - Compile the file by, for example,
     Emacs menu -> Emacs-Lisp -> Byte-compile This File
     (there are other means to do this)
   - Resolve any warnings or errors

2. Your code calls ‘substitute-in-file-name’ twice, which
   can be improved on.  Add a new variable to your ‘let*’
   expression:
    (use-env (substitute-in-file-name "$HOME/Opstk/src/hist-1.0/"))

3. Using ‘and’ suggests that your code is using a boolean
   value (t or nil).  Use ‘when’ instead:
     (when (file-directory-p use-env) use-env)
   ‘when’ will return the value of ‘use-env’ when the
   condition is met, or return nil when it is not.

4. Your ‘let*’ expression does not have a body that either:
    - returns the value of a variable it set
   or
    - has some other expression(s) that uses the variable(s)

   For example, include a reference to ‘ftpath’ as the body
   of the ‘let*’ and that value will be returned when you
   evaluate the expression.

5. Instrument the code and evaluate the expression.  Step
   through the expression and look at the values returned as
   each expression is evaluated (this includes the values of
   evaluated variables) to confirm that each one evaluates
   to the value you expect.

-- 
The lyf so short, the craft so long to lerne.
- Geoffrey Chaucer, The Parliament of Birds.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.