CS 370 The Pseudocode Programming Process D R. M I C H A E L J. R E A L E F A L L

Similar documents
CS1102: Macros and Recursion

Abstract. Chapter 6 Writing a Program. Overview. Writing a program: Strategy. Building a program. Bjarne Stroustrup

These are notes for the third lecture; if statements and loops.

Lecture 5: Writing a program

CS 241 Honors Memory

Chapter01.fm Page 1 Monday, August 23, :52 PM. Part I of Change. The Mechanics. of Change

Intro. Scheme Basics. scm> 5 5. scm>

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

Foundations, Reasoning About Algorithms, and Design By Contract CMPSC 122

Chapter 6 Writing a Program

Software Engineering /48

Chapter 1 Getting Started

Burning CDs in Windows XP

System Programming And C Language

What the CPU Sees Basic Flow Control Conditional Flow Control Structured Flow Control Functions and Scope. C Flow Control.

PROBLEM SOLVING 11. July 24, 2012

The compiler is spewing error messages.

Exception Namespaces C Interoperability Templates. More C++ David Chisnall. March 17, 2011

Introduction to Linked Lists. Introduction to Recursion Search Algorithms CS 311 Data Structures and Algorithms

Chamberlin and Boyce - SEQUEL: A Structured English Query Language

This wouldn t work without the previous declaration of X. This wouldn t work without the previous declaration of y

Software Implementation (Writing Quality Code)

Client Code - the code that uses the classes under discussion. Coupling - code in one module depends on code in another module

How To Get Your Word Document. Ready For Your Editor

Week 5, continued. This is CS50. Harvard University. Fall Cheng Gong

Compilers and computer architecture From strings to ASTs (2): context free grammars

Pointers. A pointer is simply a reference to a variable/object. Compilers automatically generate code to store/retrieve variables from memory

Classes, interfaces, & documentation. Review of basic building blocks

CS 370 Statements D R. M I C H A E L J. R E A L E F A L L

CSC C69: OPERATING SYSTEMS

CS1102: What is a Programming Language?

COSC 2P95. Procedural Abstraction. Week 3. Brock University. Brock University (Week 3) Procedural Abstraction 1 / 26

Text Input and Conditionals

Lecture 10: building large projects, beginning C++, C++ and structs

Lecture Notes on Memory Layout

Compiling with Multiple Files The Importance of Debugging CS 16: Solving Problems with Computers I Lecture #7

CS3205: Task Analysis and Techniques

Introduction to Programming

Repetition Through Recursion

CSE332: Data Abstractions Lecture 22: Shared-Memory Concurrency and Mutual Exclusion. Tyler Robison Summer 2010

Singly linked lists in C.

Introduction to C: Pointers

CS 31: Intro to Systems Arrays, Structs, Strings, and Pointers. Kevin Webb Swarthmore College March 1, 2016

COSC345 Software Engineering. Documentation

Software Construction #1: Creating High-Quality Code

Solving Problems Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3

Some Patterns for CS1 Students

Alan J. Perlis - Epigrams on Programming

How to approach a computational problem

6. Pointers, Structs, and Arrays. 1. Juli 2011

Test-Driven Development (TDD)

Computing and compilers

6. Pointers, Structs, and Arrays. March 14 & 15, 2011

Code Generation. Lecture 12

CS 111. Operating Systems Peter Reiher

Expanding Our Horizons. CSCI 4448/5448: Object-Oriented Analysis & Design Lecture 9 09/25/2011

COSC 2P91. Bringing it all together... Week 4b. Brock University. Brock University (Week 4b) Bringing it all together... 1 / 22

Program Syntax; Operational Semantics

Intermediate Programming, Spring 2017*

CS 520 Theory and Practice of Software Engineering Fall 2018

Lecture 13: more class, C++ memory management

CS3205 HCI IN SOFTWARE DEVELOPMENT INTRODUCTION TO PROTOTYPING. Tom Horton. * Material from: Floryan (UVa) Klemmer (UCSD, was at Stanford)

Lecture 11 Unbounded Arrays

CSE 374 Programming Concepts & Tools

Mastering Data Abstraction or Get nit-picky on design advantages

Lecture Topics. Administrivia

Thread Safety. Review. Today o Confinement o Threadsafe datatypes Required reading. Concurrency Wrapper Collections

Customizing DAZ Studio

Short Notes of CS201

Type Checking in COOL (II) Lecture 10

CS450: Structure of Higher Level Languages Spring 2018 Assignment 7 Due: Wednesday, April 18, 2018

Errors. And How to Handle Them

CS201 - Introduction to Programming Glossary By

QUIZ. What is wrong with this code that uses default arguments?

CS 390 Chapter 2 Homework Solutions

Unit Testing as Hypothesis Testing

Lecture 10 Notes Linked Lists

First-Order Translation Checklist

CS1 Lecture 5 Jan. 26, 2018

CITS5501 Software Testing and Quality Assurance Formal methods

CSE 12 Abstract Syntax Trees

C++ for Java Programmers

HW 3: Malloc CS 162. Due: Monday, March 28, 2016

ChipRider AQA GCSE Computer Science Mobile Assignment

G Programming Languages - Fall 2012

CS 111. Operating Systems Peter Reiher

Reengineering II. Transforming the System

Software Design and Analysis for Engineers

Lecture Notes on Queues

CS1 Lecture 22 Mar. 6, 2019

Documentation Nick Parlante, 1996.Free for non-commerical use.

1: Introduction to Object (1)

Lecture 10 Notes Linked Lists

CS 292 Software Development

Module 10A Lecture - 20 What is a function? Why use functions Example: power (base, n)

CS201 Some Important Definitions

CS 161 Computer Security

Analyzing Systems. Steven M. Bellovin November 26,

Guidelines for Writing C Code

assembler Machine Code Object Files linker Executable File

Transcription:

CS 370 The Pseudocode Programming Process D R. M I C H A E L J. R E A L E F A L L 2 0 1 5

Introduction At this point, you are ready to beginning programming at a lower level How do you actually write your classes and functions? One approach: Pseudocode Programming Process (PPP)

Pseudocode Pseudocode Informal, English-like notation for describing how an algorithm/routine/class/program will work Streamlines creation of code in routines In the following slides, we will cover: How to write good pseudocode How to construct routines using pseudocode Other alternatives to pseudocode

Good Pseudocode

What Makes Pseudocode Good? Any arbitrary English description won t be as useful Guidelines: Use English-like statements to precisely describe specific operations E.g., Do stuff is not very instructive Avoid syntactic elements from target programming language Pseudocode should be higher level than actual code E.g., allocate using malloc is specific to C Write at the level of intent describe MEANING of approach rather than HOW it will be implemented E.g., Say Keep track of current number of resources rather than increment resource number by 1 Write at low enough level that generating code will be nearly automatic Pseudocode = balance between being: Too high level not instructive Too low level no more instructive than the code itself Related question to ask: Will this pseudocode result in good/useful comments?

Example of Bad Pseudocode Increment resource number by 1 Allocate a dlg struct using malloc If malloc() returns NULL then return 1 Invoke Osrsrc_init to initialize a resource for the OS *hrsrcptr = resource number Return 0 Problems: Poorly written Includes target coding language details (C-specific pointer notation and malloc()) Focuses on how code will be written rather than meaning Coding details whether function returns 1 or 0

Example of Good Pseudocode Keep track of current number of resources in use If another resource is available Allocate a dialog box structure If a dialog box structure could be allocated Note that one more resource is in use Initialize the resource Store the resource number at the location provided by the caller Endif Endif Return true if a new resource was created; else return false Better because: Written entirely in English (no language-specific syntax, so could be implemented in any language) Written at level of intent meaning easier to determine Precise and detailed enough to implement in code Will produce good comments

Advantages of Good Pseudocode Makes reviews easier Can review low-level design without having to look at the actual code Supports the idea of iterative refinement High-level design refine to pseudocode refine to source code Catch errors earlier in pipeline Makes changes easier Can change/refine/modify pseudocode a lot easier than actual code Again, allows catching errors in design earlier Minimizes commenting effort Pseudocode becomes the comments! Pseudocode is easier to maintain than other forms of design docs Right in line with code more likely to be accurate (and easier to keep up to date)

Constructing Routines by Using the PPP

Overview There are five stages to constructing a routine: Design the routine Code the routine Check code Clean up loose ends Repeat as needed

Design the Routine Check the prerequisities Is this routine part of the design? Do the requirements call for (at least indirectly) this routine? Define the problem the routine will solve In particular, define: Information/process the routine will hide Inputs Outputs Preconditions Postconditions Name the routine Name should be clear and unambiguous If you can t come up with such a name, maybe purpose of routine isn t clear

Design the Routine Decide how to test the routine Useful for unit testing (we ll talk about this later) Think about error handling What can go wrong? What values could be invalid, and what will happen?

Design the Routine Think about efficiency Either: Efficiency not critical keep good abstraction for interface and code readable Can always update/optimize later Efficiency IS critical tune to meet your resource budgets Usually, though, don t go nuts with optimization until later (when you can assess where the real bottlenecks are)

Design the Routine Research functionality available in standard libraries Make sure you re not driving yourself nuts reinventing the wheel May be a buy vs. build decision Research algorithms and data types If you can t find a library to do the job for you, check to see if there is an existing algorithm you can use http://3.bp.blogspot.com/-twafphjbwfa/ug-jsyxy5yi/aaaaaaaazh0/oqjqlyxha_o/s1600/re-invent-the-wheel-small.png http://www.amazon.com/introduction-algorithms-edition-thomas-cormen/dp/0262033844

Design the Routine Write the pseudocode Just write it in your code editor Start general work towards more specific Write header comment that briefly and succinctly describes routine Fill in high-level pseudocode Recursively write pseudocode until you are at a low enough level

Design the Routine Think about the data Are you doing a lot of data manipulation? What data types do you need/want to make this process work well?

Design the Routine Check the pseudocode REVIEW your pseudocode Think about how you would explain it to someone else (or actually try to explain it to someone else) Make sure you understand it! If you don t, no one else will. Try a few ideas and keep the best (iterate) Once you start coding emotionally invested harder to throw away bad design Decompose until you can see exactly how to code it

Code the Routine Write the routine declaration Turn the header description into a comment Turn the pseudocode into comments At this point, turning this into code should be feel natural If it doesn t, keep working on your pseudocode Fill in code beneath pseudocode comments Writing metaphor actually works here: Comments Outline Code Text Each comment block of code (complete thought expressed by comment) Usually 2-10 lines of code per comment

Code the Routine Too much code per comment? If you find that a given comment produces a lot of code, you have two options: 1) Move code into its own routine (reapply PPP) 2) Apply PPP recursively keep breaking it down

Check the Code Why check it now? More expensive to fix later at testing Design might have problem due to low-level implementation issue Code could have a good-old-fashioned typing error

Check the Code Mentally check routine for errors Walk through process run it in your head Another reason to keep routines small Make sure you understand why it works! Less than 5% of errors hardware, compiler, or OS errors Suspect your own work first if something s wrong

Check the Code Compile the routine Debatable: Pros of compiling early: clear out syntax errors Cons of compiling early: Just One More Compile syndrome start rushing and hacking stuff together just to get rid of syntax errors Guidelines for compiling: Use pickiest compiler warning level Use validators (e.g., lint) ASIDE: Android Studio automatically runs lint Eliminate causes of all error messages and warnings Warnings usually a sign of low-quality code If you ignore them, important warnings might get buried in there and ignored as well!

Check the Code Step through the code in the debugger Test the code Run test cases you ve written Remove errors from the routine If the routine is REALLY buggy, start over and rewrite it

Clean Up Loose Ends Take a look and check you have: Good function interface Central purpose Good variables Good names, proper initialization, no unused objects, etc. Good statements and logic Look for off-by-one errors, infinite loops, improper nesting, resource leaks Good layout E.g., good use of whitespace Accurate comments Remove redundant comments May have pseudocode comments that are almost exactly the same as the code

Repeat As Needed If the routine is still awful after all this start over at pseudocode stage

Alternatives to PPP

Alternatives to PPP Test-first development Test cases written before writing any code Refactoring Write code, but then iteratively improve through semanticpreserving transformations Look for patterns of bad code (or smells, as Fowler refers to them) Design by contract Define routine preconditions and postconditions Hacking? Bad idea