diff --git a/README.md b/README.md index dcaa38d..b6c090e 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # The MarsBased Handbook 🚀 -Welcome to __MarsBased__! This is the first thing you should read when boarding the __MarsBased__ spaceship. +Welcome to **MarsBased**! This is the first thing you should read when boarding the **MarsBased** spaceship. MarsBased is an all-remote development consultancy from Barcelona, building web and mobile products using Ruby on Rails, Python and JavaScript. We work at the intersection of business and technology, to help companies with their innovation projects using bleeding-edge technologies like AR/VR and AI. @@ -48,6 +48,7 @@ For now, we have the following resources available: 1. [Our Git & Commit guidelines](/guides/development/git-guidelines.md) 1. [Some of our used programming patterns](/guides/patterns/README.md) 1. [Angular guidelines](/guides/development/angular-guidelines.md) +1. [Angular 17 guidelines](/guides/development/angular-17-guidelines.md) 1. [React guidelines](/guides/development/react-guidelines.md) 2. [Back-end guidelines](/guides/development/back-end-development-guidelines.md) 3. [Code reviews guidelines](/guides/development/code-reviews-guidelines.md) @@ -65,4 +66,5 @@ For now, we have the following resources available: 1. MarsBased YouTube channel # Contributing + We encourage you to contribute! Please check out the [Contributing guides](./CONTRIBUTING.md) for guidelines about how to proceed. diff --git a/guides/development/angular-17-guidelines.md b/guides/development/angular-17-guidelines.md new file mode 100644 index 0000000..2d85ca4 --- /dev/null +++ b/guides/development/angular-17-guidelines.md @@ -0,0 +1,81 @@ +# MarsBased Angular 17 Style Guide + +### 🚧 This guide is focused on Angular 17, for older versions see [here](./angular-guidelines.md) 🚧 + +We follow the official and opinionated [Angular coding style guide](https://angular.dev/style-guide) as the primary source of best practices and conventions. + +## Standalone Components + +In the latest versions of Angular, it is recommended to use standalone components. These are components that do not have a direct relationship with other components and can be used independently. Previously, all elements were organized in modules, and using an element from a module meant loading the entire module. + +Now, with standalone components, each component is used individually, which improves application load time and performance. + +## Single Responsibility Services with State Management + +In Angular, services should adhere to the Single Responsibility Principle, meaning each service should focus on a specific domain or functionality. By doing so, services become easier to maintain, test, and reuse. + +Additionally, these services should manage their state using signals, allowing components to subscribe to state changes efficiently. Signals provide a reactive way to handle state, enabling components to subscribe to state changes and automatically update when the state changes. + +## Smart and Dumb (Presentational) components + +The only goal of a presentational component is to keep the UI logic isolated. It doesn't have access to injected business services. It communicates using @Input and @Output data flows.Presentational components communicate using Input and Output only. + +A smart component, or smart container, injects business services and keeps a more complex state. Its goal is to orchestrate the communication and behaviour between data services and presentational components. Smart components communicate to the rest of the app using injected services, Input an Outputs. + +Having a separation between smart and presentational components adds the advantages: + +- Easier to write and more focused tests. +- Isolated UI state. +- Better component reusability. + +## Structuring Application Architecture Using Routes + +In Angular, organizing the application's architecture by following the routes can enhance readability, maintainability, and scalability. Similar to other frameworks, using a route-based folder structure helps developers quickly locate components associated with specific routes, improving overall development efficiency. + +### Folder Structure + +Within the src directory, create a routes folder that mirrors the application's routes. Each route should have its own folder containing all related components, services, etc. This structure helps keep the project organized and makes it easier to manage and scale the application as it grows. + +Here's an example of how to structure the folders for an application with a users route: + +``` +src/ + app/ + routes/ + users/ + # users components +``` + +## State Interface + +Maintaining common interface components, such as modals, popups, or loading spinners, in a parent component common to the entire application is a recommended practice. This approach helps in managing the application's UI state efficiently and ensures a consistent look and feel across the application. + +By centralizing the management of common interface components in a parent component, you can: + +- Reduce Redundancy: Avoid duplicating code for common UI elements across multiple child components. +- Ensure Consistency: Maintain a uniform appearance and behavior for UI elements, ensuring a consistent user experience. +- Simplify Maintenance: Make it easier to update and manage UI components, as changes are made in a single place rather than across multiple components. + +## Input signals + +Components in Angular should receive input data through inputs, specifically as input signals. This practice becomes particularly advantageous when writing code in a reactive style using signals. + +### Advantages of Using Input Signals + +#### Reactive Style + +By using signals as inputs, all the component's input data become signals themselves. This allows for easily deriving computed signals from them and defining effects that react to input changes. In other words, it facilitates writing components in a reactive, signal-based style. + +#### Data Transformation + +The parent component does not need to transform or update the input data. This responsibility falls on the child component, which simplifies the logic in the parent component and makes it easier to reuse and test the child components. + +#### Change Detection + +Using signals as inputs improves the component's change detection. Angular can detect changes more efficiently because signals allow for more precise tracking of state modifications. + +#### State Derivation + +Signals enable more effective derivation of the input state. For example, you can create computed signals that depend on the inputs and automatically update the component's state when the inputs change. + +A more comprehensive article on signals in Angular can be found [here](https://blog.angular-university.io/angular-signal-inputs/). diff --git a/guides/development/angular-guidelines.md b/guides/development/angular-guidelines.md index 6f4d284..101bae2 100644 --- a/guides/development/angular-guidelines.md +++ b/guides/development/angular-guidelines.md @@ -1,39 +1,44 @@ # MarsBased Angular Style Guide +### 🚧 This guide is focused on Angular 2+, for Angular 17+ features see [here](./angular-17-guidelines.md) 🚧 + +--- + We follow the official and opinionated [Angular coding style guide](https://angular.io/guide/styleguide) as the primary source of best practices and conventions. -* 1. [Do's and Don'ts](#DosandDonts) - * 1.1. [Logic in templates](#Logicintemplates) - * 1.2. [Use index.ts](#Useindex.ts) - * 1.3. [Use CDK Virtual Scroll](#UseCDKVirtualScroll) - * 1.4. [Type Lazy modules](#TypeLazymodules) - * 1.5. [Private methods](#Privatemethods) - * 1.6. [Public methods](#Publicmethods) - * 1.7. [View Methods](#ViewMethods) - * 1.8. [Lifecycle Hooks](#LifecycleHooks) - * 1.9. [HTML Attributes](#HTMLAttributes) - * 1.10. [Empty Observables](#EmptyObservables) - * 1.11. [HostListener/HostBinding decorators versus host metadata](#HostListenerHostBindingdecoratorsversushostmetadata) - * 1.12. [Class members order](#ClassOrder) -* 2. [General project organization and architecture](#Generalprojectorganizationandarchitecture) - * 2.1. [Project structure example](#Projectstructureexample) -* 3. [Description of the most common patterns used to solve common problems](#Describemostcommonpatternsusedtosolvecommonproblems) - * 3.1. [Lazy Pages](#LazyPages) - * 3.2. [API Services](#APIServices) - * 3.2.1. [Why?](#Why) - * 3.2.2. [How?](#How) - * 3.3. [Forms (Reactive Forms)](#FormsReactiveForms) - * 3.3.1. [Simple FormBuilder sample](#SimpleFormBuildersample) - * 3.4. [Smart and Dumb (Presentational) components](#SmartandDumbPresentationalcomponents) - * 3.5. [State Management (with Akita)](#StateManagementwithAkita) - * 3.5.1. [Global Entities](#GlobalEntities) - * 3.5.2. [Specific module related logic](#Specificmodulerelatedlogic) - * 3.6. [Testing](#Testing) - * 3.7. [Memory Leaks](#MemoryLeaks) - * 3.8. [I18N](#I18N) -* 4. [Libraries](#Libraries) -* 5. [Learning Resources](#LearningResources) + +- 1. [Do's and Don'ts](#DosandDonts) + - 1.1. [Logic in templates](#Logicintemplates) + - 1.2. [Use index.ts](#Useindex.ts) + - 1.3. [Use CDK Virtual Scroll](#UseCDKVirtualScroll) + - 1.4. [Type Lazy modules](#TypeLazymodules) + - 1.5. [Private methods](#Privatemethods) + - 1.6. [Public methods](#Publicmethods) + - 1.7. [View Methods](#ViewMethods) + - 1.8. [Lifecycle Hooks](#LifecycleHooks) + - 1.9. [HTML Attributes](#HTMLAttributes) + - 1.10. [Empty Observables](#EmptyObservables) + - 1.11. [HostListener/HostBinding decorators versus host metadata](#HostListenerHostBindingdecoratorsversushostmetadata) + - 1.12. [Class members order](#ClassOrder) +- 2. [General project organization and architecture](#Generalprojectorganizationandarchitecture) + - 2.1. [Project structure example](#Projectstructureexample) +- 3. [Description of the most common patterns used to solve common problems](#Describemostcommonpatternsusedtosolvecommonproblems) + - 3.1. [Lazy Pages](#LazyPages) + - 3.2. [API Services](#APIServices) + - 3.2.1. [Why?](#Why) + - 3.2.2. [How?](#How) + - 3.3. [Forms (Reactive Forms)](#FormsReactiveForms) + - 3.3.1. [Simple FormBuilder sample](#SimpleFormBuildersample) + - 3.4. [Smart and Dumb (Presentational) components](#SmartandDumbPresentationalcomponents) + - 3.5. [State Management (with Akita)](#StateManagementwithAkita) + - 3.5.1. [Global Entities](#GlobalEntities) + - 3.5.2. [Specific module related logic](#Specificmodulerelatedlogic) + - 3.6. [Testing](#Testing) + - 3.7. [Memory Leaks](#MemoryLeaks) + - 3.8. [I18N](#I18N) +- 4. [Libraries](#Libraries) +- 5. [Learning Resources](#LearningResources) -## 1. Do's and Don'ts +## 1. Do's and Don'ts Add and follow the official [MarsBased Angular ESLint configuration](https://github.com/MarsBased/marstyle/tree/master/angular), where most of do's and dont's are already defined. -### 1.1. Logic in templates +### 1.1. Logic in templates -Don't put logic in templates. +Don't put logic in templates. ```html -