planetary-social / nos

nos.social social media for all of us, using nostr
https://nos.social
Mozilla Public License 2.0
103 stars 10 forks source link

Transitioning to a Clean Code Architecture with View Models and States in SwiftUI #735

Open vigotskij opened 6 months ago

vigotskij commented 6 months ago

Description:

The current codebase employs monolithic SwiftUI views, encompassing various responsibilities such as analytics, backend calls, and presentation logic within a single file. While functional, this approach can lead to challenges in terms of code maintainability, testability, and scalability. I propose a transition to an architecture pattern that embraces View Models and States, aligning with clean code principles.

Challenges with Monolithic Views:

1. Maintainability:

Monolithic views tend to accumulate logic over time, making them challenging to maintain and understand. Separating concerns into distinct components improves code readability and ease of maintenance.

2. Testability:

Testing becomes more intricate when all logic resides within a single view. Introducing View Models enables unit testing, ensuring the robustness of the application through automated tests.

3. Scalability:

As the app grows, maintaining a modular and scalable structure becomes crucial. View Models and States offer a more scalable solution, allowing for the efficient addition of features without the risk of codebase bloat.

Benefits of Clean Code Architecture:

1. Separation of Concerns:

Adopting View Models and States facilitates the separation of concerns. Each component focuses on a specific aspect of functionality, promoting a cleaner and more modular codebase.

2. Test-Driven Development:

Clean code principles encourage TDD. With View Models, writing unit tests becomes more straightforward, providing a safety net for code changes and updates.

3. Improved Code Readability:

Clean code practices enhance code readability, making it easier for developers to comprehend and collaborate on the codebase. This, in turn, accelerates onboarding for new team members.

4. Adaptability to Change:

A clean code architecture is inherently adaptable to change. When new requirements emerge or modifications are necessary, the modular structure enables developers to implement changes more efficiently.

5. Long-Term Maintenance:

The benefits of adopting clean code principles extend into the long term. A well-structured codebase is more sustainable, reducing technical debt and facilitating ongoing maintenance efforts.

Proposal:

Transition from monolithic SwiftUI views to a clean code architecture with View Models and States. This transition promises improved maintainability, testability, and scalability, aligning with long-term development goals.

joshuatbrown commented 1 month ago

@vigotskij Thanks for the proposal! Sorry we haven't responded yet, but we really do appreciate you taking the time to write this up and share it with us! I'll bring it up with the team for discussion -- you make a lot of great points. Thanks again!

vigotskij commented 1 month ago

@joshuatbrown no poblem! Feel free to ping me if you are interested in further discussions about this or any of the other topics 👍