Reusable functions
Functions
Architecture
Organize complex logic by creating reusable functions for cleaner code.
Getting Started with a real app (Episode 6)
Back to the Academy
Reusable functions
Express Summary: Organize complex logic by creating reusable functions for cleaner code.
Problem: Complex and repetitive logic
Just like visual elements with components, we need to organize algorithmic elements with functions. This makes logic cleaner and allows reusing certain functionalities like "get number of completed tasks".
1. Create a function script
New script
- Right-click � New Script
- Name the script "fonction" or "functions"
- Important: Change scope to Frontend
The three available scopes
- Frontend: client-side user interface logic
- Backend: server-side logic
- Shared: functions usable everywhere (frontend + backend)
Why not put everything in Shared?
- Performance: user only loads client, not server
- Security: certain logic (connection, etc.) shouldn't be exposed client-side
- Architecture: avoid circular dependencies
2. Create the getTotalTasks function
Function configuration
- Name the function getTotalTasks
- Check Export to import it elsewhere
- � Attention: only check Export if really necessary (avoid circular dependencies)
Migrate logic
- From Main page, copy all total task calculation logic:
- get store � spread � get property (cards) � Map � Sum
- Paste into getTotalTasks function
- Connect input to beginning of chain (map)
- Add an output named "totalTasks" of type number
- Connect result (sum) to output
3. Create the getFinishTasks function
Same principle
- Create function getFinishTasks
- Check Export
- Migrate completed task calculation logic:
- get store � spread � get property (cards) � Filter � hat[0] � get property (tasks) � get length
Configure inputs/outputs
- Connect input to filter
- Add "finishTasks" output of type number
- Connect result to output
4. Use functions in Main
Clean old logic
- Return to Main page
- Delete all complex calculation blocks
- Keep only division, multiplication, and rounding blocks
Import and use new functions
- Add getTotalTasks to logic
- Add getFinishTasks to logic
- Connect execution: computed � getTotalTasks � getFinishTasks
- Connect results:
- totalTasks � "total" input of division
- finishTasks � "completed" input of division
Advantages of this approach
Cleaner code
- Main logic readable and concise
- Separation of responsibilities
- Explicit function names
Reusability
- Functions available throughout application
- Avoids code duplication
- Centralized maintenance
Performance
- Optimized loading by scope
- Avoids unnecessary dependencies
Best practices
When to use Export?
- Only if function will be reused elsewhere
- Avoid systematic export to not overload application
Scope organization
- Frontend: interface calculations, formatting, client-side validations
- Backend: authentication, database, sensitive business logic
- Shared: common utilities, constants, helpers
Function structure
- Input: entry parameters
- Logic: data processing
- Output: typed and named result
Final result
The percentage still works perfectly, but with much more organized and maintainable code. Functions can now be reused in other parts of the application.
Tests and validation
- Test functions independently: each function can be tested individually
- Verify reusability: import functions in other components
- Performance: loading optimized thanks to appropriate scopes
Architecture summary
Final project structure
- Pages: user interface and display logic
- Components: reusable elements with properties
- Scripts/Functions: business logic organized by scope
- Types: data structure definitions
- Store: global application state
Congratulations! You now have a functional Trello application with clean and maintainable architecture.