Software Engineering Reading Group: Clean Code Chapter 3 Led by Nicholas Vaidyanathan Lead Visionary, Visionary Software.

Slides:



Advertisements
Similar presentations
11-Jun-14 The assert statement. 2 About the assert statement The purpose of the assert statement is to give you a way to catch program errors early The.
Advertisements

Reviewing your Program CS 5010 Program Design Paradigms “Bootcamp” Lesson 2.4 © Mitchell Wand, This work is licensed under a Creative Commons.
Software Engineering Reading Group: Clean Code Chapter 4 Led by Nicholas Vaidyanathan Lead Visionary, Visionary Software.
CS 350 – Software Design The Bridge Pattern – Chapter 10 Most powerful pattern so far. Gang of Four Definition: Decouple an abstraction from its implementation.
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
Programming Logic and Design, Third Edition Comprehensive
Gary MarsdenSlide 1University of Cape Town Statements & Expressions Gary Marsden Semester 2 – 2000.
API Design CPSC 315 – Programming Studio Fall 2008 Follows Kernighan and Pike, The Practice of Programming and Joshua Bloch’s Library-Centric Software.
Chapter 2: Algorithm Discovery and Design
CS 201 Functions Debzani Deb.
Introduction to C Programming
Algorithm Programming Coding Advices Bar-Ilan University תשס " ו by Moshe Fresko.
Programming – Touch Sensors Intro to Robotics. The Limit Switch When designing robotic arms there is always the chance the arm will move too far up or.
Algebra 1 Ch 4.2 – Graphing Linear Equations. Objective Students will graph linear equations using a table. Students will graph linear equations using.
OBJECT ORIENTED PROGRAMMING IN C++ LECTURE
Test Taking Tips How to help yourself with multiple choice and short answer questions for reading selections A. Caldwell.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
Group practice in problem design and problem solving
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Clean Code III Functions Object Mentor, Inc. Copyright  2008 by Object Mentor, Inc All Rights Reserved objectmentor.com Robert C. Martin.
1 Some Patterns of Novice Programs Author : Eugene Wallingford ; Dan Steinberg ; Robert Duvall ; Ralph Johnson Source : PLoP 2004 Advisor : Ku-Yaw Chang.
Design-Making Projects Work (Chapter7) n Large Projects u Design often distinct from analysis or coding u Project takes weeks, months or years to create.
Object Oriented Programming Elhanan Borenstein Lecture #4.
CS 350 – Software Design The Object Paradigm – Chapter 1 If you were tasked to write code to access a description of shapes that were stored in a database.
How to think through your program [ principles of good program design ] Rachel Denison MATLAB for Cognitive Neuroscience ICN, 13 December 2007.
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
SE: CHAPTER 7 Writing The Program
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
Chapter 7 File I/O 1. File, Record & Field 2 The file is just a chunk of disk space set aside for data and given a name. The computer has no idea what.
CS 350 – Software Design The Strategy Pattern – Chapter 9 Changes to software, like other things in life, often focus on the immediate concerns and ignore.
Introduction to Exception Handling and Defensive Programming.
6 Chapter 61 Looping Programming Logic and Design, Second Edition, Comprehensive 6.
CPS120: Introduction to Computer Science Decision Making in Programs.
Current Assignments Homework 2 is available and is due in three days (June 19th). Project 1 due in 6 days (June 23 rd ) Write a binomial root solver using.
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
Making Decisions (True or False) Relational Operators >greater than =greater than or equal to
First Steps in Modularization. Simple Program Design, Fourth Edition Chapter 8 2 Objectives In this chapter you will be able to: Introduce modularization.
22/11/ Selection If selection construct.
Software Engineering CS3003 Lecture 4 Code bad smells and refactoring.
REFACTORINGREFACTORING. Realities Code evolves substantially during development Requirements changes 1%-4% per month on a project Current methodologies.
The Software Development Process
First Steps in Modularization. Simple Program Design, Fourth Edition Chapter 8 2 Objectives In this chapter you will be able to: Introduce modularization.
1 CS161 Introduction to Computer Science Topic #9.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Copyright © Curt Hill The IF Revisited If part 4 Style and Testing.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
Algebra – Solving Systems of Linear Inequalities Objective: Students will solve systems of linear inequalities by graphing.
CMSC 330: Organization of Programming Languages Operational Semantics a.k.a. “WTF is Project 4, Part 3?”
CHAPTER 14 Classes, Objects, and Games XNA Game Studio 4.0.
ICS3U_FileIO.ppt File Input/Output (I/O)‏ ICS3U_FileIO.ppt File I/O Declare a file object File myFile = new File("billy.txt"); a file object whose name.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Refactoring Agile Development Project. Lecture roadmap Refactoring Some issues to address when coding.
The Last Lecture CS 5010 Program Design Paradigms "Bootcamp" Lesson © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial.
Written by: Dr. JJ Shepherd
Writing an Essay. Reading a Primary Source: Step 1 Who wrote this document? In the first place, you need to know how this document came to be created.
(c) University of Washington10-1 CSC 143 Java Errors and Exceptions Reading: Ch. 15.
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
Duke CPS Programming Heuristics l Identify the aspects of your application that vary and separate them from what stays the same ä Take what varies.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
Today… Modularity, or Writing Functions. Winter 2016CISC101 - Prof. McLeod1.
Introduction to Exceptions in Java CS201, SW Development Methods.
CSCE 240 – Intro to Software Engineering Lecture 3.
Discussion 4 eecs 183 Hannah Westra.
Chapter 3 of Programming Languages by Ravi Sethi
Introduction To Repetition The for loop
C++ coding standard suggestion… Separate reasoning from action, in every block. Hi, this talk is to suggest a rule (or guideline) to simplify C++ code.
Reviewing your Program
ECS10 10/10
Phil Tayco Slide version 1.0 Created Oct 2, 2017
Selection Statements Chapter 3.
Presentation transcript:

Software Engineering Reading Group: Clean Code Chapter 3 Led by Nicholas Vaidyanathan Lead Visionary, Visionary Software Solutions

Functions matter Can you understand in 3 minutes?

How about this?

Small! “The first rule of functions is that they should be small.” “The second rule of functions is that they should be smaller than that.” “Functions should not be 100 lines long. Functions should hardly ever be 20 lines long.” Beck’s Sparkle ◦ “I was used to functions in Swing programs that took up miles of vertical space. Every function in this program was just two, or three, or four lines long. Each was transparently obvious. Each told a story. And each led you to the next in a compelling order. That’s how short your functions should be! 3”

Blocks and Indenting This implies that the blocks within if statements, else statements, while statements, and so on should be one line long. Probably that line should be a function call. This also implies that functions should not be large enough to hold nested structures. Therefore, the indent level of a function should not be greater than one or two.

Do One Thing FUNCTIONS SHOULD DO ONE THING. THEY SHOULD DO IT WELL. THEY SHOULD DO IT ONLY. If a function does only those steps that are one level below the stated name of the function, then the function is doing one thing. Another way to know that a function is doing more than “one thing” is if you can extract another function from it with a name that is not merely a restatement of its implementation [G34]. Functions that do one thing cannot be reasonably divided into sections.

One Level of Abstraction per Function In order to make sure our functions are doing “one thing,” we need to make sure that the statements within our function are all at the same level of abstraction. Mixing levels of abstraction within a function is always confusing. ◦ Readers may not be able to tell whether a particular expression is an essential concept or a detail. ◦ like broken windows, once details are mixed with essential concepts, more and more details tend to accrete within the function.

The Stepdown Rule We want the code to read like a top-down narrative we want to be able to read the program as though it were a set of TO paragraphs, each of which is describing the current level of abstraction and referencing subsequent TO paragraphs at the next level down. ◦ To include the setups and teardowns, we include setups, then we include the test page content, and then we include the teardowns. ◦ To include the setups, we include the suite setup if this is a suite, then we include the regular setup. ◦ To include the suite setup, we search the parent hierarchy for the “SuiteSetUp” page and add an include statement with the path of that page. ◦ To search the parent...

Switch Statements Can be hard to read Do N things by nature

Poor Payroll Large, multiple reasons for change Does more than 1 thing Violates Single Responsibility Principle Violates Open-Closed Principle Opens door for even worse! ◦ isPayday(Employee e, Date date) ◦ deliverPay(Employee e, Money pay)

Bury Switch with OOP Use Abstract Factory that delegates to Employee instances through polymorphism My general rule for switch statements is that they can be tolerated if they appear only once, are used to create polymorphic objects, and are hidden behind an inheritance relationship so that the rest of the system can’t see them [G23].

Better Payroll

Use Descriptive Names Remember Ward’s principle: “You know you are working on clean code when each routine turns out to be pretty much what you expected.” Don’t be afraid to make a name long. A long descriptive name is better than a short enigmatic name. ◦ A long descriptive name is better than a long descriptive comment. Choosing descriptive names will clarify the design of the module in your mind and help you to improve it. Be consistent in your names.

Function Arguments Ideal Number of arguments for a function is 0 (niladic) Next comes monadic (1), followed closely by dyadic (2) Triadic (3) should be avoided whenever possible Polyadic (3+) requires justification…and really shouldn’t be done Arguments take a lot of conceptual power ◦ They may mix abstractions Arguments are hard to test ◦ Combinatorial explosion of values for inputs Output arguments are harder to understand than input arguments.

Common monadic forms Common reasons to pass an argument ◦ Ask a question about it  boolean fileExists(“MyFile”) ◦ Transformation and return  InputStream fileOpen(“MyFile”)  transforms a file name String into an InputStream return value. ◦ Event  void passwordAttemptFailedNtimes(int attempts)  Use with care. It should be very clear to the reader this is an event ◦ Try to avoid any monadic functions that don’t follow these forms, for example

Monadic mistakes void includeSetupPageInto(StringB uffer pageText) StringBuffer transform(StringBuffer in) > void transform(StringBuffer out)

Flag Arguments Flag arguments are ugly. ◦ Passing a boolean into a function is a truly terrible practice. Complicates the signature of a method, and loudly proclaims the function does more than 1 thing!

Dyadic Functions writeField(name) > writeField(output-Stream, name) ◦ First is easily parsed, second takes a moment until we figure out to ignore the first parameter But we shouldn’t have code that we ignore! Two arguments make better sense in makePoint(int x, int Y) ◦ Ordered components of a single value! Even obvious dyadic functions like assertEquals(expected, actual) are problematic. ◦ Must memorize the parameter order, since there is no natural ordering

Triads Worse than dyads It’s likely that you should use an Argument Object ◦ When groups of variables are passed together, the way x and y are in the example above, they are likely part of a concept that deserves a name of its own.

Verbs and Keywords Choosing good names for a function can help mitigate parameter ordering issues In the case of a monad, the function and argument should form a very nice verb/noun pair. ◦ write(name)  writeField(name)  writeField() is a keyword form of a function name, it encodes the name of arguments into the function name

Have no Side Effects Side effects are LIES ◦ Your function promises to do one thing, but also does other hidden things Side effects create temporal couplings which are confusing and often the source of bugs

What’s Wrong here?

Output Arguments appendFooter(s); ◦ Does this function append s as the footer to something? ◦ Or does it append some footer to s ? Is s an input or an output? Anything that forces you to check the function signature is equivalent to a double- take. It’s a cognitive break and should be avoided. much of the need for output arguments disappears in OO languages because this is intended to act as an output argument.

Command Query Separation Functions should either do something or answer something, but not both. ◦ Either your function should change the state of an object, or it should return some information about that object. ◦ Doing both often leads to confusion.

Separate! public boolean set(String attribute, String value); if (set("username", "unclebob"))... ◦ Is it asking whether the “ username ” attribute was previously set to “ unclebob ”? ◦ is it asking whether the “username” attribute was successfully set to “unclebob”?

Prefer Exceptions to returning Error Codes Returning error codes is a subtle violation of Command Query Separation ◦ Promotes commands being used as predicates in if statements, leading to deep nesting Extract try/catch blocks Error Handling is One Thing

Don’t Repeat Yourself Duplication is a problem ◦ Requires modification in multiple places on changes..lots of opportunity for error Duplication may be the root of all evil in software. ◦ Many principles and practices have been created for the purpose of controlling or eliminating it.

Structured Programming Edsger Djikstra Rules ◦ Every function and every block within a function should have one entry and one exit ◦ Only 1 return statement ◦ No break or continue in loops ◦ Never any gotos

How Do You Write Functions Like This? Writing software is like any other kind of writing ◦ When you write a paper or an article,you get your thoughts down first, then you massage it until it reads well. ◦ Refactor, Refactor, Refactor! ◦ But write Unit Tests that stress the original first, and keep them passing!