1 / 18

How to think through your program

How to think through your program. [ principles of good program design ]. Rachel Denison MATLAB for Cognitive Neuroscience ICN, 13 December 2007. 3 Principles. Clarity. How readable is your code? How easy is it to understand what it does?. Modularity.

kina
Download Presentation

How to think through your program

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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 Clarity How readable is your code? How easy is it to understand what it does? Modularity To what extent can pieces of your program function on their own? Program Flow 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. My Friends Barnaby (“Joe”) Dan Susan

  6. 3 Principles Clarity How readable is your code? How easy is it to understand what it does? Modularity To what extent can pieces of your program function on their own? Program Flow 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 • 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 • 1. Get ingredients • 2. Mix ingredients • 3. Bake cake

  8. Modularity Before starting to write your code, understand the hierarchy of tasks involved Write some pseudocode! 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 … • 1. Read in subject data • 2. Analyze subject data • 3. Plot results • 4. Write output file Analyze subject data Read in subject data Develop your program module by module.

  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 • Both … • Are M-files  filename.m • Can be called via the command line or by other scripts/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

  11. Cell Mode

  12. 3 Principles Clarity How readable is your code? How easy is it to understand what it does? Modularity To what extent can pieces of your program function on their own? Program Flow 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 • iflogical_expression • statements • end • iflogical_expression • statements • else • statements • end • iflogical_expression • statements • elseif logical expression • statements • elseiflogical_expression • statements • else • statements • end

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

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

  16. Control Structures for loops • forindex = array • statements • end • forindex = start:increment:end • statements • end Increment is optional; default increment is 1 while loops • 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! • whileexpression • 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!

More Related