Organizing Application Code
Application architecture describes how the parts of an application work together. For a small JavaScript page, clear function responsibilities and a predictable flow are enough. A framework or multiple JavaScript files are not necessary.
Separate responsibilities
| Responsibility | Typical work |
|---|---|
| Event handling | Receive an event, prevent unwanted default behaviour, and read input. |
| Data updates | Validate input and change the application's state. |
| Display | Show the current state through text, attributes, classes, or generated elements. |
| Storage or requests | Save, load, or fetch information and handle failures. |
A handler may call several functions. Keep shared rules in one place rather than repeating them in multiple handlers.
Plan the flow
User action → read and validate → update state → update display → save if required.
Startup has a different purpose: initialize data, restore or request information where needed, and display the result. Loading a preference should not accidentally save over the stored value. A reset that removes saved data should not immediately recreate it.
For each function, record its purpose, parameters, returned value, and any changes it makes to state or the page. Display functions can be reused after user actions and during startup.
Checklist
- [ ] Each function has a clear responsibility.
- [ ] I know which event starts each interaction.
- [ ] Shared rules are not duplicated.
- [ ] The page reflects application state consistently.
- [ ] Startup, normal interaction, and reset flows are distinct.