Code Conventions¶
Why? 🤔¶
Conventions over configuration¶
Naming convention¶
- Use English for naming variables, classes, methods, etc.
- Prefix
fetchfor methods returning a Future. - Prefix
watchfor methods returning a Stream. - Use de suffix
Modelfor classes that maps a json to an Object. Generally located in/lib/src/features/feature/data/models.
Error Management strategy 🤯¶
- Add validations to every text input field.
- Apply tristate pattern (loading, error, data) for every future.
- Always provide visual loading information to the user and be sure to prevent this action from being fired again.
- Always provide visual error information to the user.
Widget Creation strategy ✨¶
- Prefer
Statelessfor static widgets. - Prefer
Statefulfor animated widgets. - Prefer
Statefulatomic Widgets to wrap Broker Widgets. Example code. - Prefer always separate the Raw Widget from the App State dependent Widget. Example code.
Do ✅ and Don'ts 🚫¶
- 🚫 Extract Widget as class functions. Self explanatory code test can be found here
- 🚫 Create one line methods that make dificult readability and maintainability.
- 🚫 Place logic pieces of code on the UI side.
- 🚫 Mix App State with Ephemeral State. Difference between both can be found here.
- 🚫 Create private widgets classes, they are not testable. The exception could be the State class of the Stateful widget.
- 🚫 Deliver code without tests.
- 🚫 Don't use non-nullable operator without previous check. Example code.
- 🚫 Don't use
Equatablewith non App State entity classes. - ✅ Use localized text and 🚫 use hardcoded text to show user information.
- ✅ Use @riverpod annotation with Futures or Streams or return AsynValue
. - ✅ Use
selectto get a value from a provider to optimez performance. More info here. - ✅ Use
constfor static widgets. - ✅ Use
finalfor widgets that will change its state. - ✅ Use
finalfor variables that will not change its state. - ✅ Use
GoRouter.of(context).push()insteadcontext.push()to navigate to another screen. Same withGoRouter.of(context).pop()andGoRouter.of(context).go(). - ✅ Always add
toEntityand overridetoStringmethod on the Model classes. Example code
Comments strategy 💬¶
-
Use BetterComments extension:
//* to help structure views with component names. //? to mark something to ask. //! to prevent the developer of something.
Big PR?? 😱¶
- It's ok, try to comment out the changes you made to make reviewer's lives easier.
- Add all the requirements or evidence of the developed task.
Test strategy 🔬¶
- Isolated tests: all the mock classes should be in the same file. Example code.
TODO strategy 😮💨¶
- Add a story for every TODO.
// TODO(UONB-XXXX): this should be refactored