Presentation is loading. Please wait.

Presentation is loading. Please wait.

Code Clichés and Conventions Lecture 10: Supporting Material Dr Kathryn Merrick Thursday 2 nd April, 2009.

Similar presentations


Presentation on theme: "Code Clichés and Conventions Lecture 10: Supporting Material Dr Kathryn Merrick Thursday 2 nd April, 2009."— Presentation transcript:

1 Code Clichés and Conventions Lecture 10: Supporting Material Dr Kathryn Merrick Thursday 2 nd April, 2009

2

3 Overview What is a code cliché? Common code clichés: Traversal Error checking Flag guarded loops Sentinel guarded loops Enumeration and case Useful conventions

4 Code Clichés In English a cliché is a phrase that is used a lot (or even overused!) In computer programming, a code cliché is a sequence of instructions that are used together a lot to achieve some goal

5 Problem You have a freshly cooked bowl of soup in your favourite flavour… You’d like to calculate the soup’s temperature at every second over a specified period of time.

6 From Problem to Program… 1.Understand the problem ‘in English’ 2.Develop a rough solution on paper 3.Implement the solution in MATLAB 4.Test your solution

7 Newton’s Law of Cooling You know that: T t = T a + (T 0 – T a )e -kt Where: T 0 is the initial temperature of the soup T t is the temperature at time t T a is the ambient temperature of the room k = 0.054 for soup Source: http://www.ugrad.math.ubc.ca/coursedoc/math100/notes/diffeqs/cool.htmlhttp://www.ugrad.math.ubc.ca/coursedoc/math100/notes/diffeqs/cool.html

8 Traversal A cliché to process every value in a list of values General Syntax: for val = start:end code to process value end

9 Demo 1: Traversal Soup Cooling…

10 Error Checking Use an if statement to check error conditions Use a different branch for each type of error Use the built-in error function to print error message Else branch runs the program if no errors were detected

11 Demo 2: Error Checking Better soup…

12 Another Problem You have a freshly cooked bowl of soup in your favourite flavour… You’d like to calculate the soup’s temperature at every second until it reaches an edible 60 degrees.

13 Flag Guarded Loops Used to permit repeated processing of an unknown number of inputs A Boolean ‘flag’ locks execution in a loop When value of flag changes execution can exit the loop Combines while+if statements

14 Flag Guarded Loops: Syntax finished =... % initialise the flag while ~finished do something if desired event has happened finished = true; end This code cliché is useful when the loop exit condition is very complex.

15 Demo 3: A flag guarded loop Yet more soup…

16 Sentinel Guarded Loops Used to permit repeated processing of an unknown number of inputs A special value indicates end-of-data Special value should not be a feasible input for processing Frequently used with user input

17 Sentinel Guarded Loops: Syntax sentinel = special value; val = initial value while val ~= sentinel code to process value val = code to get next value end

18 Demo 4: A sentinel guarded loop Ctrl-C

19 Enumeration and Case Avoid ‘hard coding’ values into a case statement Comparing strings is a relatively slow operation Instead, enumerate your values as constants at the start of your function Put the enumerated values into the case statements

20 Enumerated Case Statement: Syntax DON’T  switch service case 1 do something case 2 do something case 3 do something end DON’T  switch service case ‘army’ do something case ‘navy’ do something case ‘air force’ do something end

21 Enumerated Case Statement: Syntax DO ARMY = 1; NAVY = 2; AIR_FORCE = 3; switch service case ARMY do something case NAVY do something case AIR_FORCE do something end

22 Other Conventions: Revision follow_a_variable_naming_convention thereAreDifferentOnesToChooseFrom USE_CONSTANTS Use meaningful variable names % Comment liberally.

23 More Conventions DON’T WRITE MESSAGES IN CAPITALS – THIS IS SHOUTING Suppress all output inside functions; Use consistent indentation Makes your code easier to read Makes it easier to see syntax errors Comments and error messages should use correct English spelling and grammar and be clear and meaningful.

24 Summary After today’s lecture you should be able to: Identify and use a number of common code clichés Adhere to coding conventions


Download ppt "Code Clichés and Conventions Lecture 10: Supporting Material Dr Kathryn Merrick Thursday 2 nd April, 2009."

Similar presentations


Ads by Google