How to think through your program [ principles of good program design ] Rachel Denison MATLAB for Cognitive Neuroscience ICN, 13 December 2007.

Slides:



Advertisements
Similar presentations
Topics Introduction Types of Errors Exceptions Exception Handling
Advertisements

Why care about debugging? How many of you have written a program that worked perfectly the first time? No one (including me!) writes a program that works.
Jerry Lebowitz. Topics  Provides a facility for a systematic object oriented approach to handling runtime errors ◦ Can also handle runtime errors.
Communicating in Code: Layout and Style Programming Studio Spring 2009 Note: several examples in this lecture taken from The Practice of Programming by.
Μαθαίνοντας Python [Κ4] ‘Guess the Number’
Fall 2004ENGR 111A MatLab – Palm Chapter 4, Part 2 The if and switch structure Class 10.1 Sections: 4.4 and 4.6.
Chapter 8 and 9 Review: Logical Functions and Control Structures Introduction to MATLAB 7 Engineering 161.
Lesson 4: Basic Algorithm Tools If, While Do For Loop, Else, Switch Case.
Program Design and Development
Programming in MATLAB Week 14 – 4/28/09 Kate Musgrave
Precedence Parentheses Arithemetic ^ * / + - (exception logical not ~ ) Relational > =
MATLAB FUNDAMENTALS: INPUT/OUTPUT LOGIC CONTROL STRUCTURES HP 101 – MATLAB Wednesday, 9/24/2014
EPSII 59:006 Spring Topics Using TextPad If Statements Relational Operators Nested If Statements Else and Elseif Clauses Logical Functions For Loops.
2010/11 : [1]Building Web Applications using MySQL and PHP (W1)PHP Recap.
Agenda Control Flow Statements Purpose test statement if / elif / else Statements for loops while vs. until statements case statement break vs. continue.
Iteration and Simple Menus Deterministic / Non-deterministic loops and simple menus.
Microsoft Visual Basic 2008 CHAPTER 8 Using Procedures and Exception Handling.
1 Web-Enabled Decision Support Systems Objects and Procedures Don McLaughlin IE 423 Design of Decision Support Systems (304)
Perl Tutorial Presented by Pradeepsunder. Why PERL ???  Practical extraction and report language  Similar to shell script but lot easier and more powerful.
CISC474 - JavaScript 03/02/2011. Some Background… Great JavaScript Guides: –
Slide deck by Dr. Greg Reese Miami University MATLAB An Introduction With Applications, 5 th Edition Dr. Amos Gilat The Ohio State University Chapter 6.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Matlab Programming, part 1 M-files It is generally more convenient to program in Matlab using m-files, ascii text files containing a set of Matlab commands.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
1 Session 3: Flow Control & Functions iNET Academy Open Source Web Programming.
Introduction to MATLAB Session 3 Simopekka Vänskä, THL Department of Mathematics and Statistics University of Helsinki 2011.
CMPS 1371 Introduction to Computing for Engineers CONDITIONAL STATEMENTS.
CMPS 1371 Introduction to Computing for Engineers MatLab.
Making Good Code AKA: So, You Wrote Some Code. Now What? Ray Haggerty July 23, 2015.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
7 1 User-Defined Functions CGI/Perl Programming By Diane Zak.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command History window Tabs to toggle between Current Directory &
read and learn from example loop programs develop modular program
Introduction to Matlab Module #4 Page 1 Introduction to Matlab Module #4 – Programming Topics 1.Programming Basics (fprintf, standard input) 2.Relational.
Perl Tutorial. Why PERL ??? Practical extraction and report language Similar to shell script but lot easier and more powerful Easy availablity All details.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
1. FINISHING FUNCTIONS 2. INTRODUCING PLOTTING 1.
Introduction to Matlab Part II 1Daniel Baur / Introduction to Matlab Part II Daniel Baur / Michael Sokolov ETH Zurich, Institut für Chemie- und Bioingenieurwissenschaften.
Digital Image Processing Lecture 6: Introduction to M- function Programming.
Digital Image Processing Introduction to M-function Programming.
Introduction to MATLAB Section2, statistics course Third year biomedical dept. Dina El Kholy, Ahmed Dalal.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
How Are Computers Programmed? CPS120: Introduction to Computer Science Lecture 5.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Working with Loops, Conditional Statements, and Arrays.
September 7, 2004ICP: Chapter 3: Control Structures1 Introduction to Computer Programming Chapter 3: Control Structures Michael Scherger Department of.
Controlling Program Flow with Decision Structures.
SCRIPTS AND FUNCTIONS DAVID COOPER SUMMER Extensions MATLAB has two main extension types.m for functions and scripts and.mat for variable save files.
PHP Reusing Code and Writing Functions 1. Function = a self-contained module of code that: Declares a calling interface – prototype! Performs some task.
Assigning Values 1. $ set One Two Three [Enter] $echo $1 $2 $3 [Enter] 2. $set `date` [Enter] $echo $1 $2 $3 [Enter] 3. $echo $1 $2 $3 $4 $5 $6 [Enter]
Learning Javascript From Mr Saem
Today… Modularity, or Writing Functions. Winter 2016CISC101 - Prof. McLeod1.
CHAPTER 4 CLIENT SIDE SCRIPTING PART 3 OF 3
Matlab Training Session 4: Control, Flow and Functions
Python: Control Structures
Scripts & Functions Scripts and functions are contained in .m-files
JavaScript: Control Statements.
Agenda Control Flow Statements Purpose test statement
Introduction to MATLAB
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Communicating in Code: Layout and Style
Python Primer 2: Functions and Control Flow
Structured Program
3 Control Statements:.
© 2016 Pearson Education, Ltd. All rights reserved.
Loops CGS3416 Spring 2019 Lecture 7.
CIS 136 Building Mobile Apps
Chapter 4: Writing and Designing a Complete Program
Presentation transcript:

How to think through your program [ principles of good program design ] Rachel Denison MATLAB for Cognitive Neuroscience ICN, 13 December 2007

3 Principles Modularity Clarity Program Flow To what extent can pieces of your program function on their own? How readable is your code? How easy is it to understand what it does? How sensible is the order of events that are executed in your program? How appropriate are the control structures you have chosen?

Clarity White space Consistent spacing between program sections and lines Consistent indentations Consistent spacing around operators y=x+5; vs y = x + 5; A=[a1;a2;a3]; vs A = [a1; a2; a3]; Variable and function naming Choose meaningful and memorable names Avoid giving variables and functions the same name Follow consistent naming conventions my_vector My_Array MYSTR myFunction myfunction

Clarity Comments % This comment tells what the next block of code does statement; statement; % this comment tells something about this line % --- THIS COMMENT IS A BIG SECTION HEADING --- % more statements … % % Another way to make a comment stand out % % Two percent signs tells MATLAB to use cell mode Will someone else be able to understand my code? Will I be able to understand my code a year from now?

Barnaby (“Joe”) DanSusan My Friends

3 Principles Modularity Clarity Program Flow To what extent can pieces of your program function on their own? How readable is your code? How easy is it to understand what it does? How sensible is the order of events that are executed in your program? How appropriate are the control structures you have chosen?

Modularity: Doing a big task by doing little tasks Bake a cake 1. Get ingredients 2. Mix ingredients 3. Bake cake a) Find out what the ingredients are b) Check to see what ingredients you already have c) Go to the store to buy remaining ingredients d) Assemble the ingredients in the kitchen

Modularity Before starting to write your code, understand the hierarchy of tasks involved Write some pseudocode! Analyze subject data 1. Read in subject data 2. Analyze subject data 3. Plot results 4. Write output file Develop your program module by module. Read in subject data 1. If the subject’s file exists, open it. 2. Read in the file line by line. If a line is empty, skip it. 3. Assign the first item in the line to the variable ‘subject’ etc …

Advantages of modularity Guides program development and makes it manageable Allows you to test and debug individual modules Allows you to reuse modules in other programs function

Scripts vs. Functions Scripts … Can access and operate on data in the workspace, and create new data for the workspace Do not take input arguments or return output arguments Can be any series of Matlab statements; no beginning or end delimiters Functions … Can only ‘see’ variables created inside the function, or passed to the function when it is called May take input arguments and return output arguments Must begin with the keyword function and a function definition; subfunctions should end with return Both … Are M-files  filename.m Can be called via the command line or by other scripts/functions

Cell Mode

3 Principles Modularity Clarity Program Flow To what extent can pieces of your program function on their own? How readable is your code? How easy is it to understand what it does? How sensible is the order of events that are executed in your program? How appropriate are the control structures you have chosen?

Control Structures If statements if logical_expression statements end if logical_expression statements else statements end if logical_expression statements elseif logical expression statements elseif logical_expression statements else statements end

Control Structures switch-case switch expression (scalar or string) case value1 statements case value2 statements … otherwise statements; end

switch-case vs. if-elseif if-elseif … Can be clunky, especially if there are many elseifs However, allows for more complicated logical tests switch-case … Clean and easy to read Easy to test strings – can compare strings of different lengths Both are better than: if logical_expression statements end if logical_expression statements end …

Control Structures for loops for index = start:increment:end statements end Increment is optional; default increment is 1 while loops while expression statements end Use for when you want to iterate a set number of times, or over set values. Use while when you want to exit the loop only when a certain condition is met. Beware of infinite while loops! for index = array statements end continue passes control to the next iteration of the for or while loop break terminates the for or while loop and jumps to the next statement return immediately exits the function in which it appears

Control Structures try-catch try statements catch statements to handle error end Try to avoid having any errors Use try-catch to handle errors that you can only partially anticipate, or to decide how to handle an error using the Matlab error-message output

Happy Holidays!