1 of 49

CSCI 494-802: Industry Methods

2/25/2025

Clean Code, Code Review

2 of 49

Agenda

  • Attendance
  • Questions on Assignment 3?
  • Clean Code
  • Code Review Tips

3 of 49

Attendance

4 of 49

Questions?

Any questions about Assignment 3?

Please do not merge the branch yet

  • if you have already done this, no worries we can revert / rebuild it

5 of 49

Feedback

Push yourself out of your comfort zone

If python is your strongest language, try a different one for these easy problems.

As inspiration this individual solved the first 100 project euler problems with 100 different languages

https://github.com/jaredkrinke/100-languages

READ 10x more code than you write...

6 of 49

Clean Code

  • Simplify naming, commenting, and formatting with tips that apply to every line of code
  • Refine your program’s loops, logic, and variables to reduce complexity and confusion
  • Attack problems at the function level, such as reorganizing blocks of code to do one task at a time
  • Write effective test code that is thorough and concise—as well as readable

7 of 49

Clean Code

Take pride in your work

“Leave the campground cleaner than you found it”

“Presentation is as important as the quality of the content that you are presenting”

"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."

8 of 49

  • KISS
  • Boy Scout Rule
  • Prefer polymorphism to if/else or switch/case.
  • Law of Demeter
  • Use explanatory variables.
  • Choose descriptive and unambiguous names.
  • magic numbers << named constants
  • Don't comment out code. Just remove.
  • ...

9 of 49

Functions rules

  • Small.
  • Do one thing.
  • Use descriptive names.
  • Prefer fewer arguments.
  • Have no side effects.
  • Don't use flag arguments. Split method into several independent methods that can be called from the client without the flag.

10 of 49

More Book Recommendations

  • Clean Code: A Handbook of Agile Software Craftsmanship by Robert C. Martin
  • Design of Existing Code by Martin Fowler
  • The Pragmatic Programmer: Your Journey To Mastery by David Thomas
  • Grokking Simplicity - Eric Normand
  • Code Complete: A Practical Handbook of Software Construction by Steve McConnell

11 of 49

  1. Keep it Simple
  2. Understand Your Code
  3. Comments are your new best friends
  4. Don’t Repeat Yourself (DRY)
  5. Indent Your Code
  6. Naming Convention
  7. Explore
  8. Use Your Brain
  9. Test Runs
  10. Practice Your Art

12 of 49

Naming Variables

"A name should tell you why it exists, what it does, and how it is used. If a name requires a comment, then the name does not reveal its intent."

– Clean Code

“Take as much time as you would naming your first born child” - unknown internet

13 of 49

Clean Code Explained - Name Variables

  • Create Meaningful Names
  • Avoid Disinformation
  • Avoid Noise Words
  • Use Pronounceable Names
  • Use Searchable Names
  • Be Consistent

14 of 49

Clean Code Explained - Write Functions

  • Keep them Small
  • Make Sure They Just Do One Thing
  • Encapsulate Conditionals in Functions
  • Fewer Arguments (2 or fewer)
  • Do not use Flag Arguments
  • Do Not Have Side Effects
  • Don’t Repeat Yourself

Additional Examples from ChatGPT

15 of 49

  1. Describe everything the routine does
  2. Avoid meaningless, vague, or wishy-washy verbs
  3. Don’t differentiate routine names solely by number
  4. Make names as long as necessary
  5. For functions, try using a description of the return value
  6. Use opposites precisely
  7. Establish conventions for common operations

16 of 49

The benefits of writing clean code

1. Easier to start, or continue

2. Better for team onboarding

3. Easier to follow

Tips on writing clean code

1. Make code readable for people

2. Use meaningful names for variables, functions and methods

3. Let one function or method perform only one task

4. Use comments for clarification

5. Be consistent

6. Review your code regularly

17 of 49

  • Every line does only one thing
  • It's not all about line length
  • The Rule of Six: A line of code containing 6+ pieces of information should be simplified.
  • If code is confusing, break it

18 of 49

Other Views

19 of 49

More Tips

20 of 49

Line of sight in code: Left edge is happy path, indented is error handling and edge cases

Code: Align the happy path to the left edge | by Mat Ryer | Medium

21 of 49

example: polymorphism over an if/else statement

22 of 49

Clean code summarized

Write small blocks of code

Small functions, small classes, small modules.

It will result in:

- higher cohesion

- better maintainability

- modular code structure

- ability to capture domain knowledge

- compliance with Single Responsibility Principle

And the improved readability will make your future self and your colleagues happier.

23 of 49

24 of 49

25 of 49

26 of 49

27 of 49

Clean Code Funny

28 of 49

Code Review (+1, +10)

Much of your day as a programmer will involve reviewing other pull requests and providing valuable feedback

Auditable

SOC1, SOC2, ...

29 of 49

+1

+1 is an acknowledgement of a code review

  • in the past this was just a comment left somewhere in the PR thread

Github has a nice Code Review feature directly - DEMO

30 of 49

What do you look for during a +1?

  • Does it solve the problem?
  • Is it clean? simple? documented?
  • Does it adhere to code style guide?
  • Does it contain tests?

31 of 49

32 of 49

Tact

Always remember:

  • Use Professionalism
  • Practice Egoless Programming

33 of 49

10 Commandments of Egoless

34 of 49

Code Review tips

35 of 49

More code review tips

36 of 49

More Guidelines

37 of 49

Style Guides

a set of conventions (sometimes arbitrary) about how to write code for that project. It is much easier to understand a large codebase when all the code in it is in a consistent style.

http://google.github.io/styleguide/

https://javascript.info/coding-style

38 of 49

Google Engineering Tips

39 of 49

Conventional Commits / Comments

40 of 49

Automate Style Rules

41 of 49

Cyclomatic complexity is a software metric used to indicate the complexity of a program. It is a quantitative measure of the number of linearly independent paths through a program's source code. It was developed by Thomas J. McCabe, Sr. in 1976.

42 of 49

Cyclomatic Complexity

Why is it important?

  • Limit code complexity.
  • Determine the number of test cases required.

What Is Cyclomatic Complexity? | Perforce

43 of 49

Cyclomatic Complexity

Use the following formula to calculate cyclomatic complexity (CYC):

CYC = E – N + 2P

P = number of disconnected parts of the flow graph (e.g. a calling program and a subroutine)

E = number of edges (transfers of control)

N = number of nodes (sequential group of statements containing only one transfer of control)

44 of 49

More on CYC

45 of 49

Metrics Study

46 of 49

Code Coverage

Tools exist to measure the lines of code that are covered by tests. Some companies require a certain threshold of testing (e.g. 75% of lines covered) with every pull request, otherwise the build fails.

codecov is an example

47 of 49

+10

+10 is a sanity check that another developer can checkout and run the code

This is not a full regression test but an assurance that the code in question passes a minimal test on a separate developer’s machine

Keep the local dev story working...

48 of 49

Capture Output

  • Capture a screencast if it involves UI
  • Capture a screen output if it involves cmdline
  • Stash logs

Attach these artifacts to any review. Be as verbose as possible, but be careful of logging PII (Personally Identifiable Information).

49 of 49

Questions?