Reviewing Code A guide to smelling another developer’s source code.

Slides:



Advertisements
Similar presentations
Chapter 29: Integration Jacob Harper. The Integration Approach The order of adding components to a system is crucial Benefits to careful integration –
Advertisements

Refactoring Overview  What is refactoring?  What are four good reasons to refactor?  When should you refactor?  What is a bad smell (relative to refactoring.
Chapter 8 Runtime Support. How program structures are implemented in a computer memory? The evolution of programming language design has led to the creation.
Software Engineering and Design Principles Chapter 1.
Debugging CPSC 315 – Programming Studio Fall 2008.
Computer Programming and Basic Software Engineering 4. Basic Software Engineering 1 Writing a Good Program 4. Basic Software Engineering 3 October 2007.
Introduction to Refactoring Excerpted from ‘What is Refactoring?’ by William C. Wake and Refactoring: Improving the Design of Existing Code by Martin Fowler.
CODING Research Data Management. Research Data Management Coding When writing software or analytical code it is important that others and your future.
Chapter 24 - Quality Management Lecture 1 1Chapter 24 Quality management.
Maintenance Refactoring and Code Smells. Where are we? Over the semester we have talked about Software Engineering. The overall goal of software engineering.
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
Copyright © 2002, Systems and Computer Engineering, Carleton University a-JavaReview.ppt * Object-Oriented Software Development Unit.
Software Testing and Maintenance 1 Code Review  Introduction  How to Conduct Code Review  Practical Tips  Tool Support  Summary.
C++ Memory Overview 4 major memory segments Key differences from Java
SOFTWARE METRICS. Software Process Revisited The Software Process has a common process framework containing: u framework activities - for all software.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
Homework due Test the random number generator Create a 1D array of n ints Fill the array with random numbers between 0 and 100 Compute and report the average.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Refactoring. DCS – SWC 2 Refactoring ”A change made to the internal structure of software to make it easier to understand and cheaper to modify without.
Copyright © 2016 Curt Hill Static Code Analysis What it is and does.
Implementation Topics Describe –Characteristics of good implementations –Best practices to achieve them Understand role of comments Learn debugging techniques.
CSC 108H: Introduction to Computer Programming
Principles and examples
Memory Management.
Software Metrics 1.
Overview 4 major memory segments Key differences from Java stack
Types for Programs and Proofs
Steve Chenoweth Office Phone: (812) Cell: (937)
Engineering and Debugging an App Chapter 15
COMP280:Introduction to Software Development Week 12, Lecture 34
Review: Two Programming Paradigms
Chapter 5 Conclusion CIS 61.
Abstract Classes and Inheritence Operator over-loading
Pointers Revisited What is variable address, name, value?
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
Rename Local Variable Refactoring Instances
Refactoring SENG 301 © Refactoring: Improving the Design of Existing Code, Martin Fowler, Kent Beck Source:
This pointer, Dynamic memory allocation, Constructors and Destructor
Refactoring and Code Smells
Software Quality Engineering
Object Oriented Programming COP3330 / CGS5409
Overview 4 major memory segments Key differences from Java stack
Objects First with Java
SE-2811 Software Component Design
Software Testing and Maintenance Modifying Code
CSCE 315 – Programming Studio, Fall 2017 Tanzir Ahmed
Lesson 16: Functions with Return Values
Improving the structure of existing code
Refactoring and Code Smells
Learning Intention I will learn about evaluating a program.
Code Style.
Dr. Rob Hasker SE 3800 Note 9 Reviews.
Debugging “Why you were up till 2AM”
Design Joshua Lewis Project questions Assignment questions
Refactoring and Code Smells
Tonga Institute of Higher Education IT 141: Information Systems
Min Heap Update E.g. remove smallest item 1. Pop off top (smallest) 3
Data Structures & Algorithms
Tonga Institute of Higher Education IT 141: Information Systems
Refactoring and Code Smells
Naming & Code Reviews.
Webnote 503 THE EXTENDED ESSAY
Review of Previous Lesson
ENERGY 211 / CME 211 Lecture 30 December 5, 2008.
Lattice Motivation: Maintaining relationships between elements of two
Copy Constructors and Overloaded Assignment
Refactoring.
SPL – PS2 C++ Memory Handling.
Chapter 9: Implementation
Refactoring and Code Smells
Presentation transcript:

Reviewing Code A guide to smelling another developer’s source code

How much should you review? Option 1 – All the Code Option 2 – Parts of the Code Read through every single line Attempt to understand the general structure of everything Read a function Understand the function Find other code related to the function E.g. loading data into a global variable that the function uses Repeat with another function

Review Fewer than 400 LoC (per person) Your brain can only process so much information at a time Reviewing more than 400 LoC means you have to keep track of too many variables, functions, classes, etc. Try to limit yourself to 30-60 minutes of defect discovery This is the time you are reading code and writing notes, not writing the actual code review assignment https://smartbear.com/learn/code-review/best-practices-for-peer-code-review/

Identifying Problems by Smelling Them A Code Smell is not a bug The code works and works correctly A Code Smell likely indicates a weakness in program design i.e. The risk of future bugs arising from this smell is high There are many different kinds of code smell Refactoring: Improving the Design of Existing Code, by Martin Folwer

Long Functions Can Be Bad If a function is longer than 10 lines of code, it smells “I just need to add two more lines to this function…” Repeat the above enough times and your function has grown too large The longer a function is, the harder it is to: Read it Understand it Maintain it

Temporary Variables Can Be Bad Created for convenience by the programmer early on in the scope Typically used as in a complex function or algorithm The temporary variable only receives a real value during some circumstance Example: within an if-statement The variables are usually empty (i.e. have no value) or initialized to something meaningless Harder to debug

Example of a Long Function More Code Here

Example of Temporary Variables If possible, initialize a variable when you declare it.

Which scope should variables belong to? The length variable is only used in the if-statement. dist, dist_first, and dist_last are used in the else statement.

Losing Information on Reassignment By reassigning these to intersection1/2, we’ve actually lost information.

Extraneous Code Do we need to assign the result to length? Why not return directly? Do we lose any readability in doing so?

Comments Can Be Bad Commenting can help explain unintuitive code But the code is still unintuitive… can this be fixed? Some programmers comment out code There is no reason for this – subversion has a history of all your code Comments can become out-dated The code and the comments directly contradict each other Comments are good if… They explain why something is coded a certain way They explain a complex algorithm that is being used

Duplicating Code Duplicated Code smells bad because… It can likely be extracted into its own function It was likely copy-pasted by the programmer

Duplicated Code Example

Duplicated Code Example This code has been copy-pasted. It is clearly duplicate code.

Poor Commenting Example These comments tell the what is about to happen. But they don’t tell me why.

Removing Duplication; Adding the Why

Learning more About Code Smells Interactive Website https://sourcemaking.com/refactoring/smells The Book Refactoring: Improving the Design of Existing Code The Taxonomy Mäntylä, M. V. and Lassenius, C. "Subjective Evaluation of Software Evolvability Using Code Smells: An Empirical Study". Journal of Empirical Software Engineering, vol. 11, no. 3, 2006, pp. 395-431.

An (incomplete) C++ Specific Checklist Does the code compile without warnings? Are there any expensive copies being made? Which objects would be expensive to copy? Are the destructors simple and intuitive? Is there an excessive use of pointers? Manual memory management is hard; rely on the STL for most of your dynamic memory allocation Are variables that can be marked const marked const?

A Good Code Review Is specific Cites examples in the code Is not negative Don’t be mean, be constructive Is not opinionated Not everyone agrees on style and formatting