How to write better code — functions and methods 👩‍🔬 ✅ 💯

Here are 4ways to improve the way you code by writing cleaner and better to understand methods/functions

daboigbae
3 min readSep 21, 2022
Stop writing code like this

Elevate your coding game with our FREE JavaScript Reference Guide! Dive in and upgrade your skills today! Download Now 💪🚀

Function names should be verbs 🚶

If variables are nouns, functions should be verbs and their name should should tell what they’re doing to the data in question

  • ✅ Do this const getArrayFromObject()
  • ❌ Don’t do this const arrayFunction()

Functions should do 1 thing 🔢

Code should read like a book. That means every file you write is a page in your book, and every function a small paragraph. Make sure it’s concise and to the point.

✅ Do this
❌ Don’t do this

Use named variables for parameters 📛

Ever been calling a function that has like, 10 parameters? Things get messy and if you’re like me, it’s hard to keep track of so many parameter names. Was the name parameter first or third on the list? That’s why I to use named variables for all of my functions.

  • ✅ Do this const getData({userId, username, password})
  • ❌ Don’t do this const getData(userId, username, password)

Aim for 2 parameters at most 🥈

With the theme or remember the parameters you pass to a function, keeping them to the lowest number you can, helps with keeping things organized. You won’t have to remember a long list of variables as you call your project’s most important methods

  • ✅ Do this const handleLogin({userInformation, callback})
  • ❌ Don’t do this const handleLogin({username, email, password, callback})

Closing thoughts

How do you write better code? Do you have any advice for new developers trying to write better code?

👋 Hey, thanks for sticking around till the end!

If you’re vibing with what you’ve read and wanna keep the good times rolling, I’ve got just the spot for you. This is just a taste of what you can find over at Digital Art Dealers.

We’re talking coding tips, startup stories, AI insights, and much more. It’s like your favorite late-night rabbit hole, but better. 🎉

But hey, don’t just take my word for it. Click here and see for yourself! Who knows, you might just find your next obsession waiting for you. 💻🚀

See you on the other side! 🤘

--

--

daboigbae

🇲🇽 I'm on Medium to share programming memes and smoke weed... and I'm all out of weed...