Revealing the Secrets of Self-Documenting Code Svetlin Nakov Telerik Corporation www.telerik.com.

Slides:



Advertisements
Similar presentations
Revealing the Secrets of Self-Documenting Code Svetlin Nakov Telerik Corporation For C# Developers.
Advertisements

Code Correctness, Readability, Maintainability Svetlin Nakov Telerik Corporation
Execute Blocks of Code Multiple Times Svetlin Nakov Telerik Corporation
Execute Blocks of Code Multiple Times Telerik Software Academy C# Fundamentals – Part 1.
Structured Design. 2 Design Quality – Simplicity “There are two ways of constructing a software design: One is to make it so simple that there are obviously.
1 Chapter Six Algorithms. 2 Algorithms An algorithm is an abstract strategy for solving a problem and is often expressed in English A function is the.
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
Introduction to C Programming
Chapter 10 THINKING IN OBJECTS 1 Object Oriented programming Instructor: Dr. Essam H. Houssein.
Variables and Powerful Naming Ryan Ruzich. Naming Considerations The most important consideration in naming a variable is that the name fully and accurately.
Software Engineering and Design Principles Chapter 1.
Computer Programming and Basic Software Engineering 4. Basic Software Engineering 1 Writing a Good Program 4. Basic Software Engineering 3 October 2007.
1 Introduction to Software Engineering Lecture 42 – Communication Skills.
Pseudocode and Algorithms
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Chapter 1 Principles of Programming and Software Engineering.
Introduction to C Programming
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Chapter 2 Principles of Programming & Software Engineering.
Algorithm Programming Coding Advices Bar-Ilan University תשס " ו by Moshe Fresko.
Fundamentals of Python: From First Programs Through Data Structures
DCT 1123 PROBLEM SOLVING & ALGORITHMS INTRODUCTION TO PROGRAMMING.
The Pseudocode Programming Process Chapter 9. Summary of Steps in Building Classes and Routines.
Comp 245 Data Structures Software Engineering. What is Software Engineering? Most students obtain the problem and immediately start coding the solution.
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.
Software Construction and Evolution - CSSE 375 Software Documentation 2 Shawn & Steve Left – Ideally, online documents would let you comment and highlight,
The Java Programming Language
High-Quality Programming Code Code Correctness, Readability, Maintainability, Testability, Etc. SoftUni Team Technical Trainers Software University
Quality Code academy.zariba.com 1. Lecture Content 1.Software Quality 2.Code Formatting 3.Correct Naming 4.Documentation and Comments 5.Variables, Expressions.
School of Computer Science & Information Technology G6DICP - Lecture 9 Software Development Techniques.
Loops Repeating Code Multiple Times SoftUni Team Technical Trainers Software University
SE: CHAPTER 7 Writing The Program
CS 320 Assignment 1 Rewriting the MISC Osystem class to support loading machine language programs at addresses other than 0 1.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
Improving the Quality of Existing Code Svetlin Nakov Telerik Corporation
CS 350, slide set 5 M. Overstreet Old Dominion University Spring 2005.
Code Correctness, Readability, Maintainability Svetlin Nakov Telerik Software Academy academy.telerik.com Technical Trainer
Code Documentation and Comments
Documentation Dr. Andrew Wallace PhD BEng(hons) EurIng
Self-Documenting Code Chapter 32. Kinds of Comments  Repeat of code  Explanation of code  Marker in code  Summary of code  Description of code’s.
REFACTORINGREFACTORING. Realities Code evolves substantially during development Requirements changes 1%-4% per month on a project Current methodologies.
Comments in the Program and Self-Documenting Code
Revealing the Secrets of Self-Documenting Code Nikolay Kostov Telerik Software Academy academy.telerik.com Senior Software Developer and Technical Trainer.
CIS 234: Project 1 Issues Dr. Ralph D. Westfall April, 2010.
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Chapter 2 Principles of Programming & Software Engineering.
© 2006 Pearson Addison-Wesley. All rights reserved 2-1 Chapter 2 Principles of Programming & Software Engineering.
ANU COMP2110 Software Design in 2003 Lecture 10Slide 1 COMP2110 Software Design in 2004 Lecture 12 Documenting Detailed Design How to write down detailed.
Data Structure Introduction Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2010.
High-Quality Programming Code Code Correctness, Readability, Maintainability Svetlin Nakov Technical Trainer Software University
Refactoring Agile Development Project. Lecture roadmap Refactoring Some issues to address when coding.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 4: Introduction to C: Control Flow.
ATS Programming Short Course I INTRODUCTORY CONCEPTS Tuesday, Feb 10th, 2009 Introduction to Programming.
Designing classes How to write classes in a way that they are easily understandable, maintainable and reusable 6.0.
Communicating in Code: Commenting Programming Studio Spring 2009 Note: several examples in this lecture taken from The Practice of Programming by Kernighan.
High-Quality Programming Code Code Correctness, Readability, Maintainability, Testability, Etc. SoftUni Team Technical Trainers Software University
Principles of Programming & Software Engineering
Code Documentation and Comments in the Program
Repeating Code Multiple Times
16. Controlling Loops CSC-3004 Introduction to Software Development
Principles of Programming and Software Engineering
CMPT 201 Functions.
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
© 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Communicating in Code: Commenting
For loops.
16. Controlling Loops CSC-3004 Introduction to Software Development
Chapter 1: Computer Systems
Communicating in Code: Commenting
Week 4 Lecture-2 Chapter 6 (Methods).
Presentation transcript:

Revealing the Secrets of Self-Documenting Code Svetlin Nakov Telerik Corporation

1. The Concept of Self-Documenting Code 2. Bad Comments 3. Good Programming Style 4. To Comment or Not to Comment? 5. Key Points commenting 6. Recommended practices 2

The Concept of Self-Documenting Code

 Consists of information both inside the source- code listings and outside them  External construction documentation tends to be at a high level compared to the code  At a low level compared to the documentation from problem definition, requirements, and architecture 4

 Main contributor to code-level documentation  Program structure  Straight-forward and easily understandable approaches  Good naming approach  Clear layout and minimized complexity 5

6 public static List FindPrimes(int start, int end) { // Create new list of integers // Create new list of integers List primesList = new List (); List primesList = new List (); // Perform a loop from start to end // Perform a loop from start to end for (int num = start; num <= end; num++) for (int num = start; num <= end; num++) { // Declare boolean variable, initially true // Declare boolean variable, initially true bool prime = true; bool prime = true; // Perform loop from 2 to sqrt(num) // Perform loop from 2 to sqrt(num) for (int div = 2; div <= Math.Sqrt(num); div++) for (int div = 2; div <= Math.Sqrt(num); div++) { // Check if div divides num with no remainder // Check if div divides num with no remainder if (num % div == 0) if (num % div == 0) { // We found a divider -> the number is not prime // We found a divider -> the number is not prime prime = false; prime = false; // Exit from the loop // Exit from the loop break; break; } (continues on the next slide)

7 // Continue with the next loop value // Continue with the next loop value } // Check if the number is prime // Check if the number is prime if (prime) if (prime) { // Add the number to the list of primes // Add the number to the list of primes primesList.Add(num); primesList.Add(num); } } // Return the list of primes // Return the list of primes return primesList; return primesList;}

8 public static List FindPrimes(int start, int end) { List primesList = new List (); List primesList = new List (); for (int num = start; num <= end; num++) for (int num = start; num <= end; num++) { bool isPrime = IsPrime(num); bool isPrime = IsPrime(num); if (isPrime) if (isPrime) { primesList.Add(num); primesList.Add(num); } } return primesList; return primesList;} (continues on the next slide) Good code does not need comments. It is self-explaining.

9 private static bool IsPrime(int num) { bool isPrime = true; bool isPrime = true; int maxDivider = Math.Sqrt(num); int maxDivider = Math.Sqrt(num); for (int div = 2; div <= maxDivider; div++) for (int div = 2; div <= maxDivider; div++) { if (num % div == 0) if (num % div == 0) { // We found a divider -> the number is not prime // We found a divider -> the number is not prime isPrime = false; isPrime = false; break; break; } } return isPrime; return isPrime;} Good methods have good name and are easy to read and understand. This comment explain non-obvious details. It does not repeat the code.

10 for (i = 1; i <= num; i++) { meetsCriteria[i] = true; meetsCriteria[i] = true;} for (i = 2; i <= num / 2; i++) { j = i + i; j = i + i; while (j <= num) { meetsCriteria[j] = false; meetsCriteria[j] = false; j = j + i; j = j + i; }} for (i = 1; i <= num; i++) { if (meetsCriteria[i]) if (meetsCriteria[i]) { Console.WriteLine(i + " meets criteria."); Console.WriteLine(i + " meets criteria."); }} Uninformative variables. Crude layout

11 for (primeCandidate = 1; primeCandidate <= num; primeCandidate++) { isPrime[primeCandidate] = true; isPrime[primeCandidate] = true;} for (int factor = 2; factor < (num / 2); factor++) { int factorableNumber = factor + factor; int factorableNumber = factor + factor; while (factorableNumber <= num) while (factorableNumber <= num) { isPrime[factorableNumber] = false; isPrime[factorableNumber] = false; factorableNumber = factorableNumber + factor; factorableNumber = factorableNumber + factor; }} for (primeCandidate = 1; primeCandidate <= num; primeCandidate++) { if (isPrime[primeCandidate]) if (isPrime[primeCandidate]) { Console.WriteLine(primeCandidate + " is prime."); Console.WriteLine(primeCandidate + " is prime."); }}

 Code that relies on good programming style to carry major part of the information intended for the documentation  Self-documenting code fundamental principles 12 The best documentation is the code itself. Do not document bad code, rewrite it! Make the code self-explainable and self- documenting, easy to read and understand.

 Classes  Does the class’s interface present a consistent abstraction?  Does the class’s interface make obvious how you should use the class?  Is the class well named, and does its name describe its purpose?  Can you treat the class as a black box? 13

 Methods  Does each routine’s name describe exactly what the method does?  Does each method perform one well-defined task?  Data Names  Are type names descriptive enough to help document data declarations?  Are variables used only for the purpose for which they’re named? 14

 Data Names  Does naming conventions distinguish among type names, enumerated types, named constants, local variables, class variables, and global variables?  Others  Are data types simple so that they minimize complexity?  Are related statements grouped together? 15

Usage of Comments

"Everything the compiler needs to know is in the code!" It sounds like you guys have never had to modify someone else’s code!

 Effective comments do not repeat the code  They explain it at higher level and reveal non- obvious details  The best software documentation is the source code itself – keep it clean and readable  Self-documenting code is code that is self- explainable and does not need comments  Simple design, small well named methods, strong cohesion and loose coupling, simple logic, good variable names, good formatting, … 18

 Misleading comments 19 // write out the sums 1..n for all n from 1 to num current = 1; previous = 0; sum = 1; for ( int i = 0; i < num; i++ ) { System.out.println( "Sum = " + sum ); System.out.println( "Sum = " + sum ); sum = current + previous; sum = current + previous; previous = current; previous = current; current = sum; current = sum;} What problem does this algorithm solve? Have quesed that it computes the first num Fibonacci numbers

 Repeat of the code 20 // set product to "base" product = base; // loop from 2 to "num" for ( int i = 2; i <= num; i++ ) { // multiply "base" by "product" // multiply "base" by "product" product = product * base; product = product * base;} System.out.println( "Product = " + product ); Your best quess? Raises an integer base to the integer power num

 Poor coding style 21 // compute the square root of Num using the Newton-Raphson approximation r = num / 2; while ( abs( r - (num/r) ) > TOLERANCE ) { r = 0.5 * ( r + (num/r) ); r = 0.5 * ( r + (num/r) );} System.out.println( "r = " + r );

 Use commenting styles that don’t break down or discourage modification 22 // Variable Meaning // // xPos XCoordinate Position (in meters) // yPos YCoordinate Position (in meters)  Integrate commenting into your development style

 Write comments at the level of the code’s intent 23 // find the command-word terminator ($) done = false; maxLen = inputString.Length; i = 0; while (!done && (i < maxLen)) { if (inputString[i] == '$') if (inputString[i] == '$') { done = true; done = true; } else { else { i++; i++; }}

 Focus your documentation efforts on the code itself 24 // find the command-word terminator foundTheTerminator = false; maxCommandLength = inputString.Length(); testCharPosition = 0; while (!foundTheTerminator && (testCharPosition < maxCommandLength)) { if (inputString[testCharPosition] == COMMAND_WORD_TERMINATOR) if (inputString[testCharPosition] == COMMAND_WORD_TERMINATOR) { foundTheTerminator = true; foundTheTerminator = true; terminatorPosition = testCharPosition; terminatorPosition = testCharPosition; } else else { testCharPosition = testCharPosition + 1; testCharPosition = testCharPosition + 1; }}

 Focus paragraph comments on the why rather than the how 25 // establish a new account if ( accountType == AccountType.NewAccount ) {......}  Use comments to prepare the reader for what is to follow  Avoid abbreviations

26  Comment anything that gets around an error or an undocumented feature in a language or an environment  Justify violations of good programming style  Don’t comment tricky code – rewrite it  Use built-in features for commenting – XML comments, JavaDoc, etc.

27  Describe the design approach to the class  Describe limitations, usage assumptions, and so on  Comment the class interface  Don’t document implementation details in the class interface  Describe the purpose and contents of each file  Give the file a name related to its contents

Questions?