Self-Documenting Code Chapter 32. Kinds of Comments  Repeat of code  Explanation of code  Marker in code  Summary of code  Description of code’s.

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

Internal Documentation Conventions. Kinds of comments javadoc (“doc”) comments describe the user interface: –What the classes, interfaces, fields and.
Revealing the Secrets of Self-Documenting Code Svetlin Nakov Telerik Corporation
TAKS Strategies Review. Strategies Review… Today we will review the strategies we have discussed over the course of this six weeks. Write down the strategies.
Debugging Introduction to Computing Science and Programming I.
16-Jun-15 javadoc. 2 Javadoc placement javadoc comments begin with /** and end with */ In a javadoc comment, a * at the beginning of the line is not part.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
Chapter 2: Input, Processing, and Output
CSE1301 Computer Programming: Lecture 13 Documentation.
26-Jun-15 Methods. About methods A method is a named group of declarations and statements If a method is in the same class, you execute those declarations.
Chapter 4 Sec. 4.1, 4.2, 4.4 Procedures (User-defined)
Algorithm Programming Coding Advices Bar-Ilan University תשס " ו by Moshe Fresko.
CODING Research Data Management. Research Data Management Coding When writing software or analytical code it is important that others and your future.
Lecture 3: Writing the Project Documentation Part I
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.
Software faults & reliability Presented by: Presented by: Pooja Jain Pooja Jain.
JS Arrays, Functions, Events Week 5 INFM 603. Agenda Arrays Functions Event-Driven Programming.
Structured COBOL Programming, Stern & Stern, 9th edition
4-1 Coding Complete COBOL Programs: The PROCEDURE DIVISION Chapter 4.
4-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)
1 Chapter 4. To familiarize you with methods used to 1. Access input and output files 2. Read data from an input file 3. Perform simple move operations.
Prologue: The Software Process. Main Phases of Software Process 1. Requirements Analysis (answers “WHAT?”) Specifying what the application must do 2.
Functions Part I (Syntax). What is a function? A function is a set of statements which is split off into a separate entity that can be used like a “new.
Testing Michael Ernst CSE 140 University of Washington.
Testing CSE 140 University of Washington 1. Testing Programming to analyze data is powerful It’s useless if the results are not correct Correctness is.
Methodologies. The Method section is very important because it tells your Research Committee how you plan to tackle your research problem. Chapter 3 Methodologies.
CS 325: Software Engineering March 24, 2015 Implementation Considerations Coding Standards Pair Programming Test-Driven Development.
Software Construction and Evolution - CSSE 375 Software Documentation 2 Shawn & Steve Left – Ideally, online documents would let you comment and highlight,
Test-based programming Ask: “What should this software do?” Write a test first “Does this software do X correctly?” Fill in the code, and keep working.
October, 2006 © Copyright 2006, Larry A. Beaty. Copying and distribution of this document is permitted in any medium, provided this notice is preserved.
Chapter 4 Recursion. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Explain the underlying concepts of recursion.
SE: CHAPTER 7 Writing The Program
Scientific Communication
Program style. Global area Place comment s at beginning of the program: 1.purpose 2.author 3.the date the program was written 4.the data and description.
B065: PROGRAMMING Sub Procedures I. Starter  Identify the separate tasks to be performed in the programming task below (break it down into numbered sections).
Documentation. Your documentation must fit the needs of your audience. It’s always better to say one thing that is useful, as opposed to many things that.
1 CS161 Introduction to Computer Science Topic #9.
EE3A1 Computer Hardware and Digital Design Lecture 2 Introduction to VHDL.
CIS 234: Project 1 Issues Dr. Ralph D. Westfall April, 2010.
Revealing the Secrets of Self-Documenting Code Svetlin Nakov Telerik Corporation
Scientific Debugging. Errors in Software Errors are unexpected behaviors or outputs in programs As long as software is developed by humans, it will contain.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
COIT29222 Structured Programming 1 COIT29222-Structured Programming Lecture Week 02  Reading: Textbook(4 th Ed.), Chapter 2 Textbook (6 th Ed.), Chapters.
Computing Higher – SD Unit - Topic 8 – Procedure and Standard Algorithms P Lynch, St Andrew’s High School Unit 2 Software Development Process Topic.
Starting Out With Java 5 Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Chapter 4 Slide #1.
1 Maintenance of Documentation and Documentation of Maintenance Quiz 7 Tori Bowman CSSE 375, Rose-Hulman October 1, 2006.
M1G Introduction to Programming 2 3. Creating Classes: Room and Item.
Testing CSE 160 University of Washington 1. Testing Programming to analyze data is powerful It’s useless (or worse!) if the results are not correct Correctness.
CPSC 217 T03 Week VI Part #1: A2 Post-Mortem and Functions Hubert (Sathaporn) Hu.
Chapter Topics 2.1 Designing a Program 2.2 Output, Input, and Variables 2.3 Variable Assignment and Calculations 2.4 Variable Declarations and Data Types.
Exceptions Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Flow of Control: Loops Module 4. Objectives Design a loop Use while, do, and for in a program Use the for-each with enumerations Use assertion checks.
Today… Modularity, or Writing Functions. Winter 2016CISC101 - Prof. McLeod1.
Today… Functions, Cont.: –Designing functions. –Functional Decomposition –Importing our own module –A demo: Functional solution to assignment 2. Winter.
Communicating in Code: Commenting Programming Studio Spring 2009 Note: several examples in this lecture taken from The Practice of Programming by Kernighan.
Pseudocode and comments
Reviewing Code A guide to smelling another developer’s source code.
Testing UW CSE 160 Winter 2017.
Chapter Topics 2.1 Designing a Program 2.2 Output, Input, and Variables 2.3 Variable Assignment and Calculations 2.4 Variable Declarations and Data Types.
© 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Communicating in Code: Commenting
Testing UW CSE 160 Winter 2016.
Communicating in Code: Commenting
Applied Software Project Management
Computing Fundamentals
CMSC 345 Programming.
Pseudocode and comments
HFOOAD Chapter 5 Interlude
Presentation transcript:

Self-Documenting Code Chapter 32

Kinds of Comments  Repeat of code  Explanation of code  Marker in code  Summary of code  Description of code’s intent  Information that can not possibly be expressed by the code itself

Repeat of Code  Tells what the code does in words  Basically useless

Explanation of Code  If code is so complicated or confusing  it needs explanation then it probably needs rewriting

Marker in Code  Typical example:  // TODO: make changes here

Summary of Code  Simple summary of code into one or two sentences  valuable

Description of Code’s Intent  A comment at the level of intent explains the purpose of a section of code.  Intent comments operate more at the level of the problem than at the level of the solution.  For example,  get current employee information is an intent comment, whereas  update employeeRecord object is a summary comment in terms of the solution.  Most useful

Information that can not…  Copyright notices  Confidentiality  Etc  Not what we’re talking about here

Balance  Certainly need a useful amount of commenting  Need to avoid too many comments  If it takes too much time to wade through comments then there’s too much  If there’s as much or more comments than code then there’s too much  Rule of thumb: 1 line of comment for 10 lines of code

Avoid /* */ /* here’s a difficult style of */ /* comment block to maintain */ /* */ /********************************* * class * * hard to maintain * **********************************/

Prefer // // this style is easier because you don’t // have to align the right hand column // you can just copy and paste the dashes // to start and end the block // /*********************************************** keep in mind that your code might not be viewed with a color-coding system ************************************************/

“It Takes Too Much Time”  It takes more time later and is harder to debug  Commenting after the fact is more difficult

Commenting Techniques  Endline comments  Tend to be cryptic  Hard to maintain  OK for data declarations  OK to denote bug fixes  OK for marking ends of blocks  Commenting individual lines  Complicated line of code  Bug repair

Commenting Techniques  Commenting paragraphs of code  Example from “recover.c” // get next cluster fat_index=0; if (cluster >= (FAT_ENTRIES/2)) { cluster = cluster-(FAT_ENTRIES/2); fat_index++; } cluster = *(F_tbl[fat_index]+cluster);

Paragraphs  Write comments at the level of the code’s intent  Focus on the code itself so that comments enhance good code  Focus paragraph comments on why rather than how

Code’s intent  Better comments  // find '$' in inputString  it indicates that the goal of the loop is to find a $.  it still doesn't give you much insight into why the loop would need to find a $  into the deeper intent of the loop.  Even better  // find the command-word terminator ($)

Coment the Why, not the How

Justify “good style” violations  Explain yourself when violating good style to prevent someone from rewriting code in a better style that could break your code.

Don’t comment tricky code, rewrite it  Except if you’re maintaining code and don’t have the latitude to change it.

Other Comments  Flags to bit level  // 0x80 – NW neighbor  // 0x40 – N neighbor  // 0x20 – NE neighbor  Allowable numeric ranges  // this index should never exceed because…  Global data  I prefer a naming convention to solve this:  Example: gMeaningfullyNamedVariable  Coded meanings (could use other methods to accomplish this)  Enumerated types  // 0 – generic land texture  // 1 – herbaceous rangeland texture  Units of numeric data (again, could use other methods)  Meters  Fahrenheit degrees  Etc

Rules of Thumb  Keep comments close to the code they describe  Document parameters where they’re declared  Use Javadoc  Differentiate between input and output  Again, I prefer naming

Routines  Say what the routine WON’T do, mention legal input values  Document global effects  Document source of algorithms  Avoid enormous comment blocks  I like some comments before every routine for visual delineation at the very least