Untangling Frontend Spaghetti

Patrick Monslaup

Technical Manager & Developer
Knowit Experience Bergen

patrick.monslaup@knowit.no

Goal of the day

Be better at developing complex frontends

Part 1:
Creating less messy code

Part 2:
Eliminate existing monsters

Technical debt is a concept in software development that reflect the implied cost of additional rework caused by choosing an easy solution now instead of using a better approach that would take longer.

- wikipedia

Debt is paid back with interest

When you take a shortcut, you use your credit card

Only visible to developers.

Communicate technical debt to your project managers. 

Action #1

... Only then can they make an informed decision if they should use their "credit card"

Me Before Knowit

What is the problem with competitive programmers?

If you only measure speed then you incentivize writing ugly code without documentation. Be sure to measure the right things!”

Measure the right things

Action #2

... be mindful of what you measure,
you just might get it

Why am I talking about measurements when they clearly aren't the responsability of you as a developer?

Because they are your problem

Dev. vs Ops.

What happens when you have one team doing dev. and one team doing ops?

  • How are they measured?
  • What is the problem with these measurements?
  • How can you change the measurements or process to improve the situation?

Introspection

Which changes can be made to processes or the organization to mitigate the mess that inexperienced developers make?

  • Code Reviews
  • Pair programming
  • Mentorship
  • Technical onboarding
  • Team onboarding

Have a proper onboarding to the organization and project

Action #3

Hint: It's not about having the smartest people

Psychological safety

The best way to boost performance is to create an environment where everyone can contribute and reach their potential

Facilitate cooperation

Action #4

... both with managers and other devs

You're constantly paying interest on your technical debt, and the debt creates more debt

Clean as you go

Action #5

Actions to reduce the mess we create

  1. Communicate technical debt
  2. Measure the right things
  3. Have an onboarding process
  4. Facilitate cooperation
  5. Clean as you go

Part 2

Eliminate existing monsters

Case study

Online booking of health services through Storebrand Helse's BliFrisk app


We will examine filtering and fetching of providers

 

(some providers are in the sea due to
this being generated dummy data)

Filtering

MapScreen

filtering logic & data

Drawer

Layout of drawer

Fields

Reset filter

Data & callbacks for updating/resetting it

Initial data & update CB's

Reset fields callback

button

What happens when you can filter from many places?

Top of the map

Drawer

Referral form

MapScreen

filtering logic & data

Drawer

Layout of drawer

Fields

Navigation

Map

Hovering top filter

Referral flow

Reset filter

button

Drawer Fields

Hovering top filter

Referral flow

Views
Display data from stores

Stores
Store data

Filter data
e.g. "minRating"

set

get

Dedicated data store for filters

Components can set & get data from here

Components can observe the store

..and rerender when the data changes

Observer-pattern

Also known as publish-subscribe

Pattern #1

Instead of passing data around, store it a dedicated location and let components that need the data subscribe to changes.

 

This can easily be done in react with mobx
(or redux, or written yourself with listeners, or ...)

How should you pass functionality around?

Abstract reusable logic into controllers

Pattern #2

... the same way we put data into stores instead of sending it through many layers of components

Controllers

(E.g. "filterProviders")

Views
Display data from stores

Operate using data

from stores, and

Stores
Store data

Filter data
e.g. "minRating"

Providers

may update stores

set

get

get

MapView

Fetching providers

Non-functional requirements

  • Error logging
  • Authentication

Issue: doing this properly is a lot of code

Please don't put network logic in your views

Abstract it into a network layer.

 

Let the network layer handle authentication
and error logging

Make authentication reusable

Make good error logging easy

Consistent error logging is extremely nice for debugging

Abstract network layer
from your views. 

Pattern #3

... make error logging and authentication easy, otherwise you can't debug production issues

Benefit of abstractions

Loosely coupled abstractions allow mocking & developers can work in parallel on different modules

What do you do with the data from your API?

The providers originate from different online provider/booking API's which present data on different formats.

Text

Handle format differences backend.

What if you can't handle the format differences backend?

Our dependencies all rely on different coordinate systems

Create a coordinate model &
factory that transforms between formats

Coordinate model

Factory for transforming between formats

In better languages the solution is overloaded constructors

Create a model for your data.

If needed create a factory to transforms between formats

Pattern #4

Add models for complex data

Map data from services to your model

Tying it together

  • Stores allow you to have data sentralised and available in different parts of your application
  • Observer- or publish-subscribe pattern can be used to update views when stores change.
  • Controllers allow you to reuse logic across components and extract unnecessary boilerplate from views
  • Services should fetch data
  • Models keep your data easy to use
    • Factories transform raw data to models

MVC in frontend?

Originally implemented by Trygve Reenskaug in the 1970's for smalltalk

Actions

  1. Communicate technical debt
  2. Measure the right things
  3. Have an onboarding process
  4. Facilitate cooperation
  5. Clean as you go

Patterns

  1. Observer-pattern
  2. Abstract reusable logic into controllers
  3. Have a loosely coupled network layer
  4. Create models

Questions?

Untangling Frontend Spaghetti (outdated slides)

By Patrick Lid Monslaup

Untangling Frontend Spaghetti (outdated slides)

Complexity in frontend has steadily increased and with it components and views have become large and ugly beasts who I know have kept a lot of us up at night. In this presentation we will go through design patterns and ways we can structure code to make the monsters a little less scary. Examples are based on real projects and will be shown in React, but the principles are universally applicable regardless of framework.

  • 487