resyntax icon indicating copy to clipboard operation
resyntax copied to clipboard

Add simplify-named-let-initialization refactoring rule

Open Copilot opened this issue 3 months ago • 0 comments

Adds a refactoring rule to extract complex multi-line initialization expressions from named let loops into separate define bindings, improving readability by reducing nesting.

Implementation

  • Rule: Extracts multi-line named let initializers into define bindings prefixed with init-
  • Safety: Only applies when all non-extracted expressions are pure (prevents reordering side effects)
  • Scope: Definition contexts only (e.g., function bodies)

Example

;; Before
(define (f a b c)
  (let loop ([x (+ 1 2 3)]
             [y (if (a)
                    (b)
                    (c))])
    (loop x y)))

;; After
(define (f a b c)
  (define init-y
    (if (a)
        (b)
        (c)))
  (let loop ([x (+ 1 2 3)]
             [y init-y])
    (loop x y)))

Files

  • default-recommendations/simplify-named-let-initialization.rkt - Rule implementation
  • default-recommendations/simplify-named-let-initialization-test.rkt - Test suite covering positive and negative cases
  • default-recommendations.rkt - Added to default suite

Addresses #756

Original prompt

This section details on the original issue you should resolve

<issue_title>New rule: simplify-named-let-initialization</issue_title> <issue_description>### Rule summary

Old scheme code that doesn't use internal definitions sometimes uses complex named let loops with very large expressions used as the initial values for the named let's arguments. Since all of a named let's initial arguments are evaluated before anything in the named let is, it's more readable to extract complex initial value expressions into definitions preceding the named let.

This should only be done for named let argument initialization expressions that take up multiple lines, and it's only safe when the other initialization expressions are pure. It's also only possible in a definition context. The variable name chosen for the initial value should be the corresponding named let argument variable name prefixed with init-.

This rule should not be included alongside the let-replacement rules, since it isn't actually replacing a let binding with a define: it's introducing a new define binding to make an existing let binding easier to read.

Test case

#lang resyntax/test

test: "original code should be refactorable to new code"
--------------------
#lang racket
(define (f a b c)
  (let loop ([x (+ 1 2 3)]
             [y (if (a)
                    (b)
                    (c))])
    (loop x y)))
====================
#lang racket
(define (f a b c)
  (define init-y
    (if (a)
        (b)
        (c)))
  (let loop ([x (+ 1 2 3)]
             [y init-y)])
    (loop x y)))
--------------------

No-change test case

#lang resyntax/test

no-change-test: "code not refactorable"
--------------------
#lang racket
(define (f a b c)
  (let loop ([x (displayln "foo")]
             [y (if (a)
                    (b)
                    (c))])
    (loop x y)))
--------------------

Additional context

Saw this in racket/scribble#535, specifically this commit.</issue_description>

Comments on the Issue (you are @copilot in this section)

  • Fixes jackfirth/resyntax#745

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot avatar Nov 17 '25 17:11 Copilot