Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 3471

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 61k
Author
Asked: November 26, 20242024-11-26T03:01:07+00:00 2024-11-26T03:01:07+00:00

The importance of being descriptive when naming

  • 61k

There are only two hard things in Computer Science: cache invalidation and naming things.
Phil Karlton

TL;DR: Don't overthink and just be as descriptive as you can when naming code.

Naming things is hard. For example. When my wife was pregnant with our daughter we agonised for months over her name before coming up with her name and we still aren't sure if we picked right. What if she doesn't like her name when she grows up?

Naming code can almost seem just as hard. We think 'there has to be a perfect name to give this code justice'. We think that it has to be clever, memorable. concise and snappy.

Some common pitfalls

  • We can try to be too clever and end up with some obscure name
const exoticOptimiser = () => {} 
Enter fullscreen mode Exit fullscreen mode

  • Or we can try to use some clever reference.
const iWantToBeTheVeryBest = () => {} 
Enter fullscreen mode Exit fullscreen mode

  • We also see a lot of shorthand. Some are acceptable like event-handlers eg.
(e) => {} // e for 'event' 
Enter fullscreen mode Exit fullscreen mode

  • Some are just pure laziness
const myLazyFunction = (a, b) => {} 
Enter fullscreen mode Exit fullscreen mode

But all jokes aside poor naming has terrible consequences. I've seen some horrid naming in my short career, as I'm sure all of us have. Debugging and working with poorly named code is an avoidable nightmare.

The importance of good naming

It's very likely that at some point in the future you won't be around to explain why you named a function in a certain way.

Even if you are still there, you may forget why a function or some code was programmed in a certain way.

If the name was formulated poorly there is a large likelihood you may lead the developer down rabbit holes.

For example, what would you think if you saw this function?

const calcAB = (a,b) => {} 
Enter fullscreen mode Exit fullscreen mode

Calculate what? Is it even calculate? What is this function doing with a and b?

Sure you can read the code, but what if the code is 200 lines long? You've already wasted an opportunity to clearly identify the purpose and functionality of the function.

Imagine instead you saw this:

const calculateTheDistanceBetweenPoints = (pointA, pointB) =>  
Enter fullscreen mode Exit fullscreen mode

Sure, it's much longer but it's readable and clear and descriptive. Even naming the parameters as pointA is so much more clear than just saying a and b.

What I think is the right way

Naming code isn't like naming a baby. You don't know how the baby will turn out once they grow up! But you know exactly what the code is supposed to do (or at least I hope you do!). You have all the ingredients to write a good name.

My 2c is that it's best to be as descriptive as you can. Describe the code.

  • If it is a variable call it exactly what it is.
  • If it is a variable describe what it's supposed to do.

Forget being concise. Focus on being precise.

You may end up with names that are quite long. But this is so much better than a name that is concise but obscure.

If it is too obscure and you can't think of a good name, maybe it is actually indicating code smell and your function is not very good. If so great! You should refactor 🙂

Steps that I follow

Some steps that I follow to make sure that naming leaves a legacy and not a nightmare are:

  1. Be descriptive about what the code does.

  2. Review the name and make sure that it can't be interpreted in multiple ways.

  3. For especially complicated functions, I specifically ask for feedback about the name in code reviews.

Conclusion

Write names that are descriptive, even if it is long! Remember that code is supposed to be readable – if it isn't it is bad code and naming is huge factor in determining if code is readable.

programmingwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    ES6 - A beginners guide - Template Literals

    • 0 Answers
  • Author

    Understanding Higher Order Functions in JavaScript.

    • 0 Answers
  • Author

    Build a custom video chat app with Daily and Vue.js

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.