本篇同步发文於个人网站: 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.
(Photo from Pixabay: https://pixabay.com/illustrations/plumber-repair-tools-pipe-plunger-4427401/https://pixabay.com/illustrations/moving-boxes-mover-moving-truck-3671446/ )
// before refactoring
bool hasMyFavoriteFruit = false;
foreach(var fruit in basket)
{
if(fruit == "Apple")
{
hasMyFavoriteFruit = true;
}
}
// after refactoring
bool hasMyFavoriteFruit = basket.Contains("Apple");
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;
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)
前言 今天要来实作 Communicate Between Containers in the Sa...
上一篇有提到关於如何在向量中求梯度下降的公式, 因此此篇要来讲为什麽要向量v跟f(x,y)的偏微分作...
前言 我们今天还是没有离开 user_bp,我们要来弄写文章的页面,也就是 markdown 上场的...
-使用 AWS 构建无服务器後端 微服务通常托管在部署在本地或 PaaS 上的容器中;它们也可以在...
Cloud ?? 今天来跟各位介绍一下到底什麽是云端。 4ㄉ,所谓的云端就是先教会电脑怎麽飞行,而通...