<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Caroline Rozali</title>
    <description>The latest articles on DEV Community by Caroline Rozali (@hyfydistro).</description>
    <link>https://dev.to/hyfydistro</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F581411%2F578eff0b-73d1-4a0d-b225-b5ca9a85461b.png</url>
      <title>DEV Community: Caroline Rozali</title>
      <link>https://dev.to/hyfydistro</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/hyfydistro"/>
    <language>en</language>
    <item>
      <title>Intro to Naming Guidelines: Writing Meaningful Variable Names</title>
      <dc:creator>Caroline Rozali</dc:creator>
      <pubDate>Sun, 19 Feb 2023 08:43:30 +0000</pubDate>
      <link>https://dev.to/hyfydistro/intro-to-naming-guidelines-writing-meaningful-variable-names-13j5</link>
      <guid>https://dev.to/hyfydistro/intro-to-naming-guidelines-writing-meaningful-variable-names-13j5</guid>
      <description>&lt;p&gt;For me, naming a variable is similar to giving a baby a name. It could tell us a bit about who they are or what they will become. I overthink about picking out the right one. It is not just variables. There are also functions, arguments, classes, packages, source files, and directories. Many programmers have acknowledged the difficulty in naming things, yet few use explicit guidelines. First, we will address the benefits of using a guideline, and then we will go over some naming guidelines, some borrowed from &lt;a href="https://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882" rel="noopener noreferrer"&gt;Clean Code&lt;/a&gt;. A solid foundation will help us get started.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvocu4sjdjo26mwut34ez.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvocu4sjdjo26mwut34ez.png" alt=" " width="720" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The Benefits in Making Names Meaningful
&lt;/h2&gt;

&lt;p&gt;How many of us have been stopped and surprised by finding a unique name for a certain variable or function? A famous engineer once said:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;There are only two hard things in Computer Science: cache invalidation and naming things&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;But why is it so important to have things named properly?&lt;/p&gt;

&lt;p&gt;Chances are you know the struggle when you have had thoughts like “What does this name even mean?”, “What’s the difference between this and that?”, or “What does this actually do?”. Few of us know the value of having intentional names. The weight of it carries over into the future. It can negatively affect you and your team. If not, the reverse effect will result in improvement in the following areas:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Consistency&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Familiarity with your code creates confidence. A naming guideline encourages that expectation. This is especially potent in a team environment. A standard is something we want to use, even as a general guideline. Otherwise, we’d be picking each other’s naming abilities and having the discussion during the code review or halfway through the development.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Readability&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You relieve yourself of cognitive load when you understand something. No one likes to get their head in a twisty. By having clarity, you are effectively communicating the purpose of your code and your teammate will thank you.&lt;/p&gt;

&lt;p&gt;I was given a task to do some rework on an existing codebase when I found myself stuck and staring at a name. I didn’t understand there until I looked deeper into the code’s implementation. Why couldn’t it have been self-explanatory in the first place?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Productivity&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;No prior guidelines or thought put into naming will hold you back from finishing the project or task.&lt;/p&gt;

&lt;p&gt;You reread the code implementation to figure out what's going on. More time passes than is required. It turns out that failing to take the time to properly name your software will double in the long run.&lt;/p&gt;

&lt;p&gt;Don’t underestimate the impact of good titles or names. Having good names increases code integration and removes misunderstandings, which in turn improves other areas, such as code maintainability and code reuse!&lt;/p&gt;

&lt;h2&gt;
  
  
  Starter Kit for Naming Guidelines (and Best Practices)
&lt;/h2&gt;

&lt;p&gt;To model a naming guideline we need to know how names are designed. This require heaps of experience in programming, _especially _with a team.&lt;/p&gt;

&lt;p&gt;Fortunately, there are already few great models out there we can get some exposure in order to build our foundation upon.&lt;/p&gt;

&lt;p&gt;Naming guidelines are meant to be shared and used upon in the codebase. They are an agreement on what to use and not use.&lt;/p&gt;

&lt;p&gt;Here are my takeaways with Clean Code and other articles which provides some excellent points to go follow.&lt;/p&gt;

&lt;h3&gt;
  
  
  Use Intention-Revealing Names
&lt;/h3&gt;

&lt;p&gt;Think big questions for variable, function or class. Answer the following questions: “why it exists”, “what it does”, and “how it is used”.&lt;/p&gt;

&lt;h3&gt;
  
  
  Change Names Again When you Found its Real Purpose
&lt;/h3&gt;

&lt;p&gt;We’re not done when we have chosen our name and written the code. Look back and see if yesterday’s code make sense today. With new information and features coming, the purpose of the code in examination may change.&lt;/p&gt;

&lt;h3&gt;
  
  
  Avoid Disinformation and Noninformation
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;Information&lt;/em&gt; has consistency in naming and often uses existing knowledge to aid its naming. We don’t want to always pull out a dictionary or look up the term in our browser. &lt;em&gt;Disinformation&lt;/em&gt; is not directly answering what the code is intended to do. Hence, it lacks meaning and transparency. Meanwhile, &lt;em&gt;noninformation&lt;/em&gt; does &lt;strong&gt;not&lt;/strong&gt; have a meaning or clue.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Abbreviation can be disinformative.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In general abbreviation is discouraged.&lt;br&gt;
e.g. &lt;code&gt;c&lt;/code&gt; can stand for "Carbon", "Celsius" or "Century"&lt;/p&gt;

&lt;p&gt;Names that intertwine with existing programming concepts is confusing.&lt;/p&gt;

&lt;p&gt;e.g. &lt;code&gt;str&lt;/code&gt; can mean "strength" or, in programming terms, "string". It will vary in different project scope, but when you are making an RPG game you may have this conflict.&lt;/p&gt;

&lt;p&gt;However, I would make an exception for a list being looped.&lt;/p&gt;

&lt;p&gt;e.g. &lt;code&gt;cards&lt;/code&gt; is an array and I only expect an item.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cards.map((c) =&amp;gt; c);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;c&lt;/code&gt; could be rewritten as card but it is already implied.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;zero &lt;code&gt;0&lt;/code&gt;, lowercase &lt;code&gt;o&lt;/code&gt;, uppercase &lt;code&gt;O&lt;/code&gt;, and lowercase l are truly horrible variable names.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It's difficult to tell the difference between the number zero, the letter "o", uppercase and lowercase. It's the same with the lowercase letter "l" and the number &lt;code&gt;1&lt;/code&gt;. More so when combined!&lt;/p&gt;

&lt;p&gt;e.g.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let a = l;  
if ( O == l )  {
  a = O1;
} else {
  l = 01;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Number series naming is noninformative.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;They might as well be written in hieroglyphs.&lt;/p&gt;

&lt;p&gt;e.g.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Array.prototype.swap = function (x1, x2) {
  var b = this[x1];
  this[x1] = this[x2];
  this[x2] = b;
  return this;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;x1&lt;/code&gt; could have been &lt;code&gt;source&lt;/code&gt; and &lt;code&gt;x2&lt;/code&gt; could have been &lt;code&gt;target&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Make Meaningful Distinction
&lt;/h3&gt;

&lt;p&gt;When met with similar concepts, which are frequently given a very general name, you may be tempted to use it everywhere else with a slightly tweaked version. But you can’t (and that’s just lazy naming).&lt;/p&gt;

&lt;p&gt;Make further distinctions when you come across the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Plural ending with “s”&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;e.g. The plural of &lt;code&gt;apple&lt;/code&gt; is only one letter different from &lt;code&gt;apples&lt;/code&gt;. With the untrained eyes, programmers can miss this marginal detail. They can be solved by making them into Collective Nouns. Think of something like &lt;code&gt;basketOfApples&lt;/code&gt; or &lt;code&gt;bunchOfApples&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Noise Words — Same Meaning, Different Spelling&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Don’t even use the same term for two different purposes.&lt;/p&gt;

&lt;p&gt;e.g. What is the difference between &lt;code&gt;totalOfCash&lt;/code&gt; and &lt;code&gt;amountOfIncome&lt;/code&gt;? They both mean the same thing. Figure out the specificity and differentiate.&lt;/p&gt;

&lt;p&gt;Indistinct Noise Words &lt;/p&gt;

&lt;p&gt;e.g. Attaching words like these articles like &lt;code&gt;a&lt;/code&gt;, &lt;code&gt;an&lt;/code&gt;, or &lt;code&gt;the&lt;/code&gt; don't mean anything more different when you are simply writing the name of it. There's nothing wrong with using them. It is just useless in a case like &lt;code&gt;thePerson&lt;/code&gt; or &lt;code&gt;aPerson&lt;/code&gt;. Make names differ by more than one or two letters.&lt;/p&gt;

&lt;p&gt;Similarly with member prefixes you don't need them when your class and functions are small. As developers spend more time reading the code they ignore the prefix or suffix to see the meaningful part of the name. &lt;br&gt;
e.g.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function sendEmail(input) {
 const a_txt;
 setMessage(a_txt + input)
}
_________________________________________________ 
function sendEmail(input) {
 const text;
 setMessage(text + input)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A more cut out version of noise words is when there is no other words attach to further describe what is they are. Like &lt;code&gt;total&lt;/code&gt; and &lt;code&gt;amount&lt;/code&gt; are still similar and share the same meaning except on their own it doesn't say much. "Total" of what? "Amount" of what?&lt;/p&gt;

&lt;h3&gt;
  
  
  Use Pronounceable Names
&lt;/h3&gt;

&lt;p&gt;When you enter the tech field, you’d be surprise that programming is a social activity. So if you can’t pronounce the variable you are referring to, you will not be able to have a discussion without sounding silly!&lt;/p&gt;

&lt;h3&gt;
  
  
  Use Searchable Names
&lt;/h3&gt;

&lt;p&gt;The search bar in your code editor can narrow down results when names become more unique. This is essential when tasked in refactoring the codebase.&lt;/p&gt;

&lt;p&gt;Here’s how we can make names more searchable:&lt;/p&gt;

&lt;p&gt;Use longer names over shorter names&lt;br&gt;
Single-letter names results in maximum ambiguity. Longer names acts as a better identifier and gives enough context.&lt;/p&gt;

&lt;p&gt;There is an exception: single-letter names are okay in local variables inside short methods. See example in &lt;strong&gt;Avoid Disinformation and Noninformation&lt;/strong&gt; in point “Abbreviation can be &lt;em&gt;disinformative&lt;/em&gt;”, where an array is mapped and what items are already implied.&lt;/p&gt;

&lt;p&gt;Use constant naming convention over raw values&lt;br&gt;
e.g. MAX_PASSENGER_ON_BOARD tells you more than the number 9.&lt;/p&gt;
&lt;h3&gt;
  
  
  Class Names
&lt;/h3&gt;

&lt;p&gt;Here are some classic guidelines used to assist in naming classes that exist within object-oriented languages.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Noun or noun phrase names&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By using a noun you make sense of the &lt;code&gt;new&lt;/code&gt; keyword which initialise it.&lt;/p&gt;

&lt;p&gt;e.g.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const apple = new Fruit("sweet", "crunchy");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It makes less sense with a verb attached to it or as a verb. e.g. &lt;code&gt;new createFruit()&lt;/code&gt;, &lt;code&gt;new Swim()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Think concrete words, something that has a single clear meaning.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Open to different states&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Avoid class names that would be inconsistent with the state when calling a method that changes the state.&lt;/p&gt;

&lt;p&gt;Make the name less specific.&lt;/p&gt;

&lt;p&gt;e.g. &lt;code&gt;new FelineDomesticAnimal&lt;/code&gt; is too closed off from what it can or cannot be (it says only cats allowed). Something like &lt;code&gt;new Animal&lt;/code&gt; allows more diversity.&lt;/p&gt;

&lt;p&gt;Cover all expected values. Don’t write the means to an end. Avoid contradiction. Write the field. Use a name that applies to values the state may hold.&lt;/p&gt;

&lt;p&gt;e.g. A class with method &lt;code&gt;start&lt;/code&gt; and &lt;code&gt;end&lt;/code&gt; may have a name like &lt;code&gt;Race&lt;/code&gt;, not &lt;code&gt;FinalRace&lt;/code&gt;. "Final" suggest it is sided with the method &lt;code&gt;end&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Make it inclusive.&lt;/p&gt;

&lt;h3&gt;
  
  
  Method Names
&lt;/h3&gt;

&lt;p&gt;Method names should be a verb or a verb phrase name.&lt;/p&gt;

&lt;p&gt;e.g. &lt;code&gt;saveDocument&lt;/code&gt;, &lt;code&gt;playMusic&lt;/code&gt; or &lt;code&gt;delete&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For accessors, mutators, and predicates, the &lt;a href="https://www.oreilly.com/library/view/javaserver-pages-3rd/0596005636/ch20s01s01.html" rel="noopener noreferrer"&gt;JavaBeans Naming Convention&lt;/a&gt; says they should be named for their value and prefixed with &lt;code&gt;get&lt;/code&gt; and &lt;code&gt;set&lt;/code&gt;. Therefore, only use these for field access.&lt;/p&gt;

&lt;p&gt;e.g.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const grade = student.grade();
teacher.setGrade(80);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For methods with side-effects, do not use &lt;code&gt;get&lt;/code&gt;, &lt;code&gt;is&lt;/code&gt;, or &lt;code&gt;has&lt;/code&gt; prefixes.&lt;/p&gt;

&lt;h3&gt;
  
  
  Use Solution Domain Names
&lt;/h3&gt;

&lt;p&gt;Don’t be afraid to use Computer Science terms, algorithm names, pattern names, math terms and so forth. The people who are reading your code are programmers.&lt;/p&gt;

&lt;p&gt;Whereas, if there is no programmers, use &lt;strong&gt;problem domain names&lt;/strong&gt;. Code that has to do with problem domain concepts should have names that derive from it. Even though we may not know what it means, we can at least ask the domain expert what it means. It saves the hassle of writing up your own castle of names and referring back and forth to the blueprint domain names given.&lt;/p&gt;

&lt;h3&gt;
  
  
  Add Meaningful Context
&lt;/h3&gt;

&lt;p&gt;Names can be meaningful by themselves, but when they are not, you need to place them in context for your reader. Enclose them in well-named classes, functions or namespaces. If fail, prefix the name to add the necessary context.&lt;/p&gt;

&lt;h2&gt;
  
  
  Concluding Remarks
&lt;/h2&gt;

&lt;p&gt;Clarity is king. We write code for humans, not just to satisfy the compiler of our computer. We want to skim over our code and produce as fast as possible. We don’t want to spend an hour deciphering a code name. I don’t blame others, it is hard naming things. An established naming guidelines helps elevate the difficulties we share on deciding a name, one which we can all agree on and feel at ease understanding. Once you are a more seasoned developer you can customize or improve the standard!&lt;/p&gt;




&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Robert Cecil Martin. “&lt;a href="https://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882" rel="noopener noreferrer"&gt;Clean Code: A Handbook of Agile Software Craftsmanship&lt;/a&gt;". Courier in Stoughton, Massachusetts, USA: Pearson Education, Inc, 2009 (book)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Jolas. “&lt;a href="https://medium.com/mindorks/meaningful-names-a-dimension-of-writing-clean-code-fdae1ae4f0b1" rel="noopener noreferrer"&gt;Meaningful Names — A Dimension of writing Clean Code&lt;/a&gt;”. 2017 (article)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.quora.com/Why-do-so-many-programmers-use-bad-variable-names-Why-dont-they-just-write-out-meaningful-names-Is-there-a-practical-purpose-to-doing-this-is-it-a-bad-habit-or-is-it-just-to-be-more-hardcore" rel="noopener noreferrer"&gt;Why do so many programmers use bad variable names? Why don’t they just write out meaningful names? Is there a practical purpose to doing this, is it a bad habit, or is it just to be more “hardcore”?&lt;/a&gt; (thread)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://hilton.org.uk/" rel="noopener noreferrer"&gt;Peter Hilton&lt;/a&gt; and &lt;a href="https://www.felienne.com/" rel="noopener noreferrer"&gt;Felienne Hermans&lt;/a&gt;. “&lt;a href="https://hilton.org.uk/presentations/naming-guidelines" rel="noopener noreferrer"&gt;Naming guidelines for professional programmers&lt;/a&gt;” 2017 (article)&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>softwareengineering</category>
      <category>bestpractices</category>
      <category>namingconventions</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
