Skip to content

Instantly share code, notes, and snippets.

@alex-wilmer
Created May 15, 2023 20:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alex-wilmer/78b60c94e49fbe1e12b1678d76344908 to your computer and use it in GitHub Desktop.
Save alex-wilmer/78b60c94e49fbe1e12b1678d76344908 to your computer and use it in GitHub Desktop.
Casey First Lesson Notes

Main issue

  • Why doesn't setting the cookie in the auth middleware work?

My Sleuthing

Main Improvements

Small Improvements

Setting Up Globals

  • Put things like environment variables in a single place and import them
// config.js
import dotenv from 'dotenv'
dotenv.config()

export default {
  jwtSecret: process.env.JWT_SECRET
}
...
import config from './config'
config.jwtSecret

Put Fail Cases First

  • why? possibly improved readabiliy, definitely improved debugging
  • code execution will be linear, matching the way the function is written
// fail case last
if (a && b) {
  return a > b;
}
return false;
// fail case first
if (!a || !b) return false;
return a > b;
  • If it's a lot of variables you could write this as:
if ([a, b, c, d].some((x) => !x)) return false;

You can make a helper function for this:

function checkFalsey(...args) {
  return args.some((x) => !x);
}

if (checkFalsey(a, b, c, d)) return false;

This is true for JSX too:

// fail case first
if (!loggedIn()) return <Component />;
return <App />;

UX Improvements

  • visual event & error handling
    • currently I'm only seeing console.log(error)
    • ex: loading spinners, toast notifications

Debugging notes

  • use lsof to check for open ports
  • don't re-use npm script names
  • use different console.log titles to be more clear where issues are arising
    // use numbers
    console.log(123, value);
    // use object shorthant
    console.log({ value });

Longer term things

@soojeen
Copy link

soojeen commented May 15, 2023

remove redunant ternaries
a = b ? b : c -> a = b || c

i recommend the nullish coalescing operator. it more accurately represents the intention of most cases. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing (careful refactoring code with it, because falsey values like 0 and '' will behave differently)

@soojeen
Copy link

soojeen commented May 15, 2023

shouldn't use nested ternaries in JSX
this may be personal preference, but it's a rule I enforce

agree. but it's less a style issue and more likely a symptom of your component having too many boolean props.

recommend refactoring the component to accept a variant prop which accepts a descriptive string enum.
see https://chakra-ui.com/docs/components/alert for example. they use two different variant props, status and variant. this example's variants are mostly style related, but hopefully you can see how to extend this idea into interactive or rendering logic.

@alex-wilmer
Copy link
Author

alex-wilmer commented May 15, 2023

@soojeen 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment