Show Menu
Cheatography

CleanMethods Cheat Sheet by

How to write clean Methods

Meaningful Distin­ction

What's source?
What is destin­ation?
public static void copyCh­ars­(char a1[ ], char a2[ ]) {
... }

Use Pronou­ncable Names

Write names out!
Longer names trumps short Names
Searchable names trumps a constant in Code
Avoid Encodings
Make names pronou­ncable
Dirty:
class DtaRcrd102 {
private Date genymdhms;
private Date modymdhms;
private final String pszqint = ”102”;}

Hungarian Notation

// Name not changed when type changed
PhoneN­umber phoneString;

Method Names

Use
verbs, phrases like PostPa­yment, deletePage or SavePage
Constr­uctor Overload use
Static factory Methods, with names, that describe the arguments

No Member Prefixes

//Historic => Old Code or old Coder
private String m_dsc;

Interface & Implem­ent­ation

Dirty
Clean
interface IShapeFactory
class ShapeFactoryImpl
 
interface Shapefactory

Don't Repeat Yourself

Refactor
Clone
 
Copy & Paste
 

Verbs And Keywords

verb/noun Pair:
(to)wr­ite­Fie­ld(­name);
assert­Exp­ect­edE­qua­lsA­ctu­al(­exp­ected, actual);

Use Descri­ptive Names

The Sequence should tell a Story:
includ­eSe­tup­-An­dTe­ard­own­Pag­es();
includ­eSu­ite­Set­upP­age();
includ­eSe­tup­Pag­es();

Use Problem Domain Names

If is there no Progra­mming technique use the name from the Problem Domain
Domain­Dri­ven­Design D3 not D3.js

Use Solution Domain Names

Accoun­tVi­sitor
Visito­rPa­ttern

Dont' be cute

Dirty
Clean
holyHa­ndG­ren­ade()
delete­Items()
eatMyS­horts()
abort()

One Level of Abstra­ction per Function

interm­ediate Level
String pagePa­thName = PathPa­rse­r.r­end­er(­pag­ePath);
low Level
.appen­d(”­\n”).

Add Meaningful Context

addres­sFi­rstName
addres­sLa­stName
addres­sState

Functions

Sideef­fects

If you must have a temporal coupling, you should make it clear in the name of the function
checkP­ass­wor­dAn­dIn­iti­ali­zeS­ess­ion()

Avoid Mental Mapping

Clean:
loop Counter only: i, j, k
Dirty:
k and l because One (1)
=> too much

cont = contin­uation

 
 

Flag Arguments

Passing a boolean into a funciton is a truly terrible practise
render­(bo­olean isSuite)

Structured Progra­mming

Use
* occasional multiple return,
 
* break and
 
* continue statement does no harm
Don't USE
never, ever, any goto statements
but
only in special languages. Be careful, you can destroy or control flow

Function Arguments

Useless:
Niladic(0) best Solution:
includ­eSe­tup­Page()
Polyadic ( More then three Argume­nts):
ResponseEntity<Map> responseEntity1 = restTemplate.exchange (REST_SERVICE_URI+"/contract/"+ID, HttpMethod.PUT, entity, Map.class)
Usable
Monadic:
boolean fileEx­ist­s(“­MyF­ile”)
Dyadic:
assert­Exp­ect­edE­qua­lsA­ctu­al(­exp­ected, actual);
Triadic:
assert­Equ­als­(1.0, amount, .001);

Don't add Gratuitous Context

You need:
* descri­ptive Skills
* shared cultural backround
* Don't be afraid of Renaming
* Use Refact­oring Tools
-> IntelliJ

Blocks And Indenting - Arrow Code

The indent level of a function should not be greater then
one or two
 

TO paragraph

We want every function to be followed by those at the next level of abstra­ction so that we can read the program, descending one level of abstra­ction at a time as we read down the list of functions.
To say this differ­ently, we want to be able to read the program as though it were a set of TO paragr­aphs, each of which is describing the current level of abstra­ction and refere­ncing subsequent TO paragraphs at the next level down.
Method­Header:
To include the setups,
Code:
we include the suite setup if this is a suite, then we include the regular setup.

Don't Pun

Use:
insert(), append()
Instead of
add()

Command Query Seperation

Either your function should change the state of an object, or it should return some inform­ation about that object:
public boolean set(String attribute, String value);
Clean:
if (attri­but­eEx­ist­s(”­use­rna­me”)) {setAt­tri­but­e(”­use­rname”, ”uncle­bob”); … }
Dirty:
if (set(”­use­rname”, ”uncle­bob­”))­{...}

Avoid Mental Mapping

loop Counter only:
i, j, k
Never
l because One(1)

One Thing

Functions should do one thing. They should do it well. They should do it only
* Aids the Reader
* Promote Reuse
*Eases Naming & Testing
*Avoid Sideef­fects
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Unit testing Cheat Sheet

          More Cheat Sheets by ChaosJD

          Batch Cheat Sheet