Refactoring Guru Refactoring Examples

repository·main·Indexed 19 days ago

https://github.com/refactoringguru/refactoring-examples

A collection of interactive programming examples demonstrating refactoring techniques in Java, C#, PHP, and TypeScript. Includes documentation on defining interactive scenarios, using popover tooltips, selecting code elements, and simulating compilation results through a specialized command set including Print, Replace, and Remove.

Tokens
1.8K
Snippets
7
Records
8
Agent score
16%

What's inside refactoring-examples

  1. Simulate compilation results

    main

    Since compilation is simulated, use a two-step popover sequence to show the process:

    1. Start Compilation: Use #C followed by a message explaining the compilation attempt.
    2. Show Result:
      • Use #S for a successful compilation with a success message.
      • Use #F for a failed compilation with an error message. If failing, you can follow up with a Select command to highlight the error location.

    Example:

    #C Let's compile this baby.
    
    #S Everything works fine!
    
    #C Let's compile this baby.
    
    #F Error! Unknown variable <code>b</code> in method "someMethod"
    
    Select "b" in "someMethod"
    #C Let's compile this baby.
    
    #S Everything works fine!
    
    #C Let's compile this baby.
    
    #F Error! Unknown variable <code>b</code> in method "someMethod"
    
    Select "b" in "someMethod"
  2. Define an interactive example scenario

    main

    An interactive scenario is a script that guides a user through a refactoring process. It is composed of 5 distinct sections separated by ### delimiters:

    1. Scenario ID and language: The identifier and target language (e.g., extract-method:java).
    2. Steps: A numbered list of steps (one per line).
    3. Starting code: The initial code state, wrapped in triple backticks.
    4. Resulting code: The final code state after refactoring, wrapped in triple backticks.
    5. Actions: A list of commands that drive the interactive experience.

    Example structure:

    extract-method:java
    
    ###
    
    1. Some step.
    2. Another step.
    
    ###
    
    ```java
    class Example {
    }

    class Example {
      public int field;
    }

    Set step 1

    First popover

    extract-method:java

    1. Some step.

    2. Another step.

    class Example {
    
    }

    class Example {
      public int field;
    }

    Set step 1

    Here's the first popover

  3. Select text and code elements

    main

    The Select action highlights text to draw attention or prepare for replacement.

    Key capabilities:

    • Targeting specific items: Use Select Nth "text" to pick a specific match from multiple results.
    • Contextual selection: Use in to narrow down the search area (e.g., Select "field" in "ExampleClass").
    • Structural selection: Target parts of code like parameters of, body of, name of, visibility of, type of, or whole of a method/class.
    • Multiline selection: Use a block syntax:
      Select:
      line 1 line 2
    • Sub-selection: Use ||| to select specific parts of a larger selection (e.g., Select "Multiple |||lines||| go here").
    • Additive selection: Prefix a command with + to add to the current selection instead of replacing it (e.g., + Select "something else").
    Select "private int field;"
    Select 3rd "getSomething()"
    Select "private int field;" in "ExampleClass"
    Select "interval" in parameters of "doSomething"
    Select "interval" in body of "doSomething" in "ExampleClass"
    Select:

    Multiple lines go here

    Select: 

    Multiple |||lines||| go here. I also want to select |||this|||.

    + Select "something else"
  4. Modify code with Print, Replace, and Remove

    main

    Use these commands to change the code content:

    • Print (or Type): Inserts text at the cursor or replaces selected text. Supports multiline blocks.
    • Replace: Identical to Print, but includes a slight delay to allow the user to see the selection before the change occurs.
    • Remove selected: Deletes the currently selected text. Includes a slight delay so the user sees what is being deleted.
    • Indent / Deindent: Adjusts indentation. Use Indent N times or Deindent N times where N is the number of tab stops.
    Print "some text"
    
    Print:

    Multiline text goes here.

    
    Replace "old text"
    
    Remove selected
    
    Indent 2 times
    
    Deindent
  5. Use Popovers (tooltips) to guide users

    main

    Popovers are tooltips that attach to the selected text or the cursor. Use the # character to create them. You can append options to the # character to control behavior:

    • Direction: Use arrow characters (^, V, <, >) to point the popover. Default is down (V).
    • Delay: Append a number (milliseconds) to automatically proceed after a delay (e.g., #<2000).
    • Remain visible: Append + to keep the popover visible after the next action (use #= to hide it later).
    • Attachment to steps: Use Q to attach the popover to the step list (useful for final messages).
    • Close all: Use #= to close all visible popovers and show a new one.

    Example syntax:

    # Simple popover
    #<2000+ Popover to the right, fires in 2s, remains visible.
    #= Close all visible popovers.
    #Q Show final popover attached to steps.
    #<2000+ Popover to the right of selected text, which will fire next action in 2 seconds, but will remain visible.
    
    #= Close all visible popovers and show new one.
    
    #Q Show final popover, attached to steps.
  6. Move the cursor with Go to

    main

    The Go to command moves the cursor to a specific location. This is often used to set a target for a Print action. Note that Go to deselects any current selection.

    Usage patterns:

    • Marker-based: Use ||| to specify the exact insertion point (e.g., Go to "private |||int field;").
    • Named targets:
      • Go to the end of file
      • Go to start of "methodName"
      • Go to end of "methodName"
      • Go to before "methodName" in "ClassName"
      • Go to after "methodName"
      • Go to parameters of "methodName"
      • Go to the end of parameters of "methodName" in "ClassName"

    Note: The cursor is inserted after opening braces { or at the end of the previous line before a closing brace }.

    Go to "private |||int field;"
    Go to:

    class Example { private |||int field; }

    Go to start of "someMethod"
    Go to parameters of "someMethod"