AngularJS, Angular, Express, Spring, JavaScript, TypeScript, Java, FronEnd, BackEnd… different frameworks and languages which have the same pitfalls in common; year after year I get used applying key concept such as code centralization, atomic component and classes focusing on reusability and reliability, ending up starting every new project using those I call “foundations”: a collection of utilities which let you develop faster and solve common issues related to services, pages and functionalities working on few lines of code in specific blocks, reducing knowledge transfer and documentation studying.

Spesso si inizia un progetto senza avere una visione chiara del prodotto finale o quello che si andrà a fare a livello tecnico: uno degli errori più grandi è procedere senza uno schema iniziale; è infatti indispensabile da subito dedicare utility, funzioni wrapper, file di configurazione e costanti.

Setup: Constants and Enumerators

Even if it can seem trivial, spending time on specific files for static strings, numbers and regular expressions is extremely important to gather together key and configuration values, frequently used, in specific places. Some concept examples:

  • Validation criteria
    • Regular expressions
    • min and max string length
    • min and max numeric value
  • Web/client config parameters
    • Timeout (useful to simulate production limit lower than 120 secs)
    • Additional headers to be injected in HTTP requests
  • Output (for enrichment)
    • UX texts and l18n
    • Errors and error codes

Utility

Simple and complex utilities, used more than once, should be moved in dedicated swiss-knife files. Some examples:

  • Format conversion w/ validation
    • Date/time parsing, checking the validity or null (from/to different formats)
    • Flag parsing, such as “Y”/”N”, into boolean or null
    • Enumerator to enumerator conversion
  • Temporary data read/write
    • cache
    • session

Service wrappers

Useful like a secret agent’s gadget, often a must have, are service wrappers, or just wrappers, which, according to the used language, can be a method, a class to extend, a function or a service; it’s just an algorithm, or a series of them, applied before and/or after every service HTTP call to manage data transform, success and error cases making simpler to route such behaviours in centralized and/or custom behaviours (raising exceptions, rejecting promises or running loaders). Some examples below:

  • Loading/spinning animations
  • Cache
  • Concurrent promises management (the Same service with same input called many times while it’s still pending)
  • Applying configuration Headers or inputs
    • 3rd party services auth keys
    • Identification headers
    • Headers or params for specific output format
  • Centralized error handling
    • Mapping logical error codes
    • Mapping HTTP codes
    • Automatic error presentation
      • Modal / popup / snack bar / redirects
      • HTTP code / i18n messages

My advice is to combine always wrappers and constant/config files to have the best reliable structure.

Questo post è disponibile anche in: Italian