5 simple changes that will make you a better JavaScript developer

Liam Hall
Liam Hall
Jun 17, 2020 · 5 min read
5 simple changes that will make you a better JavaScript developer

Introduction

Use descriptive names

let items = fetchItems()
let altItems = items.filter(item => item.featured == true)
altItems.forEach(item => {
item.icon = 'star'
})
let articles = fetchArticles()
let featuredArticles = articles.filter(article => article.featured == true)
featuredArticles.forEach(featuredArticle => {
featuredArticle.icon = 'star'
})

Extract common functions

articles.forEach(article => {
let date = new Date(article.createdAt),
day = date.getDate(),
month = date.getMonth()+1,
year = date.getFullYear()
article.formattedDate = `${day}/${month}/{year}`
})
projects.forEach(project => {
let date = new Date(project.createdAt),
day = date.getDate(),
month = date.getMonth()+1,
year = date.getFullYear()
project.formattedDate = `${day}/${month}/{year}`
})
const formatDate = (rawDate) => {
let date = new Date(rawDate),
day = date.getDate(),
month = date.getMonth()+1,
year = date.getFullYear()
return `${day}/${month}/{year}`
}
articles.forEach(article => {
article.formattedDate = formatDate(article.createdAt)
})
projects.forEach(project => {
project.formattedDate = formatDate(project.createdAt)
})
const formatDate = (rawDate) => {
let date = new Date(rawDate)
return `${date.getDate()}/${date.getMonth()+1}/{date.getFullYear()}`
}

Avoid nested conditional statements and return early

const getShopTradingStatement = (shop) => {
if (shop.isTrading) {
if (shop.openFrom < Date.now()) {
return `This shop is currently trading!`
}
else {
return `This shop will open it's doors from ${shop.openFrom}`
}
}
else {
return `This shop is no longer trading.`
}
}
const getShopTradingStatement = (shop) => {
if (!shop.isTrading) return `This shop is no longer trading.`
if (shop.openFrom < Date.now()) return `This shop is currently trading!`
return `This shop will open it's doors from ${shop.openFrom}`
}

Use look up tables

const userHasAccessPermissionsForTask = (userRole, task) => {
if (userRole == 'admin' && ['write', 'edit', 'read'].includes(task)) {
return true
}
if (userRole == 'editor' && ['edit', 'read'].includes(task)) {
return true
}
if (userRole == 'subscriber' && ['read'].includes(task)) {
return true
}
return false
}
const userHasAccessPermissionsForTask = (userRole, task) => {
const permissions = {
admin: ['write', 'edit', 'read'],
editor: ['edit', 'read'],
subscriber: ['read']
}
return permissions[userRole].includes(task)
}

Start sharing your code

Conclusion

The Startup

Get smarter at building your thing. Join The Startup’s +792K followers.

Sign up for Top 10 Stories

By The Startup

Get smarter at building your thing. Subscribe to receive The Startup's top 10 most read stories — delivered straight into your inbox, once a week. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Liam Hall

Written by

Liam Hall

Full stack web developer.

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +792K followers.

Liam Hall

Written by

Liam Hall

Full stack web developer.

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +792K followers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store