Moving Features

本篇同步发文於个人网站: Moving Features

This article references the chapter 8 " Moving Features " of Refactoring: Improving the Design of Existing Code (2nd Edition). Author had highlighted many important refactorings in this chapter.

I use .NET C# to practice with these refactorings and upload to Github.

Moving Features

(Photo from Pixabay: https://pixabay.com/illustrations/plumber-repair-tools-pipe-plunger-4427401/https://pixabay.com/illustrations/moving-boxes-mover-moving-truck-3671446/ )

Move Function

Tips
  • If a nested function of a outer function may be used for other situations, move this nested function to a general scope.
  • If a function of a class is better for other class, move this function into the target class.
Examples:
  • Moving a Nested Function to Top Level
  • Moving between Classes

Github

Move Field

Tips
  • If other structures are modified after one field is modified, we should move this field into a central structure.
  • Field should be encapsulated, it means that we add get/set accessors to encapsulate fields to avoid "Bare" record.
Examples:
  • A simple example
  • Moving to a Shared Object

Github

Move Statements into Function

Tips
  • If some statement repeatably works on many places, we should move it into a function.
Examples:
  • A simple example

Github

Move Statements into Callers

Tips
  • If some statement are changed by callers in a function, we should extract it to each caller.
Examples:
  • Just use the Move Statements into Function example from end to start

Replace Inline Code with Function Call

Tips
  • If some inline code has been implements in some function, replace it with the function.
  • A good naming function shows what the inline code outputs without understanding the implementation
Examples:
// before refactoring
bool hasMyFavoriteFruit = false;
foreach(var fruit in basket)
{
    if(fruit == "Apple")
    {
        hasMyFavoriteFruit  = true;
    }
}

// after refactoring
bool hasMyFavoriteFruit = basket.Contains("Apple");

Slide Statements

Tips
  • Some variable declarations are placed together on the top of scope, but author recommends that he declares variable when only first time he uses it.
  • Some statement has side-effect. If we slide it without test, it may cause the error.
  • We can implement the statement (function) by using Command/Query separation pattern to avoid the side-effect.
  • This refactoring focuses on the change of the state. When we slide statement, Split Variable is a good method.
Examples:

Sliding with Conditionals

// before refactoring
Response result = null;
if(connection.Status == Status.OK)
{
    result = CreateSuccessMessage();
    connectionResponses.Add(result);
}
else
{
    result = CreateFailsMessage();
    connectionResponses.Add(result);
}

return result;

// after refactoring
Response result = null;
if(connection.Status == Status.OK)
{
    result = CreateSuccessMessage();
}
else
{
    result = CreateFailsMessage();
}

connectionResponses.Add(result);
return result;

Split Loop

Tips
  • If a loop contains many independent works, split them into their own loop scope.
  • We use Move Statement to move related variable declaration near by the new loop.
  • Split loop can be extracted as a function.
  • Replace Loop with Pipeline is a better implementation.
Examples:
  • A simple example

Github

Replace Loop with Pipeline

Tips
  • Collection Pipeline is a modern pattern. A pipeline receives a collection and process it with some operations and outputs a new collection.
  • JavaScript has map/filter/... pipeline functions, C# has LINQ (select/where...).
  • Pipeline increases the code readability.
Examples:
  • A simple example

Github

Remove Dead Code

Tips
  • If some code is not used ever, remove it to avoid confusing you/your team members.
  • Version control can manage all code history. Make the good use of it.

Conclusion

This chapter introduces me how to move the code logic greatly. In my early coding job, I used to write long logic function. And this function was gradually hard maintainable. I learned some like Moving Features skills to enhance the code but it still was not completed. I have grown up to write better code when I study this refactoring chapter. If you want to learn detailed motivation and mechanics, study the chapter 8 " Moving Features " of Refactoring: Improving the Design of Existing Code (2nd Edition). and it improves our programmer's ability.


<<:  ISO 27001 资讯安全管理系统 【解析】(十六)

>>:  Python & Celery 学习笔记_重试策略 (retry)

课堂笔记 - 深度学习 Deep Learning (18)

上一篇有提到关於如何在向量中求梯度下降的公式, 因此此篇要来讲为什麽要向量v跟f(x,y)的偏微分作...

Day 25 实作 user_bp (3)

前言 我们今天还是没有离开 user_bp,我们要来弄写文章的页面,也就是 markdown 上场的...

无服务器计算(Serverless computing)支持微服务架构并具有最低管理开销来部署企业级应用程序的云服务模型

-使用 AWS 构建无服务器後端 微服务通常托管在部署在本地或 PaaS 上的容器中;它们也可以在...

[Day2] What is Cloud

Cloud ?? 今天来跟各位介绍一下到底什麽是云端。 4ㄉ,所谓的云端就是先教会电脑怎麽飞行,而通...