Organizing Data

本篇同步发文於个人网站: Organizing Data

This article references the chapter 9 ” Organizing Data ” 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.

organizing data

(Photo from Pixabay: https://pixabay.com/photos/cabinet-drawer-wood-catalogue-3283536/)


Split Variable

Tips
  • Loop variable and collecting variable are special. They are assigned value many times.
  • Other variable is responsible for one thing. It should be assigned value one time.
  • If some variable is assigned many times, we should split it into different variables.
Examples:
  • A simple example
  • Assigning to an Input Parameter

Github


Rename Field

Tips
  • A field with good naming makes the developer clever about what the code does.
  • Field and accessors (getter/setter) namings are important.
  • If a record is still not encapsulated, use Encapsulate Record.
Examples:
  • A simple example

Github


Replace Derived Variable with Query

Tips
  • We should minimize the scope of mutable data at much as possible.
  • Use this skill to avoid “We update the source data but we forget to update the derived variable”
Examples:
  • A simple example
  • More Than One Source

Github


Change Reference to Value

Tips
  • A value object is immutable. We can transfer this object into other logic without caring about modifying the object’s data.
  • If an object can be shared between many objects and they can see its modification, it should be a reference object.
  • Value object usually implements equals function.
Examples:
  • A simple example
  • More Than One Source

Github


Change Reference to Value

Tips
  • If a shared Value Object data needs update, we need to find all data copies and update them. Change it to Reference Object and the object’s update reflects on all clients.
  • Build a Repository to manage the reference object. All clients find the reference objects by the repository.
Examples:
  • A simple example

Github


Conclusion

This chapter introduces me how to organize data. In ASP.NET MVC development, I have used Service/Repository pattern to manage my data source. And I used the LINQ to perform Replace Derived Variable with Query. 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 9 ” Organizing Data ” of Refactoring: Improving the Design of Existing Code (2nd Edition). and it improves our programmer’s ability.


<<:  18. STM32-CAN-BUS (上)

>>:  19. STM32-CAN-BUS (下)

Day 16 - 研习计画起源与菜鸟业师面试学生篇

渐渐的掌握专案开发的技术以及与厂商洽谈的诀窍後,接着迎接了一个新的挑战是先前没有遇过的,那就是协助单...

Day 12总之待补

代填写代填写代填写代填写代填写代填写代填写代填写代填写代填写代填写代填写代填写代填写代填写代填写代填...

Day.6 「只要我想,我也能把 div 变 table!」 —— CSS display 属性

如同前面章节所说,一开始的标签元素,很单调只有一行并没有各式各样的元素,是浏览器内建的样式表赋予它...

DAY16支持向量机演算法(续五)

昨天介绍完SMO并算出a,b,今天就要来算出w和标记在图上, 依照w的公式,我们可以得到下面程序: ...

Day-1 旅程开始

对於Python 只知道一点语法,藉由参加读书会与上网找资料来重自己对於Python了解与实作。 主...