S0Eric / YouTubeSolid-NodeProjectFromScratch

Ending source code from YouTube video
1 stars 0 forks source link

Why classes? #1

Open kostadinnm opened 3 years ago

kostadinnm commented 3 years ago

Hello. Nice series. I suggest opening discussions, as my question is rather a discussion one than an actual issue.

Since, you're trying to explain the benefits of typescript, there should be noted that those benefits need not carry some of the burdens of the Object Oriented Programming. Eric Elliott has a nice series on this @medium.

For example, here is your settings component, written in functional style, generating no typescript errors/warnings:

import { createStore } from "solid-js/store";

type StateType = {
  textColor: string;
  backgroundColor: string;
  buttonColor: string;
};

export const SettingsStore = () => {
  const getThemeSettingsLight: () => StateType = () => ({
    textColor: "black",
    backgroundColor: "white",
    buttonColor: "lightgrey"
  });
  const getThemeSettingsDark: () => StateType = () => ({
    textColor: "white",
    backgroundColor: "black",
    buttonColor: "darkgrey"
  });

  let [state, setState] = createStore(getThemeSettingsLight());

  return {
    state: state,
    setThemeLight: () => {
      setState(getThemeSettingsLight());
    },
    setThemeDark: () => {
      setState(getThemeSettingsDark());
    },
    setThemeCustom: (customColors: StateType) => {
      setState({ ...customColors });
    }
  };
};

I would shorten it even more, transforming the arrow functions, as some functional purists(e.g. D.Crockford) like the good-old function(). This will make it even clearer imho. I haven't actually tested the snippet, but I'd be glad if this grasps anyone's attention. I guess the author of solidjs should be aware of the pitfalls that class and other object-oriented stuff that is poured into javascript.That's why the examples there stay away from typescript for now. But since your series is mentioned in their "awesome" articles section, I thought it might be beneficial to point my thoughts out.

Edit: For the same arguments, I am staying away from generics, which cannot prove to beat the notion of composabilty of the primitive functions and objects top class citizens in javascript. If there's a great benefit of generics, other than easing migration from c#, it should be pointed. But the simplicity and the expressiveness of function/object composition should really be enough and set forward as a primer when showcasing a really innovative technology like solid..

Cheers, Kostadin

S0Eric commented 2 years ago

Thank you for your response. Sorry it took me so long to turn on discussions. I pasted your comment as a discussion and will respond there.