SEL III - Clean Code
SEL III - Clean Code
Clean code
1
Content
2
Writing clean code in SEL3
3
“Indeed, the ratio of time spent reading versus writing is well over 10 to
1. We are constantly reading old code as part of the effort to write new
code. ...[Therefore,] making it easy to read makes it easier to write.”
4
You are responsible for clean code
5
6
Use intention-revealing names
7
Use intention-revealing names
int elapsedTime
vs
int elapsedTimeInHours
class Metrics
vs
class PerformanceMetrics
8
Use intention-revealing names
list1 = []
for x in input_list:
list1.append(x.perf)
vs
performance_per_epoch = []
for metrics in metrics_per_epoch:
performance_per_epoch.append(metrics.performance)
(or, alternatively)
9
Functions
● Smaller is better
● Functions should do only one thing (this is harder than it sounds)
● Use intention-revealing names
● Don’t be afraid to use long names. A long descriptive name is better than a short
name with comment.
● Keep the number of arguments to a minimum
● Avoid flags in arguments
● “Master programmers think of systems as stories to be told rather than programs to
be written.” (Robert C. Martin)
10
Compare this code fragment with …
void SetupRobot(){
foreach (GameObject go in gameObjects){
if (go.name.Contains("robot")){
ArticulationBody[] articulations = go.GetComponents(typeof(ArticulationBody)) as ArticulationBody[];
foreach (ArticulationBody articulationBody in articulations){
ArticulationDrive xDrive = articulationBody.xDrive;
xDrive.stiffness = 10000;
articulationBody.xDrive = xDrive;
}
}
}
}
11
… this fragment
List<GameObject> GetAllRobots(){
List<GameObject> robots = new ArrayList<GameObject>();
foreach (GameObject go in gameObjects){
if (go.name.Contains("robot")){
robots.Add(go);
}
}
return robots;
}
Is no comment
“The proper use of comments is to compensate for our failure to express ourself in
code.” (Robert C. Martin - Clean Code)
13
What makes a bad comment
14
Avoiding comments with good code
Example:
# check whether agent is done training
if agent.is_training and agent.nr_epochs > max_epochs
Vs
if agent_is_done_training(agent)
15
What makes a good comment?
● API documentation
● Physical quantities
○ E.g. float angular_velocity = 1.0f; # in [radians s^-1]
● Clarifications, hacks
● TODO’s
● Warnings
16
Follow a style guide
17
Using version control systems (GIT)
18
Project workflow tips
20
Project documentation
21
Undiscussed topics
● Exception handling
● Testing (unit and integration)
● Concurrency
● System building
● The use of null
● Language-specific best practices
22
Clean code in practice
23
Scrum in SEL3
24
Scrum in SEL3
● Weekly sprints
● Scrum & consult session every week
● Questions
○ What have you done?
○ Where are you stuck?
○ What will you do the next week?
● Use any communication medium you want or need
25
Communication
26
Communication
27
What to do if you have (technical) questions?
1. LMGTFY
2. GitHub docs & issue tracker
3. Ufora & speak to each other
4. Consult sessions
5. [email protected] (start subject line with ‘[SEL3]’)
28