~evhan/beaker#6: 
Potential lint: if-begin into cond

Hey evhan,

Just wanted to suggest a potential lint that could be added to chicken-lint, since I found myself going through some of my code and applying it myself.

Oftentimes there will be a pattern where one might do the following:

(if condition
   (begin
     (set! ...)
     value)
   sentinel)

I've noticed it a fair amount in the generalized-arrays egg I've been rewriting, and ended up deciding that it's somewhat gross to write stuff this way. It adds to weird indentation and it's easy to mess up if you're used to one-armed if. Instead, an appropriate fix to this (what the lint should suggest):

(cond
   (condition
     (set! ...)
     value)
   (else
     sentinel))

This costs some indentation but the alignment of the code (and the existing lint that will warn you of a missing-else) means that it is very hard to do the wrong thing here.

Anyways, it probably isn't necessary to add this if you don't agree with it, but I've come to really appreciate replacing if-begin (in either branch!) with a cond.

#Regards,

Jeremy Steward

Status
REPORTED
Submitter
Jeremy Steward
Assigned to
Submitted
1 year, 2 months ago
Updated
7 months ago
Labels
No labels applied.

~evhan 1 year, 2 months ago

Hey Jeremy, thanks for the suggestion! That's not a bad idea, and it shouldn't be too complex to implement, either.

Jeremy Steward 1 year, 2 months ago · edit

Fantastic!

As I was just thinking about it more, I have some other (basic) suggestions for lints as well if you're open to them:

(if condition
   #t
   #f)

The #t / #f literals in either branch of the if can probably be replaced by the condition itself, or some variant (e.g. (not condition)).

Also:

(if condition-1
   (if condition-2
     a
     b)
   c)

Nested ifs are probably best turned into a cond as well as an (and condition-1 condition-2):

(cond
   ((and condition-1 condition-2) a)
   (condition-1 b)
   (else c))

I suppose that experienced Schemers probably won't fall for these, but giving the linter good defaults for this sort of behaviour would be ideal :)

Thanks again for anything you can add here, I really appreciate the tool!

On 11/13/23 23:50, ~evhan wrote:

Hey Jeremy, thanks for the suggestion! That's not a bad idea, and it shouldn't be too complex to implement, either.

-- Jeremy Steward

~evhan 7 months ago

I've finally gotten around to implementing this, and version 0.0.23 will include a lint for that if-begin pattern.

I'll leave this ticket open for now, until the other suggestions are implemented or moved to their own issues.

Register here or Log in to comment, or comment via email.