# Monikers Instead of Variables

Source: https://www.yegor256.com/2017/05/16/monikers.html

If we agree that all local variables [must be](https://softwareengineering.stackexchange.com/questions/48413)
`final`, multiple `return`s must be avoided, and
temporal coupling between statements is evil---we can get
rid of variables entirely and replace them with _inline values_
and their _monikers_.


{% jb_picture_body %}

Here is the code from Section 5.10 (_Algorithms_) of my book
[_Elegant Objects_](https://amzn.to/2pjciUY):

```java
public class Main {
  public static void main(String... args) {
    final Secret secret = new Secret();
    new Farewell(
      new Attempts(
        new VerboseDiff(
          new Diff(
            secret,
            new Guess()
          )
        ), 5
      ),
      secret
    ).say();
  }
}
```

Pay attention to the variable `secret`. It exists here because we
need its value twice: first, as a constructor argument for the `Diff`, second
as a constructor argument for the `Farewell`. We can't _inline_ the value by
creating two separate instances of class `Secret`, because it really has
to be the same object---it encapsulates the number that we hide
from the user in a number-guessing game.

There could be many other situations where a _value_ needs to be used multiple
times while remaining unmodifiable. Why do we still call these values _variables_ if
technically they are constants?

I'm suggesting we introduce "monikers" for these values, assigning them
through the `as` keyword. For example:

```java
public class Main {
  public static void main(String... args) {
    new Farewell(
      new Attempts(
        new VerboseDiff(
          new Diff(
            new Secret() as secret,
            new Guess()
          )
        ), 5
      ),
      secret
    ).say();
  }
}
```

Here `new Secret()` is the inlined value and `secret` is its _moniker_, which
we use a few lines later.

It would be great to have this feature in Java, right?
