Code quality

Содержание

Слайд 2

Tell me, what is a clean code for you?

Tell me, what is a clean code for you?

Слайд 3

for (i = 0;i 0 && char_code

for (i = 0;i < strlen(line);i++) {     short char_code = isupper(line[i])

? line[i] - 64 : line[i] - 96;     if (char_code > 0 && char_code < 27) { printf("%c", isupper(line[i]) ? ((char_code + step) % 26 + 64) : ((char_code + step) % 26 + 96));     } else {     printf("%c", line[i]);     }     }
Слайд 4

void CEquation::trio(double array[][rt+1],double alfa[][rt+1]) { for (int L = 0; L

void CEquation::trio(double array[][rt+1],double alfa[][rt+1]) { for (int L = 0; L

1+L; k
Слайд 5

Good vs Bad

Good vs Bad

Слайд 6

How to write beautiful code There are two parts to learning

How to write beautiful code

There are two parts to learning craftsmanship:

knowledge and work. You must gain the knowledge of principles, patterns, practices, and heuristics that a craftsman knows, and you must also grind that knowledge into your fingers, eyes, and gut by working hard and practicing.
(Robert C. Martin, Clean code)
Слайд 7

80% of the lifetime cost of a piece of software goes

80% of the lifetime cost of a piece of software goes

to maintenance.
Hardly any software is maintained for its whole life by the original author.
Code quality improve the readability of the software, allowing engineers to understand new code more quickly and thoroughly.

Why its important?

Слайд 8

Why do we see a bad code? Time: I don't have

Why do we see a bad code?

Time: I don't have a

time
We never seem to have time to do it, but always seem find time to redo it?!
Knowledge: what is a good code?
Tools: I don’t know about it
Skills: I can’t do it
Слайд 9

Слайд 10

Naming is important

Naming is important

Слайд 11

The name should represent the developer’s idea int d; // time

The name should represent the developer’s idea
int d; // time from

beginning
int elapsedTimeInDays;
int daysSinceCreation;
int daysSinceModification;
Слайд 12

The name should represent the developer’s idea public List getThem() {

The name should represent the developer’s idea
public List getThem() {
List list1

= new ArrayList();
for (int[] x : theList)
if (x[0] == 4)
list1.add(x);
return list1;
}
What kinds of things are in theList?
What is the significance of the zeroth subscript of an item in theList?
What is the significance of the value?
How would I use the list being returned?
Слайд 13

The name should represent the developer’s idea public List getFlaggedCells() {

The name should represent the developer’s idea
public List getFlaggedCells() {
List flaggedCells

= new ArrayList();
for (int[] cell : gameBoard)
if (cell[STATUS_VALUE] == FLAGGED)
flaggedCells.add(cell);
return flaggedCells;
}
Слайд 14

Use meaningful difference public static void copyChars(char a1[], char a2[]) {

Use meaningful difference
public static void copyChars(char a1[], char a2[]) {
for (int

i = 0; i < a1.length; i++) {
a2[i] = a1[i];
}
} VS
source and destination are used for the argument
names
Слайд 15

Use meaningful names private Date genymdhms; private Date modymdhms; private final

Use meaningful names
private Date genymdhms;
private Date modymdhms;
private final String pszqint

= "102";
VS
private Date generationTimestamp;
private Date modificationTimestamp;;
private final String recordId = "102";
Слайд 16

Use searchable names #define CFT_CNT 13 #define SSIM_CLS_CNT 4096 #define PX_BUFF_CNT 10000

Use searchable names
#define CFT_CNT 13
#define SSIM_CLS_CNT 4096
#define PX_BUFF_CNT 10000

Слайд 17

Class names Classes and objects should have noun or noun phrase

Class names

Classes and objects should have noun or noun phrase names

like Customer, WikiPage, Account, and AddressParser. Avoid words like Manager, Processor, Data, Info in the name of a class. A class name should not be a verb.
Слайд 18

Method names Methods should have verb or verb phrase names like

Method names

Methods should have verb or verb phrase names like postPayment

deletePage, or save.
Accessors, mutators, and predicates should be named for their value and prefixed with get, set , and is according to the javabean standard.
Слайд 19

Method Names Should Say What They Do Date newDate = date.add(5);

Method Names Should Say What They Do

Date newDate = date.add(5);
Would you

expect this to add five days to the date? Or is it weeks, or hours?
Is the date instance changed or does the function just return a new Date without changing the old one?
You can’t tell from the call what the function does.
Слайд 20

One function – one operation Functions (aka methods)

One function – one operation

Functions (aka methods)

Слайд 21

Functions (aka methods)

Functions (aka methods)

Слайд 22

F1:Too Many Arguments Functions should have a small number of arguments.

F1:Too Many Arguments
Functions should have a small number of arguments. No

argument is best, followed by one, two, and three.
F2: Output Arguments
Output arguments are counterintuitive. Readers expect arguments to be inputs, not outputs. If your function must change the state of something, have it change the state of the object it is called on.

Functions (aka methods)

Слайд 23

F3: Flag Arguments Boolean arguments loudly declare that the function does

F3: Flag Arguments
Boolean arguments loudly declare that the function does more

than one thing. They are confusing and should be eliminated
F4: Dead Function
Methods that are never called should be discarded. Keeping dead code around is wasteful. Don’t be afraid to delete the function. Remember, your source code control system still remembers it.

Functions (aka methods)

Слайд 24

I’ll leave the project when complete all my technical debt

I’ll leave the project when complete all my technical debt

Слайд 25

Code review Each team member review the code and writing comments/recomendations

Code review

Each team member review the code and writing comments/recomendations
It’s

a primary responsibility of TL/Senior at the project
Code have to be understandable by your colleagues (from other teams)
Good news – we have automatic code quality check tools available
Слайд 26

Comments C1: Inappropriate Information Change histories(?) Authors(?) Date of last update(?)

Comments

C1: Inappropriate Information
Change histories(?)
Authors(?)
Date of last update(?)
C2: Obsolete Comment
It is best

not to write a comment that will become obsolete
If you find an obsolete comments – update or erase it ASAP!
Слайд 27

Comments C3. Redundant Comment Don’t comment what a code does –

Comments

C3. Redundant Comment
Don’t comment what a code does – I can

read the code for that—keep it DRY
Example 1
i++; // increment i
What about example 2?
/**
* @param sellRequest
* @return
* @throws ManagedComponentException
*/
public SellResponse beginSellItem(SellRequest sellRequest)
throws ManagedComponentException
Слайд 28

Comments C4: Poorly Written Comment Comments should say Why or purpose,

Comments

C4: Poorly Written Comment
Comments should say Why or purpose, not how
C5:

Commented-Out Code
Who knows how old it is? Who knows whether or not it’s meaningful? Yet no one will delete it because everyone assumes someone else needs it or has plans for it.
Слайд 29

General G1: Multiple Languages in One Source File a Java source

General

G1: Multiple Languages in One Source File
a Java source file might

contain snippets of XML, HTML, YAML, JavaDoc, English, JavaScript or
in addition to HTML a JSP file might contain Java, a tag library syntax, English comments, Javadocs, XML, JavaScript, and so forth.
The ideal is for a source file to contain one, and only one, language. Realistically, we will probably have to use more than one.
Слайд 30

General G5: Duplication Every time you see duplication in the code,

General

G5: Duplication
Every time you see duplication in the code, it represents

a missed opportunity for abstraction.
Result of copy/paste programming – to separate method
switch/case or if/else chain that appears again and again in various modules, always testing for the same set of conditions – to use polymorphism
Слайд 31

General G5: Duplication (cont) modules that have similar algorithms, but that

General

G5: Duplication (cont)
modules that have similar algorithms, but that don’t share

similar lines of code – to use Template Method or Strategy design patterns.
Find and eliminate duplication wherever you can!
Слайд 32

General G6: Code at Wrong Level of Abstraction Good software design

General

G6: Code at Wrong Level of Abstraction
Good software design requires that

we separate concepts at different levels and place them in different containers
Слайд 33

General G7: Base Classes Depending on Their Derivatives The most common

General

G7: Base Classes Depending on Their Derivatives
The most common reason for

partitioning concepts into base and derivative classes is so that the higher level base class concepts can be independent of the lower level derivative class concepts.
Слайд 34

General G9: Dead Code You find it in the body of

General

G9: Dead Code
You find it in the body of an if

statement that checks for a condition that can’t happen. You find it in the catch block of a try that never throws. You find it in little utility methods that are never called or switch/case conditions that never occur.
Слайд 35

General G11: Inconsistency If you do something a certain way, do

General

G11: Inconsistency
If you do something a certain way, do all similar

things in the same way.
If you name a method processVerificationRequest, then use a similar name, such as processDeletionRequest, for the methods that process other kinds of requests.
Слайд 36

General G23: Prefer Polymorphism to If/Else or Switch/Case “ONE SWITCH” rule:

General

G23: Prefer Polymorphism to If/Else or Switch/Case
“ONE SWITCH” rule:
There may

be no more than one switch statement for a given type of selection. The cases in that switch statement must create polymorphic objects that take the place of other such switch statements in the rest of the system.
Слайд 37

General G28: Encapsulate Conditionals if (shouldBeDeleted(timer)) is preferable to if (timer.hasExpired() && !timer.isRecurrent())

General

G28: Encapsulate Conditionals
if (shouldBeDeleted(timer))
is preferable to
if (timer.hasExpired() && !timer.isRecurrent())

Слайд 38

General G29: Avoid Negative Conditionals if (buffer.shouldCompact()) is preferable to if (!buffer.shouldNotCompact())

General

G29: Avoid Negative Conditionals
if (buffer.shouldCompact())
is preferable to
if (!buffer.shouldNotCompact())

Слайд 39

General G35: Keep Configurable Data at High Levels

General

G35: Keep Configurable Data at High Levels

Слайд 40

One more Clear, not Clever Don’t be clever, instead be clear

One more

Clear, not Clever
Don’t be clever, instead be clear
“I never make

stupid mistakes. Only very, very clever ones”
John Peel
Слайд 41

What is “code smell”? Wiki say: In computer programming, code smell

What is “code smell”?

Wiki say:
In computer programming, code smell is any symptom in the source code of

a program that possibly indicates a deeper problem. Code smells are usually not bugs—they are not technically incorrect and don't currently prevent the program from functioning. Instead, they indicate weaknesses in design that may be slowing down development or increasing the risk of bugs or failures in the future.
Слайд 42

The most “popular” code smells

The most “popular” code smells

Слайд 43

Bad vs. Clean code What is the clean code? What is

Bad vs. Clean code

What is the clean code?
What is the bad

code?
Characteristics of quality code
Metrics to measure quality
Ways to identify and build quality
Слайд 44

What do you know now? What is Code Smell It’s a

What do you know now?

What is Code Smell
It’s a feeling or

sense that something is not right in the code
You can’t understand it
Hard to explain
Does some magic
Can we measure it?
Слайд 45

How to improve code quality?

How to improve code quality?