# Clean Code - 8 Boundaries
When
- Buy 3rd party packages
- Use open source
- Use components or subsystems that are created by other teams
## Using Third-Party Code
- Provider interface (3rd party packages or frameworks)
- Broad applicability for various environments and users
- User interface
- Focus on the features they need
**It will cause boundary problems in the system**

### Solution: use interfaces
> This interface is also tailored and constrained to meet the needs of the application. It results in code that is easier to understand and harder to misuse.
### Example: TypeScript Dictionary
Multiple methods can be used when using TypeScript Dictionary like:
- Add
- Update
- Remove
Using dictionary directly leads to boundary problems:
```
// a shared instance RoomFactory
class RoomFactory {
public roomKeys = {
'storage': 'storage_abc',
'kitchen': 'kitchen_123',
'livingroom': 'livingroom_010',
'bathroom' : 'bathroom_456',
'doghouse' : 'doghouse_def',
}
}
const roomFactory = new RoomFactory();
// everyone has access to all keys
// since the return value can be:
// roomFactory.roomKeys.kitchen
// roomFactory.roomKeys.livingroom
// ...
function getRoomKey(): string {
return roomFactory.roomKeys.storage;
}
console.log(getRoomKey());
```
Proper usage:
```
// a shared instance RoomFactory
class RoomFactory {
private roomKeys = {
'storage': 'storage_abc',
'kitchen': 'kitchen_123',
'livingroom': 'livingroom_010',
'bathroom' : 'bathroom_456',
'doghouse' : 'doghouse_def',
}
public get guestRoomKey(): string {
return this.roomKeys.livingroom;
}
}
const roomFactory = new RoomFactory();
// the user only has certain permissions
function getRoomKey(): string {
return roomFactory.guestRoomKey;
}
console.log(getRoomKey());
```
## Exploring and Learning Boundaries
It is good to write some tests to test the 3rd party codes that we use
### Learning tests
> Could write some tests to explore our understanding of the third-party code. - Jim Newkirk
#### Other resources:
https://blog.thecodewhisperer.com/permalink/when-to-write-learning-tests
https://www.frederikbanke.com/linkedin-api-learning-tests/
## Learning log4j
Skip
## Learning Tests Are Better Than Free
- Why
- Have a better understanding of what we are using
- When
- Writing learning tests when there is a new release of the 3rd party package
- What
- To address the risks and incompatibilities
### Boundary tests
Though might not always write learning tests, should always have boundary tests for migration when the version of the 3rd package change.
## Using Code That Does Not Yet Exist
> There is another kind of boundary, one that separates the known from the unknown.

### Define our own interface
> 
> This design also gives us a very convenient seam in the code for testing. Using a suitable FakeTransmitter, we can test the CommunicationsController classes. We can also create boundary tests once we have the TransmitterAPI that make sure we are using the API correctly.
- Precondition: the real API `Communication Controller` is not ready yet
- `Trasmitter` is an interface that we wish to have
- The method `transmit` in `Trasmitter` will receive the input from the real API, and provide output to others that use `Trasmitter`
- `TransmitterAdapter` is to bridge the gap between the real API `Communication Controller` and our interface `Trasmitter`
### How `FakeTransmitter` work?
???
### Adapter Pattern
Source
https://refactoring.guru/design-patterns/book

## Clean Boundaries
> “The only thing that never changes is that everything changes.”
>
> ― Louis L'Amour
Same as boundaries. The goal is to be able to adapt to the changes without high costs such as time to rework. A good practice is to rely on what you can control, such as the interface that you design and wish to have, instead of depending on the 3rd party API, which you don't have full control of.
### Methods of managing third-party boundaries
- Wrap them up for proper access and control the usage
- Use an Adapter for communications between interfaces
###### tags: `learn` `clean code` `test automation`