Skip to content

Conversation

@wirapratamaz
Copy link

This PR refactors all example files in the /examples directory to follow SOLID principles and clean architecture patterns, making the codebase more maintainable, testable, and easier to extend.

Main Goals

  • ETC (Easy To Change): Primary focus on making code easy to modify and extend
  • SOLID Compliance: Implement all SOLID principles throughout examples
  • Code Quality: Eliminate code smells and improve maintainability

Key Improvements

Architecture Changes

  • Template Method Pattern: Introduced BaseExample abstract class for consistent operation structure
  • Factory Pattern: Added ConfigurationFactory, RelayClientFactory, and TransactionFactory for object creation
  • Strategy Pattern: Implemented ErrorHandler with pluggable error handling strategies
  • Dependency Injection: Proper DI for error handlers and client configuration

New Structure

examples/
├── shared/
│   └── utils.ts          # Shared utilities, factories, and base classes
├── deploy.ts             # Refactored with SafeDeployer class
├── execute.ts            # Refactored with TransactionExecutor class
├── getTransaction.ts     # Refactored with TransactionRetriever class
├── getTransactions.ts    # Refactored with TransactionLister class
└── poll.ts              # Refactored with TransactionPoller class

Code Smells Eliminated

  • Long Methods: Broke down methods under 25 lines
  • Duplicate Code: Centralized environment setup and client creation
  • Callback Hell: Modern async/await patterns
  • Poor Error Handling: Structured OperationResult<T> responses
  • Inconsistent Typing: Replaced any with proper TypeScript types

Breaking Changes

  • Method Names: Fixed executeSafeTransactionsexecute in execute.ts
  • Return Types: All operations now return OperationResult<T> instead of any
  • Environment Setup: Centralized in ConfigurationFactory

Testing

All examples maintain the same external behavior while improving internal structure. Each example can be run independently with:

npx ts-node examples/deploy.ts
npx ts-node examples/execute.ts
# etc.

🔗 Related Issues

This improvement supports better developer experience and provides a solid foundation for future example contributions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant