Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 17, 2025

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)


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

Copilot AI and others added 3 commits November 17, 2025 17:21
Co-authored-by: jackfirth <8175575+jackfirth@users.noreply.github.com>
Co-authored-by: jackfirth <8175575+jackfirth@users.noreply.github.com>
Co-authored-by: jackfirth <8175575+jackfirth@users.noreply.github.com>
Copilot AI changed the title [WIP] Add new rule: simplify named let initialization Add simplify-named-let-initialization refactoring rule Nov 17, 2025
Copilot AI requested a review from jackfirth November 17, 2025 17:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

New rule: simplify-named-let-initialization

2 participants