Refactoring towards better design: Extract/inline method part 1

Hello my dear developers

How do you feel today?

On this refactor I want to pay attention to a code smell called Feature envy, this smell is easy to spot because code outside a class uses data from a class for the application flow.

I have it in my sample when accession pieces position to find a given piece.

private bool TryFindPiece(Position position, out Piece piece)
{
    if (!_pieces.Any(p => p.Position.Equals(position)))
    {
        piece = default(Piece);
        return false;
    }
...
}

Therefore let’s use the extract method refactor to have a IsAt method on piece

private bool TryFindPiece(Position position, out Piece piece)
{
    if (!_pieces.Any(p => p.Position.Equals(position)))
    {
        piece = default(Piece);
        return false;
    }
...
}
private bool TryFindPiece(Position position, out Piece piece)
{
    if (!_pieces.Any(p => p.IsAt(position)))
    {
        piece = default(Piece);
        return false;
    }
...
}

This follow the principal only talk to your friends and on this case Position is a friend of Piece. So if Piece is friends with Board, talking to Position would be talking to a friend of a friend.

I hope that this was as helpful for me as it was for you.

Happy coding and remember… there is life beyond coding 😄

Refactoring towards better design: Introduce parameter object

Hello my dear developers

How do you feel today?

It was my understanding that changing code to improve it structure is called redeveloping. To refactor you need code coverage to ensure that your changes don’t modify the behaviour of your code.

On the other hand, I’ve grown fond of living always green, meaning to apply very small changes in your code so your tests keep green, and most importantly committing these smalls increments to your source control.

To try and go always green on my tests I use TPP (Transformation priority premise) but it felt somewhat a little outdated. After all, most IDEs have some sort of refactor pre-sets (on Visual studio for instance extract method is your first option).

I wandered how to learn these techniques and found Catalog of refactoring that lists all of them.

The samples are not super descriptive, in my opinion, plus it feels that every clean code related sample is java. Therefore, being a dot net developer, I’ll try to make my own dotnet 6 samples on my Github repo.

Starting with Introduce parameter object sample.

public void Move(string startColumn, int startRow, string endColumn, int endRow)
{            
    if(TryFindPiece(startColumn, startRow, out Piece piece))
    {                
        _pieces.Remove(piece);
        _pieces.Add(piece.Move(endColumn, endRow));                
    }
}
public void Move(Position start, Position end)
{
    if(TryFindPiece(start, out Piece piece))
    {                
        _pieces.Remove(piece);
        _pieces.Add(piece.Move(end));
    }
}

I hope that this was as helpful for me as it was for you.

Happy coding and remember… there is life beyond coding 😄