Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFC/Discussion] Use Typescript enums, objects and interfaces for storing data. #43

Open
jglover opened this issue Oct 25, 2019 · 1 comment

Comments

@jglover
Copy link
Contributor

jglover commented Oct 25, 2019

When working with large data sets, I have found integrity and visibility to be a problem. It is very easy to make a mistake where an incorrect number is used. This is made more confusing by the fact there are numeric indexes as strings and it would be an easy mistake to zero-index the reference.

Since the project is already in Typescript, I'd like to propose that the data format switch from a JSON file to exported constants with interfaces and enums.
This has a few benefits:

  • Type safe (interfaces and enums)
  • Data integrity (data must be well defined, duplicates, typos etc are caught by linters and at compile time.
  • Higher visibility of data
  • Massively reduces refactoring workload and merge conflicts

Take for example:
clients/data/talks.json

{ 
  ...,
  "180": {
  "speakers": [187],
  "main_title": "Lightning launch - TakeShape",
  "alternative_titles": [],
  "categories": [15],
  "video_url": "https://youtu.be/ov9nZ2uBr2A",
  "video_upload_date": "2019-10-17",
  "conferences": [6]
 },
}

Now becomes
clients/data/talks.ts

export enum Speaker {
  'Example Person' = 'Example Person' , // string enum to make display easier.
  ...
}

export enum Category {
  React
}

export enum Conference {
  'Frontend Love Amsterdam 2018'
}

export interface IConferenceTalk {
  speakers: Speaker[],
  main_title: string
  alternative_titles: string[],
  categories: Category[],
  video_url: string,
  video_upload_date: Date,
  conferences: Conference[]
}

// Visibility of the relationships here is much improved
export const CONFERENCE_TALKS: {[number]: IConferenceTalk} = {
  "180": {
    "speakers": [Speaker.'Example Person'],
    "main_title": "Lightning launch - TakeShape",
    "alternative_titles": [],
    "categories": [Category.React],
    "video_url": "https://youtu.be/ov9nZ2uBr2A",
    "video_upload_date": new Date('2019-10-17'),
    "conferences": [Conference.'Frontend Love Amsterdam 2018']
   },
}

This would also make logic simpler in the React components that do the rendering, as they could just refer to myEnum[value] to get values.
Potentially JSON could still also be output at compile time or used as an input format to a parser than then validates the input, the downside to that option is it would still be easy to use an incorrect index.

If indexes were dropped from categories, speakers and conferences (possibly preserve them on conference talks), this would also massively reduce the number of merge conflicts and refactor work when there becomes a "race condition" on PRs using the same indexes.

I'd be interested in your thoughts

@EddyVinck
Copy link
Owner

@jglover Thanks for sharing your thoughts. I replied here to keep the discussion in one place.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants