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

Slides:



Advertisements
Similar presentations
Chapter 6 Writing a Program
Advertisements

Relationships and Dating
Thomas A. Stewart Literacy Test (OSSLT) Prep Guide 2013
Commenting and Naming Conventions
How the BMJ triages submitted manuscripts Richard Smith Editor, BMJ
Chapter 2: Understanding Structure
Test-Driven Development and Refactoring CPSC 315 – Programming Studio.
How to Solve Test Problems Test Taking Strategy
© The McGraw-Hill Companies, 2006 Chapter 15. © The McGraw-Hill Companies, 2006 Exceptions an exception is an event that occurs during the life of a program.
Internal Documentation Conventions. Kinds of comments javadoc (“doc”) comments describe the user interface: –What the classes, interfaces, fields and.
Software Engineering Reading Group: Clean Code Chapter 3 Led by Nicholas Vaidyanathan Lead Visionary, Visionary Software.
Phil 160 Kant.
Programming Logic and Design, Third Edition Comprehensive
PS4: Test Driven Development Based on Test Driven Development by Example By Kent Beck.
Programming Logic and Design Seventh Edition
API Design CPSC 315 – Programming Studio Fall 2008 Follows Kernighan and Pike, The Practice of Programming and Joshua Bloch’s Library-Centric Software.
Week 9: Methods 1.  We have written lots of code so far  It has all been inside of the main() method  What about a big program?  The main() method.
Coding Practices. What are Coding Practices ? Coding practices are a set of informal rules that the software development community has learned over time.
1 Sources:  SusanTurner - Napier University  C. Robson, Real World Research, Blackwell, 1993  Steve Collesano: Director, Corporate Research and Development.
Style Rules I. Style is important Everyone agrees that good style is important –Everyone agrees on most of the essentials –But some people have “religious.
 2007 Pearson Education, Inc. All rights reserved C Program Control.
An Object-Oriented Approach to Programming Logic and Design Chapter 6 Looping.
Algorithm Programming Coding Advices Bar-Ilan University תשס " ו by Moshe Fresko.
Effective Questioning in the classroom
Language Evaluation Criteria
Teens & Parents: How to Earn Your Parent’s Trust
Functional Question Higher (Algebra 5) For the week beginning ….
PHP meets MySQL.
Taking The Test Tips on Taking Multiple-Choice Tests The following tips are based in part on a document prepared by: Steve Houseworth, Duke University.
1 Project Information and Acceptance Testing Integrating Your Code Final Code Submission Acceptance Testing Other Advice and Reminders.
Week 5 - Wednesday.  What did we talk about last time?  Exam 1!  And before that?  Review!  And before that?  if and switch statements.
 Make sure you are subscribed to announcements on Moodle.  Activity 4 will be due 48hrs after your lab ends.
COMPUTER PROGRAMMING. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may repeat code.
LECTURE 38: REFACTORING CSC 395 – Software Engineering.
Chapter 6 Writing a Program John Keyser’s Modification of Slides by Bjarne Stroustrup
{ Philosophical Methods Exploring some ways people go about “thinking about thinking”.
Social Media Roundup Bad social media: 7 Ways to lose your audience.
Improving the Quality of Existing Code Svetlin Nakov Telerik Corporation
6 Chapter 61 Looping Programming Logic and Design, Second Edition, Comprehensive 6.
CPS120: Introduction to Computer Science Decision Making in Programs.
BIT 115: Introduction To Programming Instructor: Jon Peck
Higher English Close Reading Types of Questions Understanding Questions Tuesday 8 OctoberCMCM1.
Close Reading Intermediate 2. Time The Close Reading exam paper lasts for one hour. (Date and time for 2011: Friday 13 May, 1.00pm to 2.00pm.) NAB: Friday.
Self-Documenting Code Chapter 32. Kinds of Comments  Repeat of code  Explanation of code  Marker in code  Summary of code  Description of code’s.
Software Engineering CS3003 Lecture 4 Code bad smells and refactoring.
Refactoring. Refactoring Overview  What is refactoring?  What are four good reasons to refactor?  When should you refactor?  What is a bad smell (relative.
BMTRY 789 Lecture 11: Debugging Readings – Chapter 10 (3 rd Ed) from “The Little SAS Book” Lab Problems – None Homework Due – None Final Project Presentations.
Refactoring 2. Admin Blackboard Quiz Acknowledgements Material in this presentation was drawn from Martin Fowler, Refactoring: Improving the Design of.
REFACTORINGREFACTORING. Realities Code evolves substantially during development Requirements changes 1%-4% per month on a project Current methodologies.
Slides adapted from: Bjarne Stroustrup, Programming – Principles and Practice using C++ Chapter 6 Writing a Program Hartmut Kaiser
Copyright 2010, The World Bank Group. All Rights Reserved. Testing and Documentation Part II.
Selection Statements. Introduction Today we learn more about learn to make decisions in Turing ▫Nested if statements, ▫case statements.
Copyright © Curt Hill The IF Revisited If part 4 Style and Testing.
Responding to Reviewers. Rare to get an acceptance with no changes So two paths, rejection or revise and resubmit Rejection Revise and Resubmit.
SEG 4110 – Advanced Software Design and Reengineering Topic T Introduction to Refactoring.
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.
P ROGRAMMING L OGIC GWDA123 Sharon Kaitner, M.Ed. Winter 2015: Week 2.
FISH! PHILOSOPHY.
Journal 9/8/15 Is there anything in your life that you are 100% certain about? Anything you know for sure? Objective Tonight’s Homework To learn about.
Today… Modularity, or Writing Functions. Winter 2016CISC101 - Prof. McLeod1.
Chapter 6 Writing a Program Bjarne Stroustrup
Introduction to Exceptions in Java CS201, SW Development Methods.
CSCE 240 – Intro to Software Engineering Lecture 3.
//code refactoring Rename Method Introduce Assertion
Control Structure Testing
Controlling Program Flow
Refactoring.
Presentation transcript:

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

Comments Comments are not like Schindler’s List ◦ Comments are a necessary evil The proper use of comments is to compensate for our failure to express ourselves in code ◦ Comments are always failures

Harsh Words Every time you express yourself in code, you should pat yourself on the back. Every time you write a comment, you should grimace and feel the failure of your ability of expression

Why the hate? Comments lie ◦ The older the comment is, the farther away it is from the code it’s meant to explain, the more likely it is wrong Code changes and evolves ◦ Constantly moving around, being mish- mashed together in odd places

Programmer’s fault! Shouldn’t programmers be disciplined enough to maintain comments in a high state of repair, relevancy, and accuracy? Wouldn’t that energy be better spent making the code so expressive that comments were unnecessary?

Sometimes some is worse than none Inaccurate comments are worse than no comments at all ◦ Delude and mislead ◦ Set expectations that are left unfulfilled ◦ Lay down old rules that need not or should not be followed any longer Truth can be found in only one place: the code

Comments Do Not Make Up For Bad Code Clear and expressive code with few comments is far superior to cluttered and complex code with lots of comments

Explain yourself in code It takes only a few seconds of thought to explain most of your intent in code. In many cases it’s simply a matter of creating a function that says the same thing as the comment you want to write. Which would you rather see?

Good comments Legal comments Informative comments ◦ Can usually be replaced with cleaner code Explanation of Intent ◦ Can help rationalize seemingly odd decisions Clarification ◦ Risky, can be difficult to verify Explanation of Consequences TODO Comments Amplification ◦ Can make seemingly inconsequential more obvious Javadocs – Truly useful

Bad Comments ◦ Mumbling  Any comment that forces you to look in another module for the meaning of that comment has failed to communicate to you and is not worth the bits it consumes ◦ Redundant information ◦ Misleading comments

More Bad Mandated comments ◦ Clutter up code with unnecessary redundancy Journal comments ◦ Better put in source control logs Noise comments ◦ Add no new useful information ◦ Replace the temptation to create noise with the determination to clean your code. You’ll find it makes you a better and happier programmer.

Don’t use a comment when you can use a function or a variable

Position Markers Use banners like /* ACTIONS ----*/ sparingly

Closing Brace Comments ◦ Only makes sense for long functions with deeply nested functions ◦ …BUT ◦ We don’t like long functions with deeply nested structures…. ◦ …SO ◦ If you find yourself wanting to mark your closing braces, try to shorten your functions instead

Commented out code Few practices are as odious as commenting-out code. Don’t do this! ◦ Others who see the code won’t have the courage to delete it. They’ll think it’s there for a reason and is too important to delete. Commented-out code gathers like the dregs at the bottom of a bad bottle of wine

Use Source Control! There was a time, back in the sixties, when commenting-out code might have been useful… But we’ve had good source code control systems for a very long time now. Those systems will remember the code for us. We won’t lose it. Promise.

Nonlocal information Don’t put information in places where it may not be relevant Don’t put information about expected values of a function that are beyond that function’s control

TMI

Inobvious connection

Example of bad comments

Much better

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!