vivian-mca / quote-generator

A generator that randomly pulls quotes from a Quotes API | Built with HTML, CUBE CSS methodology, and vanilla JavaScript
https://vivian-mca.github.io/quote-generator/
0 stars 0 forks source link

Code review #1

Open xtopolis opened 2 years ago

xtopolis commented 2 years ago

CSS

HTML

Javascript

Overall

vivian-mca commented 2 years ago

@xtopolis

CSS

xtopolis commented 2 years ago

This is not my area of expertise. My original comment was more in line with using them differently (which would make more sense in a larger project)

html {
  --14px: 0.875rem;
  --15px: 0.9375rem;
  --16px: 1rem;
  --17px: 1.0625rem;
  --18px: 1.125rem;
  --19px: 1.1875rem;
  --20px: 1.25rem;
  --21px: 1.3125rem;
}
h1 {
    font-size: var(--21px);
}

// use the vars in classes or existing elements with more semantic names
h2 {
    font-size: var(--20px);
}
h3 {
    font-size: var(--19px);
}

p {
    font-size: var(--14px);
}

.authorName {
    font-size: var(--14px);
}
.quoteText {
    font-size: var(--62px);
}
vivian-mca commented 2 years ago

@xtopolis

HTML

JS

xtopolis commented 2 years ago

JS

  • βœ… Refactored showLoadingSpinner function

We can go deeper

const showLoadingSpinner = (isHidden) => {
    loader.hidden = !isHidden;
    quoteContainer.hidden = isHidden;   
};
vivian-mca commented 2 years ago

@xtopolis

Question on IIFE:

xtopolis commented 2 years ago

Question on IIFE:

  • Is it common/standard practice to wrap async functions in IIFE?
  • What are commonly wrapped in IIFE?

You wrap your entire script file in the IIFE, not just this function.

If you were to share this script with someone as is, you'd be leaking/potentially-conflicting these variables into the global scope:

const quoteContainer = document.getElementById("quote-container");
const quoteText = document.getElementById("quote");
const authorText = document.getElementById("author");
const twitterBtn = document.getElementById("twitter");
const newQuoteBtn = document.getElementById("new-quote");
const loader = document.getElementById("loader");
//-------
let apiQuotes = [];
const showLoadingSpinner()...
const getQuote() ...
function newQuote() ....
const tweetQuote() ...

If we ignore the DOM selectors at the top for now (we'd refactor this/etc before sharing), you'd still have 5 javascript variable/function definitions with not-very-unique names that have a high chance of conflicting with other scripts on a page.

By wrapping it in an IIFE, the code still gets executed but in the function scope such that the variables aren't visible outside of that wrapped code (() => wrapped code)().

If you read the section on the module pattern, it might make more sense about how the variables are accessible within that function, but not to outsiders who try to access the "private variables" like .balance. It also shows that your entire script could be anonymous, or reveal only a top-level Object with various properties to call functions you expose, reducing your footprint from N variables to 1 variable in the global scope, and that the consumer could possibly rename if they so desired (they can assign the return value of the IIFE to whatever variable name they want)