Error Types and Debugging Two major types of errors: Syntax errors - grammar, spelling, wrong number of arguments Easy to find and fix - Modern editors (like MATLAB's) highlight errors and warnings (underline in red and yellow) On a program crash the Command Window provides the line number so they are easy to find Semantic errors - logical errors, errors in meaning, program runs but does not do what is intended Harder to find and fix - Editors can't identify issues because they don't know what the programmer wanted Programmer needs to know the correct behavior of each piece of code Need to know what the contents of variables should be throughout the execution Debugging - done to methodically find where errors are Debugging Techniques: incremental coding to avoid making errors to begin with and/or detect early (as soon as you make them) comment out selected portions of the code display messages / variable values while code is running by unsuppressing output / adding fprintf or disp statements find smallest example that causes the problem using a debugger A debugger is a programming tool that assists in debugging code Basic functionality: Set breakpoints (stop executing code & wait) Step through (execute) code line by line Step over (skip over executing a function), into (go into a function), out of (go out of a function) Continue to next breakpoints View contents of key variables (in Workspace) when code has stopped executing MATLAB specific: Icons at the top of editor ribbon: Breakpoints, Continue, Step, Step In, Step Out, Quit Debugging In editor window: Breakpoints on left side (right click to set/change conditions), arrow shows line about to execute. Workspace: Shows variables at current point in execution Example on laptop: Use validateID