Coding Design, Style, Documentation and Optimization

Slides:



Advertisements
Similar presentations
A8 – Control Structures if, if-else, switch Control of flow in Java Any sort of complex program must have some ability to control flow.
Advertisements

CSCI 330: Programming Language Concepts Instructor: Pranava K. Jha Control Flow-II: Execution Order.
Chapter 1: Computer Systems
Coding Standards for Java An Introduction. Why Coding Standards are Important? Coding Standards lead to greater consistency within your code and the code.
Introduction to C Programming
Introduction to Optimizations
Program Design and Development
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 34: Code Optimization COMP 144 Programming Language Concepts Spring 2002 Felix.
Introduction to C Programming
1 Lecture 5  More flow control structures  for  do  continue  break  switch  Structured programming  Common programming errors and tips  Readings:
Agile Lab 2008a Armin B. Cremers, Günter Kniesel, Pascal Bihler, Tobias Rho, Marc Schmatz, Daniel Speicher Sommersemester 2008 R O O T S Coding standards.
Simple Program Design Third Edition A Step-by-Step Approach
Programming Style and Documentation Objective(s) F To become familiar with Java Style and Documentation Guidelines.
Programing Concept Ken Youssefi/Ping HsuIntroduction to Engineering – E10 1 ENGR 10 Introduction to Engineering (Part A)
CSC204 – Programming I Lecture 4 August 28, 2002.
School of Computer Science & Information Technology G6DICP - Lecture 9 Software Development Techniques.
1 ENERGY 211 / CME 211 Lecture 26 November 19, 2008.
Chapter 8 High-Level Programming Languages. 8-2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
Java Coding Standards and Best Practices Coding Standards Introduction: After completing this chapter, you will able to keep your code up to standards.
Chapter 2 Pseudocode. Objectives To introduce common words, keywords and meaningful names when writing pseudocode To define the three basic control structures.
Pseudocode. Simple Program Design, Fourth Edition Chapter 2 2 Objectives In this chapter you will be able to: Introduce common words, keywords, and meaningful.
Pseudocode Simple Program Design Third Edition A Step-by-Step Approach 2.
Lecture 2 Numerical Methods for Engineering MECN 3500 Department of Mechanical Engineering Inter American University of Puerto Rico Bayamon Campus Dr.
CONTENTS Processing structures and commands Control structures – Sequence Sequence – Selection Selection – Iteration Iteration Naming conventions – File.
Code Conventions Tonga Institute of Higher Education.
Writing Maintainable code Dr. Susan McKeever DT228/3 GUI Programming.
Coding Conventions  Coding conventions are a set of guidelines for a specific software project that recommend programming style, practices and methods.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
04/02/ Procedures Top-down approach / Stepwise Refinement & Sub Procedures.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 4: Introduction to C: Control Flow.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
CSCI 161 Lecture 3 Martin van Bommel. Operating System Program that acts as interface to other software and the underlying hardware Operating System Utilities.
Evolution of C and C++ n C was developed by Dennis Ritchie at Bell Labs (early 1970s) as a systems programming language n C later evolved into a general-purpose.
The Essentials of a Java Program JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin,
1 Problem Solving  The purpose of writing a program is to solve a problem  The general steps in problem solving are: Understand the problem Dissect the.
Conditionals, Block Statements, Style
Topic: Python’s building blocks -> Variables, Values, and Types
Working with Java.
Principle of Programming Lanugages 2: Imperative languages
ICS103 Programming in C Lecture 4: Data Types, Operators & Expressions
Programming Problem steps must be able to be fully & unambiguously described Problem types; Can be clearly described Cannot be clearly described (e.g.
Statement atoms The 'atomic' components of a statement are: delimiters (indents, semicolons, etc.); keywords (built into the language); identifiers (names.
Programming Fundamentals
2008/09/24: Lecture 6b CMSC 104, Section 0101 John Y. Park
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 2 Applications and Data.
Designing and Debugging Batch and Interactive COBOL Programs
Chapter 10 Programming Fundamentals with JavaScript
Software Quality Engineering
Data Types, Identifiers, and Expressions
Variables ICS2O.
Chapter 1: Computer Systems
Maintaining a Program In today’s lesson we will look at:
Building Java Programs
Anatomy of a Java Program
Beginning Style 27-Feb-19.
Coding practices For IT and Computing students 2014.
CS 144 Advanced C++ Programming January 31 Class Meeting
Unit 3: Variables in Java
Chap 2. Identifiers, Keywords, and Types
Tonga Institute of Higher Education
Java Methods: A Deeper Look Academic 2019 Class: BIT23/BCS10 Chapter 06 Abdulaziz Yasin Nageye Faculty of Computing Java How to Program, 10/e 1 © Co py.
Variables in C Topics Naming Variables Declaring Variables
CSCE-221 C++ Coding Standard/Guidelines
Variables in C Topics Naming Variables Declaring Variables
Controlling Program Flow
Chapter 4: Writing and Designing a Complete Program
Structural Program Development: If, If-Else
Presentation transcript:

Coding Design, Style, Documentation and Optimization

Coding Design Coding design refers to the low-level program design, i.e. coding. Java supports so called structured programming At the low level, a structured program is composed of simple, hierarchical program flow structures. These are sequence, selection, and repetition. No spaghetti programs, i.e. no gotos. A simple and commonly used method: Step-wise refinement.

Stepwise Refinement The oldest software designing method. It was published by Niklaus Wirth in Communications of the ACM, Vol. 14, No. 4, April 1971, pp. 221-227. A low-level designing method, i.e. a method for designing small programs. The basic idea is to repeatedly decompose pseudocode statements until each pseudocode statement can be coded in a couple of programming language statements.

Example: Write a program to print a calendar 2018   January Su Mn Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 February 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 March First refinement Read a year Print the calendar of the year Second refinement Read a year Print the calendar of the year Label the year Print months

Third Refinement Read a year Print the calendar of the year Label the year Repeat 12 times Print a month

Fourth Refinement Read a year Print the calendar of the year Label the year Repeat 12 times Print a month Label the month Label Days of the week. Print days January   Su Mn Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

Fifth Refinement Read a year Print the calendar of the year Label the year Repeat 12 times Print a month Label the month Label Days of the week. Print days Determine number of days Position the 1st day January   Su Mn Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

Sixth Refinement Read a year Print the calendar of the year Label the year Repeat 12 times Print a month Label the month Label Days of the week. Print days Determine number of days Position the 1st day Repeat (number of days) times Print a day

Why Style and Documentation are important? 80% of the lifetime cost of a piece of software goes to maintenance. Hardly any software is maintained for its whole life by the original author. Good style and documentation improve the readability of the software, allowing engineers to understand new code more quickly and thoroughly. Reference “Code Conventions for the Java Programming Language” http://www.oracle.com/technetwork/java/codeconv-138413.html

What are included in style and documentation? Style and documentation are divide into three categories: Naming Comments Format

Naming Use meaningful names. Make names long enough to be meaningful but short enough to avoid being wordy. Java naming conventions: Packages: Names should be in lowercase. Classes & interfaces: Names should be in CamelCase, i.e. each new word begins with a capital letter (e.g. CamelCase, CustomerAccount) Methods & variables: Names should be in mixed case, i.e. first letter of the name is in lowercase (e.g. hasChildren, customerFirstName). Constants: Names should be in uppercase, e.g. MAX_HEIGHT

Comments There are four styles of comments: Block comments are used to provide descriptions of files, methods, data structures and programs. Example: /* * Lab number: 1 * Your name: * Section number: 4 */ Short comments can appear on a single line indented to the level of the code that follows. if (condition) { /* Handle the condition. */ ... }

Comments conti. Trailing Comments: Very short comments can appear on the same line as the code they describe, but should be shifted far enough to separate them from the statements. if (a == 2) { return true; /* special case */ } else { return isPrime(a); /* works only for odd number a */ } End-Of-Line Comments: The // comment delimiter can comment out a complete line or only a partial line. if (foo > 1) { // Do a double-flip. ... return false; // Explain why here.

Format To make the logical organization of the code stand out. To ensure that the source code is formatted in a consistent, logical manner.

Indentation Establish a standard size for an indent, such as four spaces, and use it consistently. Align sections of code using the prescribed indentation.

Example: loops for (i = 0; i < 100; i++) { ... … } for (i = 0; i < 100; i++) { ... … }

Example: if statements if ( condition) { statements; } else { } if ( condition) { statements; } else

Example: nested statements for (i = 0; i < 100; i++) { if ( condition) statements; } else for (i = 0; i < 100; i++) { if ( condition) { statements; } else { }

Coding optimization Donald Knuth, premature optimization is the root of all evil Optimization can introduce new, subtle bugs Optimization usually makes code harder to understand and maintain Get your code right first, then, if really needed, optimize it Document optimizations carefully Keep the non-optimized version handy, or even as a comment in your code

The 80/20 rules In general, 80% percent of a program’s execution time is spent executing 20% of the code. 90%/10% for performance-hungry programs. Spend your time optimizing the important 10/20% of your program. Optimize the common case even at the cost of making the uncommon case slower.

General optimization techniques Strength reduction Use the faster and cheaper version of an operation E.g. x >> 2 instead of x / 4 x << 1 instead of x * 2 Common sub expression elimination Reuse results that are already computed and store them for use later, instead of re-computing them. double x = d * (limit / max) * sx; double y = d * (limit / max) * sy; double depth = d * (limit / max); double x = depth * sx; double y = depth * sy;

General optimization techniques conti. Code motion Invariant expressions should be executed only once E.g. for (int i = 0; i < x.length; i++) x[i] *= Math.PI * Math.cos(y); double picosy = Math.PI * Math.cos(y); x[i] *= picosy;

General optimization techniques conti. Eliminate unnecessary loads and stores x = y + 5; z = y; w = z; u = w * 3; u = z * 3; x = 5; z = y+x; z = y+5;

General optimization techniques conti. Factoring out invariants If an expression is carried out both when a condition is met and is not met, it can be written just once outside of the conditional statement. E.g. if (condition) { do A; do B; do C; } else { do D; } do A; if (condition) { do B; } else { do D; } do C;