This article is an article that summarizes what I learned from reading "Principles of system design useful in the field" and what seems to be useful at work. It's just a summary, and I've omitted the parts that I can't explain, so if you want to understand it properly, please read the book!
[Principles of system design useful in the field](https://www.amazon.co.jp/%E7%8F%BE%E5%A0%B4%E3%81%A7%E5%BD%B9%E7%AB % 8B% E3% 81% A4% E3% 82% B7% E3% 82% B9% E3% 83% 86% E3% 83% A0% E8% A8% AD% E8% A8% 88% E3% 81% AE % E5% 8E% 9F% E5% 89% 87-% E5% A4% 89% E6% 9B% B4% E3% 82% 92% E6% A5% BD% E3% 81% A7% E5% AE% 89% E5% 85% A8% E3% 81% AB% E3% 81% 99% E3% 82% 8B% E3% 82% AA% E3% 83% 96% E3% 82% B8% E3% 82% A7% E3% 82% AF% E3% 83% 88% E6% 8C% 87% E5% 90% 91% E3% 81% AE% E5% AE% 9F% E8% B7% B5% E6% 8A% 80% E6% B3% 95-% E5% A2% 97% E7% 94% B0-% E4% BA% A8 / dp / 477419087X)
The author is Toru Masuda, who is also involved in application design and development using Domain Driven Design. There were many slides related to Domain Driven Design in SlideShare.
――Side effects and unexpected problems occur due to software modifications and changes because there is a problem with the design (= source code). --A program that is difficult to change has three characteristics. --Long method (difficult to understand) --Large class (difficult to identify the range of influence when changing) --Many arguments (difficult to specify the range of influence when changing) ――The whole thing becomes complicated due to the overlap of small corrections.
--Use a descriptive name --Avoid using abbreviations and mismatched names
// Bad
int a;
int qty;
return qty * up;
// Good
int quantity;
return quantity * unitPrice;
--Insert a blank line for each code unit (processing unit) --Prepare variables (= variables for explanation) for each purpose (Avoid destructive assignment ((destructive assignment is a way of writing by using one variable and repeating assignment)))
// Bad
int price = quantity * unitPrice; //Quantity x unit price
if (price < 3000)
price += 500; //Shipping
price = price * taxRate(); //Amount including tax
// Good
int basePrice = quantity * unitPrice; //Quantity x unit price
int shippingCost = 0; //Shipping
if (basePrice < 3000)
shippingCost = 500;
int ItemPrice = ... //Amount including tax
--Independent as a method (merits are as follows) --It's easier to lock the effects of changes inside the method --Details are described in the method, so the calling code is clean. --The name of the method makes it easier to understand the intent of the code
int basePrice = quantity * unitPrice; //Quantity x unit price
int shippingCost = shippingConst(basePrice) //Cut out the shipping calculation to another method
int shippingCost(int basePrice) {
...
}
--Eliminate duplicate code in different classes --Create a class (domain object) corresponding to each business interest ――For example, you can limit the target of change and the range of influence within the object by grouping the shipping fee into the shipping class.
--Values handled in business may not match the values handled in basic data types ――For example, even if the unit price is up to 100 million yen in business, if you use int, you can handle from minus 2.1 billion yen to plus 2.1 billion yen. --You can prevent abnormal values by creating your own class (value object) so that you can handle correct values for business purposes ((the method of creating a dedicated class for handling values is called a value object)). it can --For example, by creating a Quantity class that specializes in quantity and a Telephone class that specializes in telephone numbers, you can prevent abnormal values and at the same time clarify the intent of the code. --Make value objects immutable (create another object when you need another value) --Variable overwriting can have unexpected side effects --Use types to make your code clear and secure --Use types to get the compiler to find mistakes
// Bad
int amount(int unitPrice , int quantity) {
... //A compile error does not occur even if the order of passing unitPrice and quantity is incorrect.
}
// Good
Money amount(Money unitPrice, Quantity quantity) {
... //If you pass the unitPrice and quantity in the wrong order, a compile error will occur.
}
--The basis of code organization is in names and paragraphs --Short methods, organize your code in small classes --Let's make it easy to understand and secure by using value objects --Matching class names and method names with business terms makes the program easier to understand and easier to change.