[JAVA] I learned a lot about "principles of system design that are useful in the field", so I summarized them ~ Chapter 1 ~

Do you know the principles of system design?

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 person who wrote this book

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.

Mr. Masuda's slideshare

Chapter 1 "Small and easy to understand"

Why software changes are so difficult

――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.

How to write to make it easier to change the program

--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.

Make it easy to understand and safe in a small 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.
}

Chapter 1 Summary

--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.

Recommended Posts

I learned a lot about "principles of system design that are useful in the field", so I summarized them ~ Chapter 1 ~
[Reading memo] System design principles that are useful in the field- "Small and easy to understand"
I learned about the existence of a gemspec file
I hate this kind of code! A collection of anti-patterns actually seen in the field
When I switched to IntelliJ, I got a lot of differences in the encoding of the properties file.
Determine that the value is a multiple of 〇 in Ruby
A program that counts the number of words in a List