Skip to content

Instantly share code, notes, and snippets.

@BiosBoy
Created August 9, 2024 06:18
Show Gist options
  • Save BiosBoy/9b693006d30992c6ff63deebe67b8450 to your computer and use it in GitHub Desktop.
Save BiosBoy/9b693006d30992c6ff63deebe67b8450 to your computer and use it in GitHub Desktop.
const maxUsers = 100;
const apiUrl = 'https://api.example.com/users';
// You know these won't change. They're constant.
let userCount = 0;
if (users.length > 0) {
userCount = users.length;
}
// Did we really need to use `let` here?
const userCount = users.length > 0 ? users.length : 0;
for (let i = 0; i < items.length; i++) {
console.log(items[i]);
}
let isUserLoggedIn = false;
if (loginSuccessful) {
isUserLoggedIn = true;
}
// Avoid: Mutates formattedAddress
let formattedAddress = "";
switch (user.country) {
case "USA":
formattedAddress = // Logic for formatting USA address here
break;
case "UK":
formattedAddress = // Logic for formatting UK address here
break;
default:
throw new Error("Unhandled country");
}
// Instead, use a function to handle the logic.
const formattedAddress = formatAddress(user);
// Function to format the address based on the user's country.
function formatAddress(user) {
switch (user.country) {
case "USA":
return formatUsaAddress(user); // Logic for formatting USA address here
case "UK":
return formatUkAddress(user); // Logic for formatting UK address here
default:
throw new Error("Unhandled country");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment