Doing It Wrong

learn in public

Site Navigation

  • Home
  • Books
  • Work & Play

Site Search

You are here: Home / Archives for Development

Form-Associated Custom Elements (FACE)

posted on January 8, 2024

I was using the static property formAssociated on some Lit -based web components I’m writing for a data-subsetting form at work. I’m bummed that it doesn’t click well with my brain. I get it, though—APIs are hard to design, and I can’t even imagine the level of skill needed to build the web component spec. I just wish it had been an attribute on a form input, not a static property on the web component class.

This is how you define a form-associated web component that can set its value on the parent form, using Lit element:

class SubsetterPart extends LitElement {
  static formAssociated = true; // (1)

  /*
    Look, you do your private class fields however you want
    when you're at home. In this house we use proper native
    private class fields.
  */
  #internals;

  #name = 'subsetter-part'

  constructor() {
    super();

    this.#internals = this.attachInternals(); // (2)
  }

  connectedCallback() {
    super.connectedCallback();

    this.setAttribute('name', this.#name) // (3)
  }

  @state();
  state = {};

  #handleChange(event) {
    const { value } = event.target

    this.#internals.setFormValue(value) // (4)

    /* set some state and stuff... */
  }

  /* The rest of your class stuff...including the form <input>s */
Code language: TypeScript (typescript)

This associates your custom element with a parent form (1), lets you access some shared parent state (2), and adds a name onto the host element (shadow host? whatever…the web component, itself) so that the parent form’s submit handler can read it from the FormData (3), then set’s that value through a method available on the shared state (4). A quick aside: learning enough about FACE web components to be able to type that sentence cost me three days (they felt so unproductive).

But why? In this <subsetter-part> custom element, why would I want the host to control whether it associates itself with a form? Well, that might be useful if you were making a web component that wanted to itself be thin wrapper around a form control—think maybe a custom temporal picker element that might be used to choose a temporal point [start] for subsetting data.

  /* The rest of your class stuff...including the form <input>s. */

  #validity = {
   /* Some instance state about what's a valid start date, end date. */
  }

  render() {
    return html`
      <label>
        <slot name="label">choose a date</slot>
        <input
          .value=${this.state.date}
          @change=${this.#handleChange}
          name="date"
          type="date"
          min=${this.validity.fromDate}
          max=${this.validity.toDate}
        >
      </label>
    `
  }
Code language: TypeScript (typescript)

If you’re vision of web components includes that use case, I’m happy. We share that vision. But if your vision of web components ends at that, I’m bummed.

What if I want to build a form control that is itself made up of a few form controls? Like what if I wanted to make a custom element that allowed me to select a temporal range [start, end] for that same data subsetting task?

I’m going to code this and you’re maybe going to have the objection that I could have built this with a series of the same “thin wrapper around a form control” components that I just wrote plus a container component, and you’re right. It’s not that I can’t get this done with the current API, it’s that it doesn’t click with my brain. Anyway, a few form controls like this:

  /* The rest of your class stuff...including the form <input>s. */

  #validity = {
   /* Some instance state about what's a valid start date, end date. */
  }

  render() {
    return html`
      <label>
        <slot name="start-label">choose a start date</slot>
        <input
          .value=${this.state.startDate}
          @change=${this.#handleChange}
          type="start-date"
          name="start-date"
          min=${this.validity.fromDate}
          max=${this.validity.toDate}
        >
      </label>

      <label>
        <slot name="end-label">choose an end date</slot>
        <input
          .value=${this.state.endDate}
          @change=${this.#handleChange}
          type="end-date"
          name="end-date"
          min=${this.validity.fromDate}
          max=${this.validity.toDate}
        >
      </label>
    `
  }
Code language: TypeScript (typescript)

So, altogether like this:

class SubsetterPart extends LitElement {
  static formAssociated = true; // (1)

  #internals;

  #name = 'subsetter-part'

  constructor() {
    super();

    this.#internals = this.attachInternals(); // (2)
  }

  connectedCallback() {
    super.connectedCallback();

    this.setAttribute('name', this.#name) // (3)
  }

  @state();
  state = {};

  #handleChange(event) {
    const { value } = event.target

    this.#internals.setFormValue(value) // (4)

    /* set some state and stuff... */
  }

  #validity = {
   /* Some instance state about what's a valid start date, end date. */
  }

  render() {
    return html`
      <label>
        <slot name="start-label">choose a start date</slot>
        <input
          .value=${this.state.startDate}
          @change=${this.#handleChange}
          type="start-date"
          name="start-date"
          min=${this.validity.fromDate}
          max=${this.validity.toDate}
        >
      </label>

      <label>
        <slot name="end-label">choose an end date</slot>
        <input
          .value=${this.state.endDate}
          @change=${this.#handleChange}
          type="end-date"
          name="end-date"
          min=${this.validity.fromDate}
          max=${this.validity.toDate}
        >
      </label>
    `
  }
Code language: TypeScript (typescript)

Whereas if formAssociated had been an attribute, this is how I’d imagine it being used:

class SubsetterPart extends LitElement {
  @state();
  state = {};

  #validity = {
   /* Some instance state about what's a valid start date, end date. */
  }

  #handleChange = (event) => {
    /* Some of the most amazing change-handling you've seen, admit it. */
  }

  render() {
    return html`
      <label>
        <slot name="start-label">choose a start date</slot>
        <input
          .value=${this.state.startDate}
          @change=${this.#handleChange}
          type="start-date"
          name="start-date"
          min=${this.validity.fromDate}
          max=${this.validity.toDate}
          formassociated
        >
      </label>

      <label>
        <slot name="end-label">choose an end date</slot>
        <input
          .value=${this.state.endDate}
          @change=${this.#handleChange}
          type="end-date"
          name="end-date"
          min=${this.validity.fromDate}
          max=${this.validity.toDate}
          formassociated
        >
      </label>
    `
  }
Code language: TypeScript (typescript)

In my magical greenfield world where such a thing is possible, the formassociated attribute would tell the parent form that this form control and some key associations should pierce through the Shadow DOM to a parent form. Maybe formassociated="some-form-id" could even be a thing, like form on a button, etc. Multiple levels of form -> Shadow DOM could have some common-sense use case like using the first available (but form attribute is a nice escape hatch). Because formassociated is on the input, any form-control-associated label can come along for the ride, too!

In the end, and like I was starting to say previously, I know this same thing could be build with the current API: I just need to chunk components differently. But that’s what I dislike! Sometimes a couple of form controls is just the right amount of atomicity. Sometimes I want to share that state and functionality in one class / custom element instead of two. And making everything a well-abstracted component takes time and boilerplate and more files. The hypothetical attribute formassociated lets me chunk my state / control my atomicity way better than the static property formAssociated.

Filed Under: Development Tagged With: Web Components

The Pragmatic Programmer: Chapter 6

posted on December 27, 2023

Concurrency

  • concurrency is when the execution of two or more pieces of code act as if they run at the same time: it is a software mechanism
  • parallelism is when they do run at the same time: it is a hardware concern
  • Concurrency requires that you run code in an environment that can switch execution between different parts of your code. This is often implemented using things like fibers, threads, and processes.
  • Almost any decent-sized codebase will need to handle concurrency: it’s just a real-world requirement.
  • temporal coupling happens when your code imposes a sequence of things that is not actually required to solve the problem at hand.
  • Shared state is the biggest liability once two things can happen at the same time.

Breaking Temporal Coupling

There are two aspects of time that are important to us: concurrency (things happening at the same time), and ordering (the relative positions of things in time). We should think about concurrency in our project by analyzing our workflow through an activity diagram.

  • drawn with rounded boxes for actions
  • arrow leaving an action leads to another action (A -> B means B must wait on A) or to a thick line called a synchronization bar
  • once all actions leading to a synchronization bar are complete, you can proceed along any arrows leaving it (to another action)
  • actions with no arrows leading in can be completed at any time.
  • this activity helps to identify activities that could be performed in parallel

Section Challenges

  • [ ] How many tasks do you perform in parallel when you get ready for work in the morning? Could you express this in a UML activity diagram? Can you find some way to get ready more quickly by increasing concurrency?

Shared State is Incorrect State

Scenarioizing servers in an restaurant selling pie (in a pie case) to their tables. Both see the last piece of pie and promise it to their table. One is disappointed.

  • The problem is not that two processes can write to the same memory; the problem is that neither process can guarantee that its view of that memory is consistent.
  • This is because fetching then updating the pie count is not an atomic operation: the underlying value can change in the middle.
  • Semaphores (a thing that only once process can own at a time) can solve this: only the server holding a plastic Leprechaun from the pie case can sell a pie.
  • trade-offs: this only works if all follow the convention
  • mitigation: move resource locking / semaphore handling into resource
  • multiple-resource transactions (pie and ice cream) should generally be a separate resource (so that you don’t have a server holding pie without ice cream)
  • non-transactional updates: concurrency problems aren’t restricted to writing to memory, but can pop up in files, databases, external services, etc.
  • whenever two or more instances of your code can access some resource at the same time, you have a potential for a problem
  • random failures are often concurrency issues
  • many languages have library support for mutexes (mutual exclusion), monitors, or semaphores
  • one could argue that functional languages (tendency to make all data immutable) make concurrency simpler, though they also run in the real world, subject to temporal restrictions, so you need to be aware of concurrency

Actors and Processes

Actors and processes offer a means to implement concurrency without the burden of synchronizing access to shared memory. Use actors for concurrency without shared state

  • an actor is an independent virtual processor with its own local (and private) state
  • each actor has a mailbox, whose messages get processed as soon as the actor is idle
  • there’s no single thing in control: nothing schedules what happens next, no orchestration of raw data to final output
  • the only state in the system is held in messages and the local, private state of the actor
  • all messages are one-way: there’s no built-in concept of replying
    • replies can be built by including a mailbox address in the message; make replying part of the message processing
  • processes each message to completion, and only processes one message at a time
  • a process is typically a more general-purpose virtual processor, often implemented by the operating system to facilitate concurrency

The diner / server scenario with actors:

  • the customer becomes hungry
  • they respond by asking the server for pie
  • the server asks the pie case for pie
  • if available, pie case sends pie to customer and notifies waiter to add to bill
  • if not available, pie case informs server

In all of the code for this, there’s no explicit concurrency handling, as there is no shared state. Erlang language and runtime are a great example of an actor implementation. Erlang calls actors processes, but they’re not regular operating system processes as described in this chapter’s notes. Also has a supervision system that manages process lifetimes.

Section Challenges

  • [ ] Do you currently have code that uses mutual exclusion to protect shared data? Why not try a prototype of the same code written using actors?
  • [ ] The actor code for the diner only supports ordering slices of pie. Extend it to let customers order pie a la mode, with separate agents managing the pie slices and the scoops of ice cream. Arrange things so that it handles the situation where one or the other runs out.

Blackboards

Blackboards provide a form of laissez faire concurrency, where the blackboard is the storage repository for independent processes, agents, actors, etc. They may be a good fit when writing an application with many sometimes independent and sometimes inter-dependent steps. Use them to coordinate workflows. Imagine writing an application to process loan applications.

  • responses (to credit inquiries, bank account balances, etc.) can arrive in any order.
  • data aggregation may be done by many different people, distributed across different time zones
  • some data gathering may be done automatically by other systems; data may arrive asynchronously
  • certain data may be dependent on other data (e.g., cannot start a car’s title search until the system has proof of ownership)
  • the arrival of new data raises new questions and policies; e.g., bad result from a credit check means stricture requirements for down payment

Messaging systems (think NATS, SQS, Kafka) can be like blackboards, since they offer message persistence and the ability to retrieve messages through pattern matching. Using blackboards comes with trade-offs:

  • harder to reason about business logic because everything is disconnected
  • observability can suffer unless you implement a system
  • agreeing on a communication format (or at least having a repository of different formats) requires work
  • more troublesome to deploy as there are more moving parts

Section Challenges

  • [ ] Exercise 24 Would a blackboard-style system be appropriate for the following applications? Why or why not?
  • Image processing: you’d like to have a number of parallel processes grab chunks of an image, process them, and put the completed chunk back.
  • Group calendaring: you’ve got people scattered across the globe, in different time zones, speaking different languages, trying to schedule a meeting.
  • Network monitoring tool: the system gathers performance statistics and collects trouble reports, which agenst use to look for trouble in the system.
  • [ ] Do you use blackboard systems in the real world—the message board by the refrigerator, or the big whiteboard at work? What makes them effective? Are messages ever posted with a consistent format? Does it matter?

Filed Under: Development Tagged With: Book, Notes

The Pragmatic Programmer: Chapter 5

posted on December 20, 2023

Bend, or Break

Make every effort to write code that’s as flexible as possible.

Decoupling

  • Coupling is the enemy of change, because it links together things that must change in parallel.
  • Software is not bridges—we don’t want components to be rigidly coupled together, or else change one component requires changing its coupled components.
  • Coupling is transitive: A -> B, B -> C means A -> C.
  • Decoupled code is easier to change (ETC).
  • Symptoms of coupling
  • dependencies between unrelated modules / libraries
  • simple changes to one module propagates changes through unrelated modules (or breaks)
  • developers are afraid to change code because they don’t know what will be affected
  • meeting where everyone has to attend because nobody is sure who will be affected by a change

Train Wrecks

public void applyDiscount(customer, order_id, discount) {
      totals = customer
          .orders
          .find(order_id)
          .getTotals();
      totals.grandTotal = totals.grandTotal - discount;
      totals.discount   = discount;
}Code language: JavaScript (javascript)

That code transverses five levels of abstraction, from customers to total amounts. We have to know that customer exposes orders that have a .find() method, etc., all the way down. That’s a lot of implicit knowledge and things that cannot change in the future.

Tell, don’t ask. Don’t make decisions based on the internal state of an object and then update that object. This is a pattern, not a law of nature, so don’t follow it slavishly.

public void applyDiscount(customer, order_id, discount) {
      totals = customer
          .findOrder(order_id)
          .applyDiscount(discount);
}Code language: JavaScript (javascript)

The Law of Demeter expressing the following sentiment in a more detailed way (LoD probably not very relevant today): don’t chain method calls. This “one-dot rule” doesn’t apply if the things you’re chaining are very unlikely to change (like language-level features). The following Ruby code doesn’t violate the one-dot rule, because it’s language-level.

people
  .sort_by {|person| person.age }
  .first(10)
  .map {| person | person.name }

Pipelines are not method-call chains: pipelines transform data, passing it from one function to the next. We’re not relying on hidden implementation details.

  • [ ] How does “tell, don’t ask” strike you?
  • [ ] Do you think the “one-dot rule” is practical? Could it be helpful?

The Evils of Globalization

  • Global data is coupling, as you never know what will break if you change it. Reuse should not be your primary concern when writing code, but the thinking that makes code reusable should be in your mind as you create it. Avoid global data—it slows you down.
  • Singletons are global data, though at least they have intelligence behind Confg.getLogLevel() that can help you not break calling code.
  • Any mutable external resource is global data. You can’t avoid using a database, so you can minimize the impact of global data by wrapping these resources behind code you control.
  • If it’s important enough to be global, wrap it in an API.
  • [ ] What are appropriate uses of global data?

Inheritance Adds Coupling

Subclassing just isn’t shy: it doesn’t deal with only it’s own concern. Alterations in one place (the parent class) can change the subclass elsewhere.

  • [ ] How does this strike you? Do you prefer to work in an OOP mental model / language?
  • [ ] Can you imagine using OOP without subclassing?

Juggling the Real World

Events represent the availability of information. They can come from external or internal sources. When we write applications that response to events, here are are few strategies:

  • Finite State Machines
  • The Observer Pattern
  • Publish / Subscribe
  • Reactive Programming and Streams

Finite State Machines

  • There exist a limited number of states for your application. You can be in one state at any given time.
  • Events move you from one state to another.
  • Actions can be triggered upon moving.
  • [ ] What do you think about using a FSM in your next application? Have you ever used one before?

The Observer Pattern

  • source of events is the observable
  • observers are watching the observable
  • fairly simple pattern: push a function reference into a list, and call those functions when the event occurs
  • because the observers have to register with the observable, you introduce coupling
  • callbacks are handled synchronously, so you have more opportunity for performance bottlenecks

Publish / Subscribe (PubSub)

  • generalizes the observer pattern, dealing with coupling and performance bottlenecks
  • publishers and subscribers are connected via channels (the how is an implementation detail hidden from your application logic)
  • subscribes register to 1 or more channels
  • publishers write to channels
  • good choice for decoupling the handling of asynchronous events
  • observability is hard with such a distributed system
  • is a good example of reducing coupling by abstracting up through a shared interface (the channel)

Reactive Programming

Reactive programming, as a paradigm, is often compared to using a spreadsheet: you change one value, and other values reactively update. Reactivity can be created with events, but streams build reactivity in. RxJS is a good example of this paradigm. Event streams unify synchronous and asynchronous processing behind a common API.

Section Challenges

  • [ ] Exercise 19 In the FSM section we mentioned that you could move the generic state machine implementation into its own class. That class would probably be initialized by passing in a table of transitions and an initial state. Try implementing the string extractor that way.
  • [ ] Exercise 20 Which of these technologies (perhaps in combination) would be a good fit for the following situations:
  • If you receive three network interface down events within five minutes, notify the operations staff.
  • If it is after sunset, and there is motion detected at the bottom of the stairs followed by motion detected at the top of the stairs, turn on the upstairs lights.
  • You want to notify various reporting systems that an order was completed.
  • In order to determine whether a customer qualifies for a car loan, the application needs to send requests to three backend services and wait for the responses.

Transforming Programming

All programs transform data, yet we rarely thing about creating transformations when designing software. There’s great value in thinking about programs as being something that transforms inputs into outputs—like an industrial assembly line.

  • think of the Unix philosophy
  • programming is about code, but programs are about data
  • break down your program into transform |> transform, then repeat
  • even if your language doesn’t support pipes, you can still use the philosophy of design
const content = File.read(fileName);
const lines = findMatchingLines(content, pattern);
const result = truncateLines(lines);Code language: JavaScript (javascript)
  • the reason transforms are worthwhile is that instead of hoarding state (encapsulation in objects), you pass it around—you lose a whole category of complexity and coupling
  • data becomes a flow…a peer to functionality
  • error handling can be done with either:
  • an :ok/:error tuple (I like [error, data]), handled inside each transformation
  • handle it in the pipeline (some kind of andThen function that only continues if no error)

Section Challenges

  • [ ] Exercise 21 Can you express the following requirements as a top-level transformation? That is, for each, identify the input and the output.
  1. Shipping and sales tax are added to an order
  2. Your application loads configuration information from a named file
  3. Someone logs in to a web application
  • [ ] Exercise 22 You’ve identified the need to validate and convert an input field from a string into an integer between 18 and 150. The overall transformation is described by
  field contents as string
    -> [validate & convert]
      -> {:ok, value} | {:error, reason}Code language: JavaScript (javascript)

Write the individual transformations that make up validate & convert.

  • [ ] Exercise 23 In _Language X Doesn’t Have Pipelines, on page 153 we wrote:
  const content = File.read(file_name);
  const lines = find_matching_lines(content, pattern);
  const result = truncate_lines(lines);Code language: JavaScript (javascript)

Many people write OO code by chaining together method calls, and might be tempted to write this as something like:

  const result = content_of(file_name)
                  .find_matching_lines(pattern)
                  .truncate_lines(lines)Code language: JavaScript (javascript)

What’s the difference between these two pieces of code? Which do you think we prefer and why?

Inheritance Tax

  • two types of inheritance (from two origins):
  • Simula, where inheritance was a way of combining types
  • Smalltalk, where inheritance was a dynamic organization of behaviors
  • both types have the issue of coupling code
  • alternatives to inheritance:
  • interfaces and protocols, which allow us to
  • delegation
  • mixins and traits
  • delegate to services: has-a trumps is-a

Section Challenges

  • [ ] The next time you find yourself subclassing, take a minute to examine the options. Can you achieve what you want the interfaces, delegation, and / or mixins? Can you reduce coupling by doing so?

Configuration

Parameterize your application by using external configuration. Common configurable data:

  • credentials for external services
  • logging levels and destinations
  • ports, IP addresses, machine names, cluster names
  • environment-specific validation parameters
  • externally-set parameters (like tax rates)
  • site-specific formatting details
  • license keys

You could structure this as a flat-file off-the-shelf plain-text document (that works). You can also store it in a database table if it is likely to be changed by the customer. You can also do both!

Consider putting your configuration data behind a thin API:

  • multiple applications can share configuration data (with appropriate authN and authZ)
  • configuration changes can be made globally
  • configuration data can be made via a specialized UI
  • configuration data become dynamic (no application restart necessary)

As with all things, don’t overdo it. You can have too much configuration.

Filed Under: Development Tagged With: Book, Notes

  • « Previous Page
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • …
  • 9
  • Next Page »

Profile Links

  • GitHub
  • Buy Me a Coffee?

Recent Posts

  • Event Listeners
  • A Philosophy of Software Design
  • The Programmer’s Brain
  • Thoughts on Microservices
  • API Design Patterns

Recent Comments

No comments to show.

Archives

  • May 2025
  • September 2024
  • July 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • December 2022
  • December 2021

Categories

  • Development