loading words...

May 08, 2019 22:11:20

Data and code

by @richardlynch | 361 words | 🐣 | 14💌

Richard Lynch

Current day streak: 0🐣
Total posts: 14💌
Total words: 4574 (18 pages 📄)

This post is best viewed at http://200words.rjldev.com/data_and_code.html where the formatting works

2019-05-08

I'm working on a project to manage visits to schools. We have a school object and this school object has many phases (primary, secondary, college). We have a wizard (shudder) for building up a school. One of the screens in the wizard is a group of check boxes for the school to select the phases they offer. This screen, like the other wizard screens, is backed by it's own form model. When the wizard is completed there's a service object to create a new School in the database from it's attributes.

class Phase < ApplicationRecord

validates :name, uniqueness: true

end

class School < ApplicationRecord

belongs_to :phase

end

class PhaseSelection

include ActiveModel::Model

include ActiveModel::Attributes

attribute :primary, :boolean

attribute :secondary, :boolean

attribute :college, :boolean

# validations etc ...

end

<%= form_for @phase_selection do |f| %>

<%= f.checkbox :primary %>

<%= f.checkbox :secondary %>

<%= f.checkbox :college %>

<%= f.submit %>

<% end %>

Can use see the design flaw? The lead dev on our project spotted it straight away, it was pretty impressive. The issue is that with this design we're storing business knowledge (the number of phases available) in both the code and the data, in both the columns and the rows. Doing this makes the application much harder to maintain. Now every time we create a phase in the database Phase.create! name: 'Nursery', we need to change our code if we want the phase to be selectable in the wizard.

class PhaseSelection

...

attribute :nursery, :boolean

attribute :primary, :boolean

attribute :secondary, :boolean

attribute :college, :boolean

end

<%= form_for @phase_selection do |f| %>

<%= f.checkbox :nursery %>

<%= f.checkbox :primary %>

<%= f.checkbox :secondary %>

<%= f.checkbox :college %>

<%= f.submit %>

<% end %>

This makes providing an admin screen to create phases impossible. We need to pick either storing the business knowledge (the number of phases available) in the code (think database columns) or in the data (think number of rows). If we choose both we'll eventually introduce inconsistent state into the application.

back

You can follow me on twitter @rjldev

Originally published at 200words.rjldev.com

  • 1

    @richardlynch I'd choose rows. What did you choose?

    (Plays theme music) find out next time on RJLDEV Words

    Craig Petterson avatar Craig Petterson | May 09, 2019 08:23:18
    • 1

      @craigpetterson In general I'd pick rows too. We've currently gone for both :-/ as we've got a dead line and haven't had time to fix the issue! The app is split into different namespaces and we only realised the row/column mix up yesterday when bringing the pieces together.
      We're paying down that tech debt after we've got this release out!

      Richard Lynch avatar Richard Lynch | May 09, 2019 10:23:16
  • 1

    @richardlynch - maybe you also need to write at 200linesofcodeaday.com ;-)

    Brian Ball avatar Brian Ball | May 08, 2019 17:20:34
contact: email - twitter / Terms / Privacy