ATS Programming Short Course I INTRODUCTORY CONCEPTS Tuesday, Feb 10th, 2009 Introduction to Programming
STYLE PHILOSOPHY The principles of programming style are based on common sense guided by experience, not on arbitrary rules and prescriptions. Code should be clear and simple – straightforward logic, natural expression, conventional language use, meaningful names, neat formatting, helpful comments – and it should avoid clever tricks and unusual constructs. Consistency is important because others will find it easier to read your code, and you theirs.
PROGRAMMING STYLE NAMES – A name should be informative, concise, memorable, and pronounceable if possible. Much information comes from context and scope; the broader the scope of a variable, the more information should be conveyed by its name.
PROGRAMMING STYLE NAMES – ◦ Descriptive names for globals, short names for locals ◦ Be consistent – related things get related names to highlight their relationship ◦ Use active names for functions ◦ Be accurate.
PROGRAMMING STYLE EXPRESSIONS AND STATEMENTS ◦ Indent to show structure ◦ Use the natural form for expression ◦ Parenthesis to resolve ambiguity ◦ Break up complex expressions
CS TAPESTRY
PROGRAMMING STYLE CONSISTENCY – ◦ Use a consistent indention and brace style
PROGRAMMING STYLE COMMENTS – ◦ Don’t belabor the obvious ◦ Comment functions and global data ◦ Don’t comment bad code, rewrite it ◦ Don’t contradict the code
P
CS TAPESTRY
PROGRAM FLOW CONTROL Branches – (if block) ◦ Used to direct program flow through different cases based on tests. ◦ if/else blocks ◦ case blocks Loops – (for loop) ◦ Used to repeat a segment of code based on: for loop – loops over a segment ‘for’ a specified number of times (counting loop). while loop – loops over a segment ‘while’ a certain condition is true.
RELATIONAL LOGIC
COMBINATIONAL LOGIC
ERRORS Syntax – semi-colon left out. Runtime – array cell access beyond limit Logical – if/else not correct
DEBUGGING Examine the most recent change Don’t make the same mistake twice Read before typing Explain your code to someone else Print statements, print statements,…
TESTING Test as you write the code Systematic testing Test automation Stress tests
PROGRAM COMPONENTS Functions Subroutines Modules Classes, Objects
TOP-DOWN DESIGN
FLOW CHART
FLOW CHART EXAMPLE
PSEUDO-CODE