34
eleks.com eleks.com CODE PRACTICES

Code Practices

Embed Size (px)

Citation preview

Page 1: Code Practices

eleks.com eleks.com

CODE PRACTICES

Page 2: Code Practices

HELLO!I am Victor Matyushevskyy

Page 3: Code Practices

MEET BOB

Page 4: Code Practices

1.CODE LAYOUT

Page 5: Code Practices

75 PERCENTSThat much information we percieve though sightHow your code looks matters

Page 6: Code Practices

A way to better looking code

• Consistent Code Indentation• Consistent Members Ordering• Avoid Deep Nesting• Use Access Modifiers• Limit Line Length

Page 7: Code Practices

BAD STYLE

Page 8: Code Practices

BAD STYLE

Page 9: Code Practices

GOOD STYLE

Page 10: Code Practices

GOOD STYLE

Page 11: Code Practices

2.NAMING CONVENTIONS

Page 12: Code Practices

Reasons for using a convention• Reduce the effort needed to read and

understand source code• Provide additional information about identifier

usage• Provide better understanding in case of code

reuse after a long interval of time• Help avoid "naming collisions"• Enable code reviews to focus on more

important issues• Enhance the aesthetic and professional

appearance of work product

Page 13: Code Practices

Let’s compare this typical code:float a, b, c; a=9.81; b=5; c=0.5*a*(b^2);

To this self-documenting code:const float gravitationalForce = 9.81;float timeInSeconds = 5;float displacement = (1 / 2) * gravitationalForce * (timeInSeconds ^ 2);

Page 14: Code Practices

Naming Styles

Pascal CaseDijkstraResult ReconstructShortestPath(Graph graph, Vertex startVertex)

Camel CaseDijkstraResult reconstructShortestPath(Graph& graph, Vertex& startVertex)

Underscore Casedef reconstruct_shortest_path(predecessors, start_vertex, end_vertex)

Page 15: Code Practices

3.CODEDOCUMENTATION

Page 16: Code Practices

Comments. Use them to…• Describe code that might be difficult for

someone to follow• In cases of extreme optimization when using

advanced techniques• If you take an approach that might not be

obvious to others• Mark places of code that must be unchanged

for a reason• Describing why things are done the way they

are, not how they work

Page 17: Code Practices

Commentaries Conventions• Splitting long comments into multiple line

comment • Avoid obvious comments• Place the comment on a separate line, not at

the end of a line of code.• Begin comment text with an uppercase letter• Insert one space between the comment

delimiter and the comment text

Page 18: Code Practices

Let’s compare this example of a poor commenting style:const float a = 9.81; //gravitational forcefloat b = 5; //time in secondsfloat c = (1/2)*a*(b^2) //multiply the time and gravity together to get displacement

And this documented code, which better explains why it is being done :// Compute displacement with Newton's equation x = vₒt + ½at².const float gravitationalForce = 9.81;float timeInSeconds = 5;float displacement = (1 / 2) * gravitationalForce * (timeInSeconds ^ 2);

Page 19: Code Practices

4.CODESMELL

Page 20: Code Practices

CODE SMELL

• Usually is not a bug• Does not prevent the program from

functioning• Indicates weaknesses in design• Might increase risks of a bug in

future

Page 21: Code Practices

BLOATERS

Long Methods Long Parameter List Large Class

Page 22: Code Practices

OBJECT-ORIENTATION ABUSERS

Switch Statements Temporary Field Refused Bequest

Pavlo Hodysh
Temporary FieldsWhen fields in class are most of the time empty and store data only occasionaly and temporarly
Pavlo Hodysh
Refused BequestWhen using same interface for completely different types because of some similar API
Page 23: Code Practices

CHANGE PREVENTERS

Divergent Change Shotgun Surgery Parallel Inheritance Hierarchies

Pavlo Hodysh
Divergent ChangeWhen you need to change one thing and you go all aroung the class methods and adjust logic
Page 24: Code Practices

DISPENSABLES

Speculative Generality

Duplicate Code Comments

Page 25: Code Practices

COUPLERS

Feature Envy Message Chains Middle Man

Page 26: Code Practices

5.CODEREVIEW

Page 27: Code Practices

NO REVIEW

Page 28: Code Practices

WHAT FOR?

• Improves code quality• Detects code smells• Checks for styles and conventions• Knowledge sharing

Page 29: Code Practices

TWO TYPES OF REVIEW

Design ReviewAnalysis of design and architecture of application.

Code ReviewAnalysis of written code, conventions and styles.

Page 30: Code Practices

CODE REVIEW PROCESS

Request Review

Review Comments

Refactor

Close Review

Repeat

Page 31: Code Practices
Page 32: Code Practices

LET’S REVIEW SOME CONCEPTSLayoutThe way your code is structured is the way it is perceived.

NamesChoosing a better name for variables or type members is a step towards readability.

CommentsLess of them makes you concentrate on code itself. Use them wisely.

SmellLike a rotten apple, youre code can smell, so keep it fresh and clean.

ReviewOne head is good, two is better. Reviewing you code can detect potential bugs and gaps.

ConventionsBefore writing some code know what convetions to use to keep code hygiene consistent.

Page 33: Code Practices

THANKS!Any questions?

Page 34: Code Practices

eleks.com

Inspired by Technology.Driven by Value.