0% found this document useful (0 votes)
460 views

Clean Code 01

The document discusses rules for naming variables, methods, and classes in clean code. It states that names should reveal intent, explain why things exist, what they do, and how they are used. It provides an example of unclear naming with variables like "s" and "cmd" and how more descriptive names like "numberOfStudents" and renaming a "compare" method to "isRemoveCommand" can improve clarity.

Uploaded by

Sandra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
460 views

Clean Code 01

The document discusses rules for naming variables, methods, and classes in clean code. It states that names should reveal intent, explain why things exist, what they do, and how they are used. It provides an example of unclear naming with variables like "s" and "cmd" and how more descriptive names like "numberOfStudents" and renaming a "compare" method to "isRemoveCommand" can improve clarity.

Uploaded by

Sandra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

CLEAN CODE 01

REINŽENJERING INFORMACIONIH SISTEMA


2
Robert C. Martin/Uncle Bob

• rođen 1952.
• programer od 1970. (1964.)
• Jedan od autora Manifesto of Agile Software Development
(2001.)
• Clean Code: A Handbook of Agile Software Craftsmanship
(2011.)
• Clean Architecture: A Craftsman's Guide to Software
Structure and Design (2017.)

Reinženjering informacionih sistema 2020/2021


3
• Uvod
• Nazivi (Names)
• Metode/Funkcije (Functions)
• Komentari (Comments)
• Formatiranje (Formatting)

• Objekti i strukture podataka (Objects and Data Structures)


• Upravljanje greškama (Error Handling)
• Testiranje (Unit Tests)

• Klase (Classes)
• Code Smells

• Junit Internals (primer)


• Refactoring SerialData (primer)

Reinženjering informacionih sistema 2020/2021


4

Preuzeto sa : https://www.osnews.com/story/19266/wtfsm/

Reinženjering informacionih sistema 2020/2021


5
Bjarne Stroustrup
inventor of C++ and author of The C++ Programming Language

“I like my code to be elegant and efficient. The logic should be straightforward to make it hard for bugs
to hide, the dependencies minimal to ease maintenance, error handling complete according to an
articulated strategy, and performance close to optimal so as not to tempt people to make the code
messy with unprincipled optimizations. Clean code does one thing well.”*

* Robert C. Martin - Clean Code: A Handbook of Agile Software Craftsmanship

Reinženjering informacionih sistema 2020/2021


6
Grady Booch
author of Object Oriented Analysis and Design with Applications

“Clean code is simple and direct. Clean code reads like well-written prose. Clean code never obscures
the designer’s intent but rather is full of crisp abstractions and straightforward lines of control.”*

* Robert C. Martin - Clean Code: A Handbook of Agile Software Craftsmanship

Reinženjering informacionih sistema 2020/2021


7
Dave Thomas
Founder of OTI (Object Technology International)

“Clean code can be read, and enhanced by a developer other than its original author. It has unit and
acceptance tests. It has meaningful names. It provides one way rather than many ways for doing one
thing. It has minimal dependencies, which are explicitly defined, and provides a clear and minimal API.
Code should be literate since depending on the language, not all necessary information can be
expressed clearly in code alone.”*

* Robert C. Martin - Clean Code: A Handbook of Agile Software Craftsmanship

Reinženjering informacionih sistema 2020/2021


8
Michael Feathers
author of Working Effectively with Legacy Code

“I could list all of the qualities that I notice in clean code, but there is one overarching quality that leads
to all of them. Clean code always looks like it was written by someone who cares. There is nothing
obvious that you can do to make it better. All of those things were thought about by the code’s author,
and if you try to imagine improvements, you’re led back to where you are, sitting in appreciation of the
code someone left for you—code left by someone who cares deeply about the craft.”*

* Robert C. Martin - Clean Code: A Handbook of Agile Software Craftsmanship

Reinženjering informacionih sistema 2020/2021


9
Ron Jeffries
author of Extreme Programming Installed and Extreme Programming Adventures in C#

“In recent years I begin, and nearly end, with Beck’s rules of simple code. In priority order, simple
code:
• Runs all the tests;
• Contains no duplication;
• Expresses all the design ideas that are in the system;
• Minimizes the number of entities such as classes, methods, functions, and the like.”*

* Robert C. Martin - Clean Code: A Handbook of Agile Software Craftsmanship

Reinženjering informacionih sistema 2020/2021


10
Ward Cunningham
inventor of Wiki, inventor of Fit, coinventor of eXtreme Programming

“You know you are working on clean code when each routine you read turns out to be pretty much
what you expected. You can call it beautiful code when the code also makes it look like the language
was made for the problem.”*

* Robert C. Martin - Clean Code: A Handbook of Agile Software Craftsmanship

Reinženjering informacionih sistema 2020/2021


11
Pravila imenovanja/Nazivi (Names)

• Naziv treba da otkrije nameru


• Nazivi imena promenljivih, metoda i klasa treba da daju odgovore na “velika” pitanja.
• Treba da objasne:
o zašto postoje
o šta rade i
o kako se koriste

Reinženjering informacionih sistema 2020/2021


12
Pravila imenovanja/Nazivi (Names)

• Naziv treba da otkrije nameru


• Nazivi imena promenljivih, metoda i klasa treba da daju odgovore na “velika” pitanja.
• Treba da objasne:
o zašto postoje
o šta rade i
o kako se koriste

int s;

Reinženjering informacionih sistema 2020/2021


13
Pravila imenovanja/Nazivi (Names)

• Naziv treba da otkrije nameru


• Nazivi imena promenljivih, metoda i klasa treba da daju odgovore na “velika” pitanja.
• Treba da objasne:
o zašto postoje
o šta rade i
o kako se koriste

int s;

int s; // number of students

Reinženjering informacionih sistema 2020/2021


14
Pravila imenovanja/Nazivi (Names)

• Naziv treba da otkrije nameru


• Nazivi imena promenljivih, metoda i klasa treba da daju odgovore na “velika” pitanja.
• Treba da objasne:
o zašto postoje
o šta rade i
o kako se koriste

int s;

int s; // number of students

int numberOfStudents;

Reinženjering informacionih sistema 2020/2021


15
Pravila imenovanja/Nazivi (Names) – Primer 1

if(compare(cmd)) {
boolean commandRemove = true;
while(commandRemove) {
executeCommand.peek().unexecute();
unexecuteCommand.push(executeCommand.pop());
Command nextCommand=executeCommand.peek();

if(!compare(nextCommand)) {
commandRemove=false;
}
}
}

Reinženjering informacionih sistema 2020/2021


16
Pravila imenovanja/Nazivi (Names) - – Primer 1

if(compare(cmd)) {
boolean commandRemove = true;
while(commandRemove) {
executeCommand.peek().unexecute();
unexecuteCommand.push(executeCommand.pop());
Command nextCommand=executeCommand.peek();

if(!compare(nextCommand)) {
commandRemove=false;
}
}
}

Reinženjering informacionih sistema 2020/2021


17
Pravila imenovanja/Nazivi (Names) – Primer 1

public boolean compare(Object o1) {


if(o1 instanceof Remove) {
return true;
}
else {
return false;
}
}

Reinženjering informacionih sistema 2020/2021


18
Pravila imenovanja/Nazivi (Names) – Primer 1

public boolean compare(Object o1) {


if(o1 instanceof Remove) {
return true;
}
else {
return false;
}
}

Reinženjering informacionih sistema 2020/2021


19
Pravila imenovanja/Nazivi (Names) – Primer 1

public boolean compare(Object o1) {


if(o1 instanceof Remove)
return true;
return false;
}

Reinženjering informacionih sistema 2020/2021


20
Pravila imenovanja/Nazivi (Names) – Primer 1

public boolean commandIsRemove(Command command) {


if (command instanceof RemoveCommand)
return true;
return false;

Reinženjering informacionih sistema 2020/2021


21
Pravila imenovanja/Nazivi (Names) – Primer 1

if (commandIsRemove(command)) {
boolean commandRemove = true;
while (commandRemove) {
executeCommand.peek().unexecute();
unexecuteCommand.push(executeCommand.pop());
Command nextCommand = executeCommand.peek();

if(!commandIsRemove(nextCommand)) {
commandRemove = false;
}
}
}

Reinženjering informacionih sistema 2020/2021


22
Pravila imenovanja/Nazivi (Names) – Primer 1

public boolean commandIsRemove(Command command) {


if (command instanceof RemoveCommand)
return true;
return false;

Reinženjering informacionih sistema 2020/2021


23
Pravila imenovanja/Nazivi (Names) – Primer 1

public boolean commandIsRemove(Command command) {


return command instanceof RemoveCommand;
}

Reinženjering informacionih sistema 2020/2021


24
Pravila imenovanja/Nazivi (Names) – Primer 1

if (command instanceof RemoveCommand) {


boolean commandRemove = true;
while (commandRemove) {
executeCommand.peek().unexecute();
unexecuteCommand.push(executeCommand.pop());
Command nextCommand = executeCommand.peek();

if(!(nextCommand instanceof RemoveCommand)) {


commandRemove = false;
}
}
}

Reinženjering informacionih sistema 2020/2021


25
Pravila imenovanja/Nazivi (Names) – Primer 1

public boolean compare(Object o1) {


if(o1 instanceof Remove) {
return true;
}
else {
return false;
}
}

command instanceof RemoveCommand

Reinženjering informacionih sistema 2020/2021


26
Pravila imenovanja/Nazivi (Names) – Primer 1

if (command instanceof RemoveCommand) {


boolean commandRemove = true;
while (commandRemove) {
executeCommand.peek().unexecute();
unexecuteCommand.push(executeCommand.pop());
Command nextCommand = executeCommand.peek();

if(!(nextCommand instanceof RemoveCommand)) {


commandRemove = false;
}
}
}

Reinženjering informacionih sistema 2020/2021


27
Pravila imenovanja/Nazivi (Names) – Primer 1

private Stack<Command> executeCommand;


private Stack<Command> unexecuteCommand;

Reinženjering informacionih sistema 2020/2021


28
Pravila imenovanja/Nazivi (Names) – Primer 1

private Stack<Command> executeCommand;


private Stack<Command> unexecuteCommand;

Klase (Classes) – Nazivi klasa treba da budu imenice

Varijable (Variables) – Nazivi varijabli treba da budu imenice, u slučaju kada se radi o kolekcijama,
treba da budu u množini

Metode (Methods) – Nazivi metoda treba da budu glagoli

Reinženjering informacionih sistema 2020/2021


29
Pravila imenovanja/Nazivi (Names) – Primer 1

private Stack<Command> executedCommands;


private Stack<Command> unexecutedCommands;

Reinženjering informacionih sistema 2020/2021


30
Pravila imenovanja/Nazivi (Names) – Primer 1

if (command instanceof RemoveCommand) {


boolean commandRemove = true;
while (commandRemove) {
executedCommands.peek().unexecute();
unexecutedCommands.push(executedCommands.pop());
Command nextCommand = executedCommands.peek();

if( !(nextCommand instanceof RemoveCommand)) {


commandRemove = false;
}
}
}

Reinženjering informacionih sistema 2020/2021


31
Pravila imenovanja/Nazivi (Names) – Primer 1

if (command instanceof RemoveCommand) {


boolean commandRemove = true;
while (commandRemove) {
executedCommands.peek().unexecute();
unexecutedCommands.push(executedCommands.pop());
Command nextCommand = executedCommands.peek();

if( !(nextCommand instanceof RemoveCommand)) {


commandRemove = false;
}
}
}

Reinženjering informacionih sistema 2020/2021


32
Pravila imenovanja/Nazivi (Names) – Primer 1

if (command instanceof RemoveCommand) {


boolean commandRemove = true;
while (command instanceof RemoveCommand) {
executedCommands.peek().unexecute();
unexecutedCommands.push(executedCommands.pop());
Command nextCommand = executedCommands.peek();

if( !(nextCommand instanceof RemoveCommand)) {


commandRemove = false;
}
}
}

Reinženjering informacionih sistema 2020/2021


33
Pravila imenovanja/Nazivi (Names) – Primer 1

if (command instanceof RemoveCommand) {


boolean commandRemove = true;
while (command instanceof RemoveCommand) {
executedCommands.peek().unexecute();
unexecutedCommands.push(executedCommands.pop());
command = executedCommands.peek();

if( !(command instanceof RemoveCommand)) {


commandRemove = false;
}
}
}

Reinženjering informacionih sistema 2020/2021


34
Pravila imenovanja/Nazivi (Names) – Primer 1

if (command instanceof RemoveCommand) {


while (command instanceof RemoveCommand) {
executedCommands.peek().unexecute();
unexecutedCommands.push(executedCommands.pop());
command = executedCommands.peek();
}
}

Reinženjering informacionih sistema 2020/2021


35
Pravila imenovanja/Nazivi (Names) – Primer 1

while (command isInstanceof RemoveCommand) {


executedCommands.peek().unexecute();
unexecutedCommands.push(executedCommands.pop());
command = executedCommands.peek();
}

Reinženjering informacionih sistema 2020/2021


36
Pravila imenovanja/Nazivi (Names) – Primer 1

unexecuteConsecutiveRemoveCommands(command);

public void unexecuteConsecutiveRemoveCommands(Command command){


while (command isInstanceof RemoveCommand) {
executedCommands.peek().unexecute();
unexecutedCommands.push(executedCommands.pop());
command = executedCommands.peek();
}
}

Reinženjering informacionih sistema 2020/2021


37
Pravila imenovanja/Nazivi (Names) – Primer 1

unexecuteConsecutiveRemoveCommands(command);

public void unexecuteConsecutiveRemoveCommands(Command command){


while (command isInstanceof RemoveCommand) {
executedCommands.peek().unexecute();
unexecutedCommands.push(executedCommands.pop());
command = executedCommands.peek();
}
}

Reinženjering informacionih sistema 2020/2021


38
Pravila imenovanja/Nazivi (Names) – Primer 1

unexecuteConsecutiveRemoveCommands(command);

private void unexecuteConsecutiveRemoveCommands(Command command){


while (command isInstanceof RemoveCommand) {
executedCommands.peek().unexecute();
unexecutedCommands.push(executedCommands.pop());
command = executedCommands.peek();
}
}

Reinženjering informacionih sistema 2020/2021


39
Pravila imenovanja/Nazivi (Names) – Primer 1

if(compare(cmd)) { public boolean compare(Object o1) {


boolean commandRemove = true; if(o1 instanceof Remove) {
while(commandRemove) { return true;
executeCommand.peek().unexecute(); }
unexecuteCommand.push(executeCommand.pop()); else {
Command nextCommand=executeCommand.peek(); return false;
}
if(!compare(nextCommand)) { }
commandRemove=false;
}
}
}

unexecuteConsecutiveRemoveCommands(command);

private void unexecuteConsecutiveRemoveCommands(Command command){


while (command isInstanceof RemoveCommand) {
executedCommands.peek().unexecute();
unexecutedCommands.push(executedCommands.pop());
command = executedCommands.peek();
}
}

Reinženjering informacionih sistema 2020/2021


40
Pravila imenovanja/Nazivi (Names)

Nazivi sa dodatnim značenjem:

private ArrayList<Key> keysList = new ArrayList<Key>();

//izbegavati, pošto List ima određeno značenje za programera


private Key[] keysList = new Key[10];

private Key[] keys = new Key[10];

Ukoliko koristite neki od dizajnerskih obrazaca, koristite nazive koji će to jasno staviti do znanja,
kao što je to bio slučaj Remove i RemoveCommand iz prethodnog primera.

Reinženjering informacionih sistema 2020/2021


41
Pravila imenovanja/Nazivi (Names)

Izbegavajte slične nazivi:

• getSelectedProduct();
• getSelectedProducts();
• getSelectedProductDetails();
• getSelectedProductInfo();

Kratki nazivi koje se teško pronalaze u kodu (Search):

• i
• e
• 4

Reinženjering informacionih sistema 2020/2021


42
Pravila imenovanja/Nazivi (Names)

for (int i=1; i<=16; i++) {


s += i*3;
}

----

const int NUMBER_OF_CONNECTIONS_PER_WORKPLACE = 3;


const int NUMBER_OF_WORKPLACES = 16;
int totalNumberOfConnections;

for (int i=1; i<=NUMBER_OF_WORKPLACES; i++) {


totalNumberOfConnections += i*NUMBER_OF_CONNECTIONS_PER_WORKPLACE;
}

Reinženjering informacionih sistema 2020/2021


43
Pravila imenovanja/Nazivi (Names)

Izbegavajte istovremenu upotrebu reči koje mogu biti sinonimi:


• fetch, get, collect, retrive

• fetchData()
• getUser()
• collectSubmissions()
• retrieveSubscribtions()

• fetchData()
• fetchUser()
• fetchSubmissions()
• fetchSubscribtions()

Reinženjering informacionih sistema 2020/2021


44
Pravila imenovanja/Nazivi (Names) – Primer 2

private void printGuessStatistics(char candidate, int count) {


String number;
String verb;
String pluralModifier;

if (count == 0) {
number = "no";
verb = "are";
pluralModifier = "s";
} else if (count == 1) {
number = "1";
verb = "is";
pluralModifier = "";
} else {
number = Integer.toString(count);
verb = "are";
pluralModifier = "s";
}

String guessMessage = String.format("There %s %s %s%s", verb, number, candidate, pluralModifier);

print(guessMessage); * Robert C. Martin - Clean Code: A Handbook of Agile Software Craftsmanship


}*

Reinženjering informacionih sistema 2020/2021


45
Pravila imenovanja/Nazivi (Names) – Primer 2

public class GuessStatisticsMessage {


private String number;
private String verb;
private String pluralModifier;

public String make(char candidate, int count) {


createPluralDependentMessageParts(count);
return String.format("There %s %s %s%s", verb, number, candidate, pluralModifier );
}

private void createPluralDependentMessageParts(int count) {


if (count == 0) {
thereAreNoLetters();
} else if (count == 1) {
thereIsOneLetter();
} else {
thereAreManyLetters(count);
}
}

* Robert C. Martin - Clean Code: A Handbook of Agile Software Craftsmanship

Reinženjering informacionih sistema 2020/2021


46
Pravila imenovanja/Nazivi (Names) – Primer 2

private void thereAreManyLetters(int count) {


number = Integer.toString(count);
verb = "are";
pluralModifier = "s";
}

private void thereIsOneLetter() {


number = "1";
verb = "is";
pluralModifier = "";
}

private void thereAreNoLetters() {


number = "no";
verb = "are";
pluralModifier = "s";
}
}*

* Robert C. Martin - Clean Code: A Handbook of Agile Software Craftsmanship

Reinženjering informacionih sistema 2020/2021


47
Pravila imenovanja/Nazivi (Names)

U prethodnom primeru pokazano je kako je kod postao čitljivijim dodavanjem metoda koje su mu
dale kontekst.

Navedene su samo neke osnovne preporuke, tokom nastavka često će ponovo biti pominjana
pravila imenovanja.

Pitanja?

Reinženjering informacionih sistema 2020/2021

You might also like