Writing correct and understandable code in C++ and Matlab

Slides:



Advertisements
Similar presentations
1 Java Programming Basics SE-1011 Dr. Mark L. Hornick.
Advertisements

Internal Documentation Conventions. Kinds of comments javadoc (“doc”) comments describe the user interface: –What the classes, interfaces, fields and.
Coding Standards for Java An Introduction. Why Coding Standards are Important? Coding Standards lead to greater consistency within your code and the code.
Documentation 1 Comprehending the present – Investing in the future.
Computer Programming and Basic Software Engineering 4. Basic Software Engineering 1 Writing a Good Program 4. Basic Software Engineering 3 October 2007.
Algorithm Programming Coding Advices Bar-Ilan University תשס " ו by Moshe Fresko.
Chapter 2 Build Your First Project A Step-by-Step Approach 2 Exploring Microsoft Visual Basic 6.0 Copyright © 1999 Prentice-Hall, Inc. By Carlotta Eaton.
3.1 Documentation & Java Language Elements Purpose of documentation Assist the programmer with developing the program Assist other programers who.
Elements of a C++ program 1. Review Algorithms describe how to solve a problem Structured English (pseudo-code) Programs form that can be translated into.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
The Java Programming Language
CSC204 – Programming I Lecture 4 August 28, 2002.
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
Paul Mundy Editing step by step How an expert does it.
Classes CS 21a: Introduction to Computing I First Semester,
Java Syntax and Style JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin,
Java Coding Standards and Best Practices Coding Standards Introduction: After completing this chapter, you will able to keep your code up to standards.
Feb. 8, 2008 UHCO Graduate Course in MATLAB Core Programming Module Best Practices Core Grant Programming Module Best Practices (Coding Conventions) General.
Arrays. The array data structure Array is a collection of elements, that have the same data type Integers (int) Floating point numbers (float, double)
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
A First Book of ANSI C Fourth Edition
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 1: Computer Systems Presentation slides for Java Software Solutions for AP* Computer Science.
Chapter 2 Build Your First Project A Step-by-Step Approach 2 Exploring Microsoft Visual Basic 6.0 Copyright © 1999 Prentice-Hall, Inc. By Carlotta Eaton.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
The Essentials of a Java Program JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin,
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Emdeon Office Batch Management Services This document provides detailed information on Batch Import Services and other Batch features.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Asst.Prof.Dr. Tayfun ÖZGÜR
Chapter 1.2 Introduction to C++ Programming
Chapter 9: Value-Returning Functions
Chapter 6 - Functions modular programming general function format
More Sophisticated Behavior
CS1020 – Data Structures And Algorithms 1 AY Semester 2
Types for Programs and Proofs
APPENDIX a WRITING SUBROUTINES IN C
The Hardware/Software Interface CSE351 Winter 2013
Revision Lecture
EGR 115 Introduction to Computing for Engineers
University of Central Florida COP 3330 Object Oriented Programming
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.
Programmazione I a.a. 2017/2018.
2.1 Parts of a C++ Program.
Use of Mathematics using Technology (Maltlab)
Winter 2018 CISC101 11/27/2018 CISC101 Reminders
Chapter 1: Computer Systems
Govt. Polytechnic,Dhangar
Introduction C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell.
Applying OO Concepts Using Java
Tonga Institute of Higher Education
CISC/CMPE320 - Prof. McLeod
Program Design Language (PDL)
Beginning Style 27-Feb-19.
COP 3330 Object-oriented Programming in C++
How to organize and document your classes
Functions continued.
Classes CS 21a: Introduction to Computing I
Arrays.
Lecture 13 Teamwork Bryan Burlingame 1 May 2019.
Variables in C Topics Naming Variables Declaring Variables
Pointers, Dynamic Data, and Reference Types
Variables and Constants
Programming The ideal style of programming is Structured or
Getting Started With Coding
Presentation transcript:

Writing correct and understandable code in C++ and Matlab Andras Lasso Laboratory for Percutaneous Surgery (Perk Lab) School of Computing Queen’s University, Kingston, ON, Canada

1992 1984 2009 2000 Laboratory for Percutaneous Surgery (The Perk Lab) – Copyright © Queen’s University, 2012

Organization of information Computed data Source data Source code Compiled code Docs Tools 1. Data, documents, and source code in separate folders, backed up Laboratory for Percutaneous Surgery (The Perk Lab) – Copyright © Queen’s University, 2012

Organization of information Version control src data doc (tools) (releases) File server 2012-05-05_ExperimentName 2012-10-29_ExperimentName Local computer tools compiled code computed data … Temporary cache for quick access Changing Valuable Laboratory for Percutaneous Surgery (The Perk Lab) – Copyright © Queen’s University, 2012

Organization of information Source code Tests Algo-rithms GUI, etc. I/O 2. Algorithms, tests, input/output, and others in separate files Laboratory for Percutaneous Surgery (The Perk Lab) – Copyright © Queen’s University, 2012

Separation of algo, test, I/O, other clear all; … file_name='lena.bmp'; cover_image=double(imread(file_name)); Mc=size(cover_image,1); Nc=size(cover_image,2); file_name='copyright.bmp'; watermark=double(imread(file_name)); Mm=size(watermark,1); Nm=size(watermark,2); watermark_vector=fix(reshape(watermark,Mm*Nm,1)./2); rand('state',16); [cA1,cH1,cV1,cD1] = dwt2(cover_image,'haar'); [cA2,cH2,cV2,cD2] = dwt2(cA1,'haar'); [cA3,cH3,cV3,cD3] = dwt2(cA2,'haar'); .. cA2 = idwt2(cA3,cH3,cV3,cD3,'haar',[Mc/4,Nc/4]); cA1 = idwt2(cA2,cH2,cV2,cD2,'haar',[Mc/2,Nc/2]); watermarked_image = idwt2(cA1,cH1,cV1,cD1,'haar',[Mc,Nc]); watermarked_image_uint8=uint8(watermarked_image); imwrite(watermarked_image_uint8,'watermarked_lena.bmp','bmp'); psnr=psnr(cover_image,watermarked_image_uint8,Mc,Nc), figure(1) imshow(watermarked_image_uint8,[]) title('Watermarked Image') Tests Algo-rithms GUI, etc. I/O Image source: Code Complete (Steve McConnell) Laboratory for Percutaneous Surgery (The Perk Lab) – Copyright © Queen’s University, 2012

Separation of algo, test, I/O, other function test_result=watermark_test_01 background_file_name='lena.bmp'; watermark_file_name='copyright.bmp'; output_file_name='watermarked_lena.bmp‘; Reference_output_file_name = 'watermarked_lena_baseline.bmp‘; background_image = double(imread(background_file_name)); watermark_image = double(imread(watermark_file_name)); output_image=apply_watermark (background_image, watermark_image); … test_result=compare_results(output_image, reference_output_image) if (interactive) display_watermarking_results(background_image, watermark_image, output_image, test_result) end Tests Algo-rithms I/O GUI, etc. function output_image=apply_watermark(background_image, watermark_image) … Mc=size(background_image,1); Nc=size(background_image,2); Mm=size(watermark_image,1); Nm=size(watermark,_image2); watermark_vector=fix(reshape(watermark,Mm*Nm,1)./2); rand('state',16); [cA1,cH1,cV1,cD1] = dwt2(cover_image,'haar'); [cA2,cH2,cV2,cD2] = dwt2(cA1,'haar'); [cA3,cH3,cV3,cD3] = dwt2(cA2,'haar'); .. function display_watermarking_results(background_image, watermark_image, output_image) figure(1) imshow(watermarked_image_uint8,[]) title('Watermarked Image') … Laboratory for Percutaneous Surgery (The Perk Lab) – Copyright © Queen’s University, 2012

Optimization priorities Important Readability Maintainability Robustness Rarely needed Tricky solution Optimization for performance (premature optimization) 3. Optimize for readability Laboratory for Percutaneous Surgery (The Perk Lab) – Copyright © Queen’s University, 2012

What do these code snippets do? for( char *p = str, *q = strchr(str, 0) - 1, t; p < q; t = *p, *p++ = *q, *q++ = t ); Source: http://stackoverflow.com char *sourcePtr = str; char *destinationPtr = str+strlen(str)-1; while (sourcePointer<destinationPtr) { swap(sourcePtr, destinationPtr); sourcePtr++; destinationPtr--; } Reverse the str string Laboratory for Percutaneous Surgery (The Perk Lab) – Copyright © Queen’s University, 2012

Naming #1 Take the time to find the right name for classes, methods, variables: focus on clarity Do not abbreviate: slows down code reading Give names that contain useful information No useful information in these: i, j, k, a, b, c, pos, tmp Magic number: unnamed numeric constant value Include the unit (e.g., NeedleToTargetDistancePixel, NeedleToTargetDistanceMm, NeedleAngleDeg) 4. Make names fit Laboratory for Percutaneous Surgery (The Perk Lab) – Copyright © Queen’s University, 2012

What’s wrong? function BSh = CalcBoneShadow(imMask, im) What is BSh? [imH, imW] = size(im); BSh = zeros(imH, imW); % Create the shadow model estimated by a gaussian Gsigma = 6; tt = 1 : imH; ShadowM = exp(-(tt-1).^2/(2*Gsigma*Gsigma)); … What is BSh? What is imH, imW? What is the unit of Gsigma? Why it is 6? What is tt? What is the unit? What is ShadowM? function bone_shadow_image = CalcBoneShadow(mask_image, input_image, intensity_halving_depth_pixel) [image_height, image_width] = size(input_image); bone_shadow_image = zeros(image_height, image_width); % Create the shadow model estimated by a gaussian gaussian_sigma_pixel = …some…computation…; % Computed from intensity_halving_depth_pixel depth_pixel = 1 : image_height; ShadowModel = exp(-(depth_pixel-1).^2/(2*gaussian_sigma_pixel*gaussian_sigma_pixel)); … Laboratory for Percutaneous Surgery (The Perk Lab) – Copyright © Queen’s University, 2012

What’s wrong? Magic number! if( 1 == mMethodToIncorporateBeamWidth || 3 == mMethodToIncorporateBeamWidth ) { // Option: BWVar or BWTHEVar Weight4ThisAxialDepth = 1/( USBeamWidthEuclideanMagAtThisAxialDepthInMM/4 * 0.001 ); } else // Option = BWRatio Weight4ThisAxialDepth = sqrt( 1/mUS3DBeamwidthAndWeightFactorsInUSImageFrameTable5xM.get(4,0) ); enum BeamWidthComputationMethodType {BEAMWIDTH_EUCLIDEAN_NORMALIZED, BEAMWIDTH_EUCLIDEAN, BEAMWIDTH_RATIO); // …documentation of the options… … switch (mMethodToIncorporateBeamWidth ) { case BEAMWIDTH_EUCLIDEAN_NORMALIZED: case BEAMWIDTH_EUCLIDEAN: Weight4ThisAxialDepthM = 1/( USBeamWidthEuclideanMagAtThisAxialDepthInMM/4 * 0.001 ); break; case BEAMWIDTH_RATIO Weight4ThisAxialDepthM= sqrt(1/mUS3DBeamwidthAndWeightFactorsInUSImageFrameTable5xM.get(4,0)); break: default: LOG_ERROR(“Unknown beamwidth method: “<< mMethodToIncorporateBeamWidth ); return FAIL; } Laboratory for Percutaneous Surgery (The Perk Lab) – Copyright © Queen’s University, 2012

Naming #2 Distinguish global, local, member variables by the name e.g., THIS_IS_A_CONSTANT, m_MemberVariable, localVariable Naming conventions: Class name, variables name: noun (e.g., DataCollector, InputImage) Method/function name: start with a verb (e.g., Get…/Set…, Add…/Remove…, Display…, Compute…) Cannot find a good name? => underlying conceptual problem LoadAndDisplayInputData => need to split into two methods …Manager, …Controller => do you have any idea what this class should do? 4. Make names fit Laboratory for Percutaneous Surgery (The Perk Lab) – Copyright © Queen’s University, 2012

Naming #3 - spatial information PerkLab conventions (no generally applied rules) Point: 4-element vector, last element is 1 Name: somePoint_CoordinateSystemName Vector: 4-element vector, last element is 0 Name: someVector_CoordinateSystemName Transform: 4x4 element matrix, last row is [0 0 0 1] somePoint_Frame2 = Frame1ToFrame2Transform * somePoint_Frame1 Name: FromCoordinateSystemNameToToCoordinateSystemNameTransform 4. Make names fit Laboratory for Percutaneous Surgery (The Perk Lab) – Copyright © Queen’s University, 2012

Which 4 names are wrong? ProbeCalibrationMatrix ScanlineEndPoint_Image ProbeTransform ImagePlaneNormalVector_Tracker ImageToProbeTransform NeedleTipVector NeedleTrackerMatrix StylusTipToTrackerTransform Laboratory for Percutaneous Surgery (The Perk Lab) – Copyright © Queen’s University, 2012

Comments Try to write self-documenting code; use comments to describe purpose (the why, not the what or how) provide interface documentation (Matlab: following standard templates, C++: Doxygen, etc.) Need for too much commenting may indicate problems Don’t maintain the change log in comments: the version control system does a better job Remove dead (commented out) code: it can be retrieved from the version control system if needed Use standard TODO comment tags: // TODO: double-check this 5. Comment judiciously Laboratory for Percutaneous Surgery (The Perk Lab) – Copyright © Queen’s University, 2012

Comments ToDo list automatically generated from comments in VS View / Other Windows / Task List, select “Comments” Laboratory for Percutaneous Surgery (The Perk Lab) – Copyright © Queen’s University, 2012

What’s wrong? if ( theta < M_PI/4 || theta > 3*M_PI/4 ) { for ( uint y = 0; y < rows; y++ ) { // name removed - retouched for ANSI-C++ //float x = roundf(( p - y * sin(theta) ) / cos(theta)); float x = floor( ( p - y * sin(theta) ) / cos(theta) + 0.5 ); uint r = rows - y - 1; uint c = (uint)x; if ( c >= 0 && c < cols ) image[r*cols+c] = UCHAR_MAX; } Laboratory for Percutaneous Surgery (The Perk Lab) – Copyright © Queen’s University, 2012

What’s wrong? // Important FrameBufferMutex rules: // // The frame grabs are generally done asynchronously, and it is necessary // to ensure that when the frame buffer is valid when it is being written // to or read from // The following information can only be changed within a mutex lock, // and the lock must not be released until the frame buffer agrees with the // information. // FrameBuffer // FrameBufferTimeStamps // FrameBufferSize // FrameBufferIndex // FrameBufferExtent // FrameBufferBitsPerPixel // FrameBufferRowAlignment // After one of the above has been changed, and before the mutex is released, // the following must be called to update the frame buffer: // UpdateFrameBuffer() // Likewise, the following function must only be called from within a … Laboratory for Percutaneous Surgery (The Perk Lab) – Copyright © Queen’s University, 2012

6. Maintain high cohesion Keep file size between approximately 2KB-30KB Keep methods/functions short (preferably within 1-2 screens) Keep blocks within 1 screen (in C++: don’t put closing braces more than one screen away from the matching opening brace) Declare your variables as close as possible to the place where you will use them 6. Maintain high cohesion Laboratory for Percutaneous Surgery (The Perk Lab) – Copyright © Queen’s University, 2012

What’s wrong? void vtkV4L2VideoSource::UpdateFrameBuffer() { int i, oldExt; int ext[3]; vtkDataArray *buffer; // clip the ClipRegion with the FrameSize for (i = 0; i < 3; i++) oldExt = this->FrameBufferExtent[2*i+1] - …; this->FrameBufferExtent[2*i] = … ext[i] = this->FrameBufferExtent[2*i+1] - …; if (oldExt > ext[i]) { // dimensions of framebuffer changed this->OutputNeedsInitialization = 1; } } … int totalSize = bytesPerRow * ext[1] * ext[2]; i = this->FrameBufferSize; while (--i >= 0) buffer = reinterpret_cast<vtkDataArray *>(this->FrameBuffer[i]); if (buffer->GetNumberOfTuples() != totalSize …) buffer->Delete(); buffer = vtkUnsignedCharArray::New(); this->FrameBuffer[i] = buffer; … void vtkV4L2VideoSource::UpdateFrameBuffer() { // clip the ClipRegion with the FrameSize int ext[3]={0,0,0}; for (int i = 0; i < 3; i++) int oldExt = this->FrameBufferExtent[2*i+1] - …; this->FrameBufferExtent[2*i] = … ext[i] = this->FrameBufferExtent[2*i+1] - …; if (oldExt > ext[i]) { // dimensions of framebuffer changed this->OutputNeedsInitialization = 1; } } … int totalSize = bytesPerRow * ext[1] * ext[2]; for (int frameIndex = this->FrameBufferSize-1; frameIndex>=0; --frameIndex) vtkDataArray *buffer = reinterpret_cast<vtkDataArray *> (this->FrameBuffer[frameIndex]); if (buffer->GetNumberOfTuples() != totalSize …) buffer->Delete(); this->FrameBuffer[frameIndex] = vtkUnsignedCharArray::New(); … Laboratory for Percutaneous Surgery (The Perk Lab) – Copyright © Queen’s University, 2012

Summary Data, documents, and source code in separate folders, backed up Algorithms, tests, input/output, and others in separate files Optimize for readability Make names fit Comment judiciously Maintain high cohesion http://www.possibility.com/Cpp/CppCodingStandard.html http://www.joelonsoftware.com/ http://www.codinghorror.com/blog/ Book: Effective C++ Book: Code Complete 2 Laboratory for Percutaneous Surgery (The Perk Lab) – Copyright © Queen’s University, 2012