Extract Variable

Extract Variable gives part of an expression a name: a local that holds it, and the expression using the name. Here one expression does too much at once to read, so EK9 refuses it - E11013, explained in the Hovers panel. To play it in the IDE: Help > Lessons > Refactoring…, or ek9 -ide -lesson extract-variable.

The EK9 IDE as the lesson opens: the code to read and, in the Hovers panel, its error explained

Start from

The code the lesson starts from: copy it into a file of your own and follow the steps.

Fails with E11013: excessive expression complexity - extract intermediate variables

#!ek9
defines module error.e11013.bad

  defines function

    chosen()
      ->
        first as Integer
        second as Integer
        third as Integer
        fourth as Integer
      <- rtn as Integer: ((first ?: second) <? third) >? fourth

Step 1: The expression

Select (first ?: second) <? third, on line 12.

The EK9 IDE once this is done: Select (first ?: second) <? third, on line 12

Step 2: Extract Variable

Choose Refactor > Extract Variable…

The EK9 IDE once this is done: Choose Refactor > Extract Variable…

Name it lower - the lower of the first two and the third - and choose Preview.

The EK9 IDE just before choosing Preview: Name it lower - the lower of the first two and the third - and choose Preview

Step 3: Preview and apply

The preview shows every change the refactoring makes; then the IDE formats each file it changed. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows every change the refactoring makes; then the IDE formats each file it changed

The result

That is Extract Variable: (first ?: second) <? third is lower, and the expression that did too much reads lower >? fourth - the return's value moved into the body to make room for the local. AI assistants use the ek9_extract_variable tool.

The EK9 IDE when the lesson has finished

Where this lesson comes from