Skip to content

General usage feedback (naming things, type checks) #365

@micahjon

Description

@micahjon

Last month I launched a silly, React-based side project with Microstates (anymammalsmilk.com) and overall had a great experience with it. Thanks in particular for your help with issue #342, which was resolved in only 9 hours!

Figured the least I could do was share some feedback as a Microstates newcomer that might make it easier to use for other folks down the road.

How to distinguish Microstate variable names from the values they represent?

For instance, I might pass a "votes" Microstate as a prop to a React component that calls valueOf and further transforms the resulting Array of votes. If "votes" is the Array, then what should I call the prop? If "votes" is the prop, how can I tell it apart from other, non-Microstate props?

In jQuery-land, this issue was solved by adding a $ to the beginning of jQuery elements (e.g. $pageHeader) to distinguish them from standard DOM Elements (e.g. pageHeader). Having a convention would make it easier to add Microstates to an existing codebase (with standard Arrays, Objects, etc.) and distinguish them from unwrapped values at a glance.

Would it make sense to enforce initial Microstate types?

When working within class methods, I would occasionally forget to unwrap a nested Microstate before setting it on a property. For instance:

class Person {
  constructor() {
    this.name = String;
    this.age = Number;
  }
  updateName(name) {
    return this.set({ name, age: this.age });
  }
}

let person1 = create(Person, { name: "Homer", age: 39 });
let person2 = person1.updateName("Micah");
// person2.age is now a Microstate object, not 39

In the (contrived) example above, it feels natural to set age to this.age, but we really need to use { age: valueOf(this.age) } or age will be set to an object instead of a number.

It might be out of the scope of this library, but it would be helpful if an error was thrown when values of incorrect types are set, so that programmer errors like this are more quickly fixed. Again, probably out of scope but it feels strange to be able to get away with this:

let num1 = create(Number, 5);
let num2 = num1.set("random string");

Just my two cents. Thanks for a great library, and happy to discuss any of this further!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions