Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "How to think through your program [ principles of good program design ] Rachel Denison MATLAB for Cognitive Neuroscience ICN, 13 December 2007."— Presentation transcript:

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

2 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?

3 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

4 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?

5 Barnaby (“Joe”) DanSusan My Friends

6 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?

7 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

8 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 …

9 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

10 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

11 Cell Mode

12 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?

13 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

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

15 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 …

16 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

17 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

18 Happy Holidays!


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

Similar presentations


Ads by Google