avatarBirat Rai

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

3547

Abstract

span> nameString; // Name can<span class="hljs-symbol">'t</span> be other than <span class="hljs-built_in">string</span>, hence noise added </pre></div><blockquote id="ee1c"><p><b>Use Pronounceable Names:</b></p></blockquote><ul><li>Don’t include acronyms unless otherwise a universal one.</li></ul><div id="eade"><pre>private <span class="hljs-built_in">Date</span> genymdhms --> This may be a generation (<span class="hljs-built_in">date</span>,<span class="hljs-built_in">year</span> <span class="hljs-built_in">month</span>,<span class="hljs-built_in">day</span>, <span class="hljs-built_in">hour</span>, <span class="hljs-built_in">minute</span> and <span class="hljs-built_in">second</span>) <span class="hljs-built_in">TimeStamp</span> since it is company specific acronym. But, doesn<span class="hljs-string">'t make sense.</span></pre></div><blockquote id="691b"><p><b>Use Searchable Names:</b></p></blockquote><ul><li>Use single name not single letter for local variables.</li></ul><div id="74fe"><pre><span class="hljs-built_in">int</span> d; <span class="hljs-comment">// One might name local variable as just letter</span> <span class="hljs-built_in">int</span> t; <span class="hljs-comment">//</span></pre></div><div id="0814"><pre><span class="hljs-built_in">int</span> workedDay; <span class="hljs-comment">// This looks more readable</span> <span class="hljs-built_in">int</span> realTask;</pre></div><ul><li>Use numeric constant to define constants in class, so that 5 or 24 wouldn’t be ambiguous.</li></ul><div id="2597"><pre>int WORK_DAYS_PER_WEEK <span class="hljs-operator">=</span> <span class="hljs-number">5</span><span class="hljs-comment">;</span></pre></div><div id="7d48"><pre>int HOURS_IN_A_DAY <span class="hljs-operator">=</span> <span class="hljs-number">24</span><span class="hljs-comment">;</span></pre></div><ul><li>Consider the following for-loop:</li></ul><div id="3a3d"><pre>for (int j <span class="hljs-operator">=</span> <span class="hljs-number">0</span><span class="hljs-comment">; j < 34; j++) {</span></pre></div><div id="7b14"><pre> s += (<span class="hljs-built_in">t</span>[j] * <span class="hljs-number">4</span>)/<span class="hljs-number">5</span>; }</pre></div><div id="0b5c"><pre><span class="hljs-comment">// Did you understand what the loop is doing??</span></pre></div><div id="8664"><pre>for (int <span class="hljs-keyword">j=0; </span><span class="hljs-keyword">j </span>< NUMBER_OF_TASKS<span class="hljs-comment">; j++) {</span></pre></div><div id="efeb"><pre> <span class="hljs-built_in">int</span> realTaskDays = taskEstimate[j] * realDaysPerIdealDay; <span class="hljs-built_in">int</span> realTaskWeeks = (realdays / WORK_DAYS_PER_WEEK); <span class="hljs-keyword">sum</span> += realTaskWeeks;</pre></div><div id="d9b8"><pre>}</pre></div><div id="d004"><pre><span class="hljs-comment">// Hopefully I don't have to explain what it's doing now</span></pre></div><blockquote id="6f65"><p><b>Avoid Hungarian Notations:</b></p></blockquote><ul><li>Java is type-rich system, that enforces types.</li></ul><div id="ffd4"><pre><span class="hljs-type">String</span> mName; <span class="hljs-comment">// Even- though Android specifies it's usage</span> <span class="hljs-type">int</span> sDayCount; <span class="hljs-comment">// avoid Hungarian notations</span></pre></div><blockquote id="4781"><p><b>Special Case of Interfaces and Implementations</b></p></blockquote><ul><li>Interface and it’s implementations can be exempted from prefixes and hungarian notation.</li></ul><div

Options

id="4e89"><pre>IShapeFactory factory<span class="hljs-comment">;</span> ShapeFactoryImpl shapeFactory<span class="hljs-comment">;</span></pre></div><blockquote id="6ffb"><p><b>Class Names</b></p></blockquote><ul><li>Class and objects should have noun or noun phrase names.</li></ul><div id="37ef"><pre><span class="hljs-built_in">Customer,</span> WikiPage, Account, AddressParser</pre></div><ul><li>Avoid Class name as verbs.</li></ul><div id="5579"><pre><span class="hljs-symbol">Manager</span>, Processor, <span class="hljs-meta">Data</span>, <span class="hljs-meta">Info</span></pre></div><blockquote id="2723"><p><b>Method Names</b></p></blockquote><ul><li>Method name should have verb or verb phrases</li></ul><div id="0a03"><pre><span class="hljs-built_in">postPayment</span>(); <span class="hljs-built_in">deletePage</span>(); <span class="hljs-built_in">save</span>();</pre></div><ul><li>Accessor and mutators and predicators should be named for their value and prefixed with get, set and is.</li></ul><div id="37fc"><pre><span class="hljs-built_in">getName</span>(); <span class="hljs-built_in">setName</span>(); <span class="hljs-built_in">isPosted</span>();</pre></div><blockquote id="509c"><p><b>Don’t be Cute with Names</b></p></blockquote><ul><li>Don’t be clever with naming.</li></ul><div id="a85d"><pre>HolyHandGrenade, <span class="hljs-built_in">whack</span>(), <span class="hljs-built_in">eatMyShorts</span>()</pre></div><blockquote id="dada"><p><b>Pick One Word per Concept</b></p></blockquote><ul><li>A consistent lexicon should be followed.</li><li>We might easily mix the following as equivalent word for similar methods.</li></ul><div id="849d"><pre><span class="hljs-keyword">fetch</span>, retrieve <span class="hljs-keyword">and</span> <span class="hljs-keyword">get</span> <span class="hljs-comment">--> Represent similar meaning</span></pre></div><div id="b90d"><pre>controller, manager <span class="hljs-keyword">and </span>driver --> Can <span class="hljs-keyword">be </span>used interchangeably</pre></div><blockquote id="2b74"><p><b>Use Solution Domain Names</b></p></blockquote><ul><li>Whenever possible use CS terms, algorithms names, pattern names, math terms for naming.</li></ul><div id="095a"><pre><span class="hljs-built_in">AccountVisitor,</span> JobQueue,</pre></div><blockquote id="a1a9"><p><b>Add Meaningful Context</b></p></blockquote><ul><li>There may be variables that are meaningful in themselves. But, they may be referring to a higher context.</li></ul><div id="fb49"><pre>firstName, lastName, street, houseNumber, <span class="hljs-keyword">state</span>, zipCode.</pre></div><ul><li>Here these variables are individually correct but they represent a bigger context that is the billing address.</li><li>In this case, extracting them to a single Class Address would represent the bigger context.</li></ul><blockquote id="0d24"><p><b>Don’t Add Gratuitous Context</b></p></blockquote><ul><li>Your company might be eager on using acronyms for everywhere possible, eg GSD for “Gas Station Deluxe”.</li><li>Don’t add gratuitous context to every class possible.</li></ul><div id="0cae"><pre><span class="hljs-built_in">GSDAccountAddress,</span> GSDPhoneNumber, GSDEmployee.</pre></div><p id="aaa3">Well these are conventions after all, nobody cares much.</p><figure id="563e"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*IlfIyYNUb0zgjc7MklGrRg.jpeg"><figcaption></figcaption></figure><p id="0364">Thanks for bearing with me. The third post will follow soon.</p></article></body>

Clean Code Series: II

If you haven’t read the previous blog Clean Code Series:I from my blog series please refer to them.

Meaning Full Names Jutsu

Why meaningful Names?

  • Consider you are naming a child (sounds easy right)
youAreMyKid(), cuteLittleAmanda(), shyBabyRobAndJanet()
  • I guess you would want to consider those names.
  • Hope you now consider naming variables, functions and classes.

How to write meaningful names?

These katas (Rules) will let you flex the rationale behind the naming convention. They have been made succinct from Clean Code Chapter 2.

User Intention Revealing Names:

a) int d ; 
b) int days;
c) int elapsedTimeInDays; 
  • All variables are intended for the same purpose → describe days.
  • I hope you found option (c) more comforting to understand it’s purpose.
  • We should always name variable to make it easier to understand and reveal the purpose of it.

Avoid Disinformation:

  • We should avoid using standard Java Keywords in names unless it refers to it.
List accountList; 
Naming is good unless it's a Java List.
  • Using name which vary in small way.
User user;
Users users;
// Both are valid but creates ambiguous as to what they refer.
  • Using letters ‘O’ and ‘l’ similar to number ‘0’ and ‘1’.

Make Meaningful Distinctions:

  • The name should be distinct and should make it meaningful.
ProductInfo and ProductData; // Are they related or similar, can't be distinguished
getActiveAccounts(), getActiveAccount() and getActiveAccountInfo() // Can't get any meaningful distinction whenever we need to use it, we have to look at the function what it does to understand it
  • Remove redundant noise words.
String nameString; // Name can't be other than string, hence noise added

Use Pronounceable Names:

  • Don’t include acronyms unless otherwise a universal one.
private Date genymdhms --> This may be a generation (date,year month,day, hour, minute and second) TimeStamp since it is company specific acronym. But, doesn't make sense.

Use Searchable Names:

  • Use single name not single letter for local variables.
int d; // One might name local variable as just letter
int t; //
int workedDay; // This looks more readable
int realTask;
  • Use numeric constant to define constants in class, so that 5 or 24 wouldn’t be ambiguous.
int WORK_DAYS_PER_WEEK = 5;
int HOURS_IN_A_DAY = 24;
  • Consider the following for-loop:
for (int j = 0; j < 34; j++) {
   s += (t[j] * 4)/5;
}
// Did you understand what the loop is doing??
for (int j=0; j < NUMBER_OF_TASKS; j++) {
  int realTaskDays = taskEstimate[j] * realDaysPerIdealDay;
  int realTaskWeeks = (realdays / WORK_DAYS_PER_WEEK);
  sum += realTaskWeeks;
}
// Hopefully I don't have to explain what it's doing now

Avoid Hungarian Notations:

  • Java is type-rich system, that enforces types.
String mName; // Even- though Android specifies it's usage
int sDayCount; // avoid Hungarian notations

Special Case of Interfaces and Implementations

  • Interface and it’s implementations can be exempted from prefixes and hungarian notation.
IShapeFactory factory;
ShapeFactoryImpl shapeFactory;

Class Names

  • Class and objects should have noun or noun phrase names.
Customer, WikiPage, Account, AddressParser
  • Avoid Class name as verbs.
Manager, Processor, Data, Info

Method Names

  • Method name should have verb or verb phrases
postPayment(); deletePage(); save();
  • Accessor and mutators and predicators should be named for their value and prefixed with get, set and is.
getName(); setName(); isPosted();

Don’t be Cute with Names

  • Don’t be clever with naming.
HolyHandGrenade, whack(), eatMyShorts()

Pick One Word per Concept

  • A consistent lexicon should be followed.
  • We might easily mix the following as equivalent word for similar methods.
fetch, retrieve and get --> Represent similar meaning
controller, manager and driver --> Can be used interchangeably

Use Solution Domain Names

  • Whenever possible use CS terms, algorithms names, pattern names, math terms for naming.
AccountVisitor, JobQueue,

Add Meaningful Context

  • There may be variables that are meaningful in themselves. But, they may be referring to a higher context.
firstName, lastName, street, houseNumber, state, zipCode.
  • Here these variables are individually correct but they represent a bigger context that is the billing address.
  • In this case, extracting them to a single Class Address would represent the bigger context.

Don’t Add Gratuitous Context

  • Your company might be eager on using acronyms for everywhere possible, eg GSD for “Gas Station Deluxe”.
  • Don’t add gratuitous context to every class possible.
GSDAccountAddress, GSDPhoneNumber, GSDEmployee.

Well these are conventions after all, nobody cares much.

Thanks for bearing with me. The third post will follow soon.

Java
Clean Code
Android
Robert C Martin
Meaningful Names
Recommended from ReadMedium