How to use refactoring methods?

Starting from this code:

 new Person("ET").WithAge(88)

How can this be reorganized:

 new Person("ET", 88)

What sequence of refactoring is needed to complete the conversion?

Why? Because there can be hundreds of them, and I would not want to introduce errors by doing this manually.

Could you say that the flaw is with smooth interfaces - they cannot be easily reorganized?

NOTE. I want to do this automatically without manually typing the code.

+3
source share
5 answers

, - "WithAge" "InitAge", InitAge private, . new Person(string).WithAge(int), .

WithAge , InitAge, .

, , .

+4

, WithAge - Person, Person, -

Person(string name, int age)
{
    this.name = name;
    this.WithAge(age);
}

:

Person(SomeType originalParameter, FluentParamType fluentParameter)
{
    //Original constructor stuff
    this.FluentMethod(fluentParameter);
}

, FluentMethod , , , .

+3

# ( ), Person :

public Person(string name, int age)
    : this(name) { WithAge(age); }

, , , :

new Person(x1).WithAge(x2)

x1 x2 :

new Person(x1, x2)

WithAge, . :

new Person(x1).WithHair(x2).WithAge(x3)

, , :

new Person(x1, x3).WithHair(x2)

, IDE, /, . , .

, , ?

- , IDE , , . , , , .

+2

, , , , custom Eclipse refactorings ( Refactor! Pro .Net, , ).

, , , , . .

, , . :

class Person {
  public Person(String name, int age);
  public Person(String name, int numberOfChildren);
}

, Person.WithAge, .

, , . , .

, . :

public Person(String name, int age) {
  this(name);
  withAge(age);
}

.

( , Age , , , . , , withAge - . , ...)

+1
  • .

  • , .

0

Source: https://habr.com/ru/post/1706259/


All Articles