iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🍣

Notes on "Principles of System Design": Chapters 1 & 2

に公開

Introduction

I read Principles of System Design for the Field (link to Gihyo's website).

This article serves as a place for me to write down what I learned from the book. In other words:

  • To jot down key points I want to remember
  • To include links to related knowledge
  • Since the book is written in Java, to make notes on the syntax when writing in C#

It got long while I was writing, so I've limited this to Chapter 2.

Note: These are my own interpretations and do not necessarily capture the author's intent entirely.

If you only want the key points

The author's slides are available, and you can just keep hitting the right arrow key.

How to Find, Create, and Nurture Domain Objects

Chapter 1

The keyword is ★Encapsulation★

Key Points:

  • Never abbreviate names.
  • Use paragraphs to give meaning to your code.
  • Do not reuse variables (do not reassign).
  • Encapsulate concerns into classes.
  • Create classes to handle units.
    • Don't use int directly; create unique types like Price.
    • Encapsulate logic within the type (avoid classes that are just data, and avoid methods that don't use instance variables).
  • Create classes to handle collections; First-class collections.

I haven't fully grasped the nuances of the last point, First-class collections, as it is a deep topic.

Immutable Properties

It is reassuring to know that "this variable will never change."
It consumes less "mental memory."

If possible, I want to make full use of the provided modifiers (final and readonly).

Qiita Article -> Tips for writing readable code

// java
class A
{
    static final int MIN = 1;
}
// c#
class A
{
    static readonly int MIN = 1;
}

Are you in the underscore camp or the this camp for private variables?

In C#, it is common to prefix variables with an underscore _.
In this book, this is attached to private variables.
I don't know if that is a rule or a convention in Java.

My point is, I used to be in the underscore camp, but this is also nice.
I feel like IntelliSense is more on target with this.

Case 1: Using underscores... Not good enough

Preprocessors like if and region are suggested.

Case 2: Using this... Good!!

Relevant methods or variables are displayed.

Example of destructive assignment

It is better if the values of variables do not change as much as possible.
If you want to change them, it might be better to prepare a different variable.

In languages like JavaScript, it is recommended to declare values using const whenever possible.
In Rust, which has been popular lately, immutable variables are the standard.

I found some sample code from Microsoft recently that could be called a destructive assignment, so I'm linking it here.
At this scale, if you go out of your way to prepare many variables, I end up suspecting some deep intent, so I feel more at ease when Microsoft keeps it this rough.

Example of destructive assignment? -> TcpListener class
The variable data is reused.

Value Objects

Also known as Value Objects.

Everything is written in the article by MinoDriven of the "Crude Code Extermination Squad."

Qiita Article -> Exterminating low-cohesion classes with Value Objects packed with design requirements

Chapter 2

Key Points:

  • Do not use else.
  • Early return.
  • Use interfaces and polymorphism.
    • The side using the class should need to "know" as little as possible.
    • This also relates to the keyword ★Encapsulation★.
  • Make full use of enumerations.
    • Java's powerful enum is introduced. Can the same be done in C#... (?)

Enumerations

Although the book covers many useful things, unfortunately, they cannot be imitated in C#
(Though you could approximate them with wrappers or extension methods?)

Qiita Article -> Differences between C# and Java enums

Qiita Article -> 【C#】Tips for using Enums

I wonder if Java's Map is equivalent to C#'s Dictionary.
There seem to be minor differences... Where a Java programmer got stuck writing programs in C# Part 1

State transitions using Enums

P65: Trying to forcibly write a program in C# to determine if a transition from State A to State B is possible.

Define 3 states using an enum. Something like standby, ready, and operating.

internal enum State
{
    Wait,
    Move,
    Ready,
}

Create an extension method class for Enum

internal static class EnumExtend
{
    // Substitute Java's Map with C#'s Dictionary
    static Dictionary<State, HashSet<State>> allowd;

    static EnumExtend()
    {
        allowd = new();

        // Use a state as Key, and the transition destination as Value. There can be multiple Values.
        allowd.Add(State.Wait, new() { State.Ready });
        allowd.Add(State.Ready, new() { State.Wait, State.Move });
        allowd.Add(State.Move, new() { State.Wait });
    }

    // True if state transition is possible
    public static bool CanTransit(this State from, State to)
        => allowd[from].Contains(to);
}

Usage side

var state = State.Move;
var b = state.CanTransit(State.Wait);
// b: True

Conclusion

If I feel like it, I'll write about Chapter 3 onwards.

Discussion