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

ResponsibilityTypical work
Event handlingReceive an event, prevent unwanted default behaviour, and read input.
Data updatesValidate input and change the application's state.
DisplayShow the current state through text, attributes, classes, or generated elements.
Storage or requestsSave, 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.