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 7610

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

Author
  • 60k
Author
Asked: November 28, 20242024-11-28T05:25:09+00:00 2024-11-28T05:25:09+00:00

Meaningful Names (Clean Code) — Why it is important for software developers?

  • 60k

Robert Martin (Uncle Bob) once said,

“Any fool can write code that computers understand. Good programmers write code that humans can understand”

Alt Text

Many of us take pride in ourselves when we provide a solution and write code for a complex problem, but what makes you a complete developer when you write code which your fellow developers can easily understand and giving meaningful names to the variables, functions and classes plays a vital role in that.

Let me tell you why?

I did understand this principle of clean code after a few years into professional coding when I struggled to understand my own code written just a few months ago. You must have us gone through a situation where you would like to prefer writing a fresh code for a bug fix or changes in the requirements instead of incorporate changes to existing code of other developers. These codes are technical debts to the team and organization and if you are also one of them who does not put deliberate effort to keep your code clean and followed the principles of clean codes, someone else down the line reading your code will feel the technical debt you have written that will increase the burden for maintainability, scalability and code debugging.

Providing meaningful names is one of the many principles of clean code and I feel providing meaningful names is the most important one.

Here are the rules for providing meaningful names

Naming Classes:
One class should carry only one responsibility. Hence this intent should reflect through the class name. A good rule of thumb while naming classes and methods is to think of nouns while naming class and verbs while naming methods.

Not Clean

Builder  Processor  WebsiteBO  Utility 
Enter fullscreen mode Exit fullscreen mode

Above class names do not tell what specific single responsibility it holds and hence becomes magnet class for other developers as they shove other responsibilities to these classes.

Clean

User  QueryBuilder  ProductRepository 
Enter fullscreen mode Exit fullscreen mode

Naming Method:
By method name reader should understand what is there inside the method and should have only one job to do.
Not Clean

send() get() export() 
Enter fullscreen mode Exit fullscreen mode

Clean

sendMail() getValidUser() exportZipFile() 
Enter fullscreen mode Exit fullscreen mode

Not Clean

//Code 1 Public Boolean sendSuccessMail( User user){   If(user.status == STATUS.active &&            isValidUserMail(user.mail)){                          //code for generating emailId,subject and email body      MailUtils.sendMail(mailId,subject,body);    } } ```   code 1 breaks 2 rules of clean code. It not only performs two responsibilities and but also its name does not indicate its jobs clearly. Which may cause lots of problem in future.  Let's look at the better version but still not clean   ``` Code 2 Public Boolean checkValidUserAndSendSuccessMail( User user){ If(user.status == STATUS.active && isValidUserMail(user.mail)){    //code for generating emailId,subject and email body    ....    MailUtils.sendMail(mailId,subject,body);   } } ```   Code 2 with method name `checkUserAndSendSuccessMail` is somewhat better in terms of clear intent, but it still carries more than one responsibility which is not good for code reusability and codebase may end up with many duplicate codes and in many places, you may need different logic for user validation or only mail send. And if there is any change in user validation code, it will not only impact user validation test cases but also send mail test cases.  Clean Code   ``` Public Boolean checkValidUser ( User user){   return user.status == STATUS.active &&                     isValidUserMail(user.mail) } Public Boolean sendSuccessMail( User user){   //code for generating emailId,subject and email body   ...   MailUtils.sendMail(mailId,subject,body); }  Boolean isValidUser = checkValidUser(user);    If(isValidUser){       sendSuccessMail(user)     } ```   `Warning signs for methods carrying multiple jobs: AND, OR, IF etc Avoid Abrr like isUsrRegis, regisDone`  ***Naming Boolean variable:*** Boolean variable should always ask questions and should be symmetrical when used in pair.  Not Clean   ``` send complete close login open/completed ```   if you declare these kinds of variable, the reader will face problem to understand the use of these variables later in code something like `if(login == true)`  Clean   ``` loggedIn isMailSent isValidationDone closed open/close (symmetric) ```    ***Conditions:*** Write implicit code around boolean variables  Not Clean   ``` If(isValidationDone == true) ```   Clean   ``` if( isValidationDone) ```   Not Clean   ``` Boolean isEligibleforDiscount; If(purchaseAmount >3000){    isEligibleforDiscount = true; }else{    isEligibleforDiscount = false } ```   Clean   ``` Boolean isEligibleforDiscount = purchaseAmount >3000; ```   Use positive conditions  Not Clean   ``` If(!isUserNotValid) ```   Clean   ``` If(isValidUser) ```   User ternary operator Avoid using “stringly-typed”/”hard-coded” value  Not Clean   ``` If(process.status == “completed”) ```   Instead of stringly typed value, we can use Enums and keep all the status of the process in a single place and we can get rid of typos with strongly typed and help maintain code base with a single place to change.  Avoid using magic numbers Not Clean   ``` If( employee.yearsWorked > 10){ } ```   replace constant or enums with magic number  Clean   ``` Int promotionEligibleYears = 10; If(employee.yearsWorked > promotionEligibleYears) ```   >“Programming is the art of telling another human what one wants a computer to do” — Donal Knuth   For more knowledge on the clean code principles, you can go through below listed resources  ***References & Resources:*** [Clean Code: Writing Code for Humans](https://www.pluralsight.com/courses/writing-clean-code-humans) [Clean Code: A Handbook of Agile Software Craftsmanship (Robert C. Martin)](https://www.amazon.in/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882)  Follow me [Twitter](https://twitter.com/imimtiyaz9)    [Linkedin](https://www.linkedin.com/in/md-imtiyaz-ahmed-092a6597/)  
Enter fullscreen mode Exit fullscreen mode

cleancodejavascriptprogrammingwebdev
  • 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

    How to ensure that all the routes on my Symfony ...

    • 0 Answers
  • Author

    Insights into Forms in Flask

    • 0 Answers
  • Author

    Kick Start Your Next Project With Holo Theme

    • 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.