Do One Thing … Well … Well
Focus, Focus, Focus
You’ve heard it…
- Cohesion
- Separation of concerns
- Single responsibility principle
- Square law of computation
It’s even more important here
Why?
A Simple Server
Imagine a server that forever…
- Waits for a client connection,
- Gets a message
- Processes it
And its client
Now we need a client that…
- Connects to the server
- Sends a message
- Awaits a response
- Disconnects from the server
You need to make this faster…
Threads to the rescue, right?
- How can I introduce threading reliably?
- What will change in the design to support threading?
-
How can I make this easier to test?
How do we test?
What makes this hard to test?
- Direct dependency on Sockets
- Embedded logic not exposed
- Class violates Single Responsibility
- Connection Management
-
Process client request
We want to verify threading…
Extract Classes:
- Pull client processing into its own class
- Pull client acquisition into its own class
- Verify code works as is
- Introduce threading
-
Show everything still working
Demo
Now we’ll walk through a rewrite of the class to speed it up…
- Client Request Processing
- Client Acquisition
- Dependency Injection
- Server Update
Comments