1 / 49

Programming

Programming. Prof.Dr. Bruce W. Watson Software Construction Research Group b.w.watson@tue.nl www.win.tue.nl/~watson/TM/. Presentation style. Language: English, Nederlands, Afrikaans. Class interaction! Presentations (by you) on the board. Breaks of 15 minutes. Introduction. Who am I?

ellery
Download Presentation

Programming

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. Programming Prof.Dr. Bruce W. Watson Software Construction Research Group b.w.watson@tue.nl www.win.tue.nl/~watson/TM/

  2. Presentation style • Language: English, Nederlands, Afrikaans. • Class interaction! • Presentations (by you) on the board. • Breaks of 15 minutes.

  3. Introduction Who am I? • Professor of Software Construction, head of the research group. • TU/e and University of Pretoria. • PhD Eindhoven, Bachelors Waterloo. • Work experience: • Watcom (Canadian company producing programming-language products).

  4. Microsoft (again, working on programming-language products). • Ribbit (startup company, produced hard-core software technologies). • Current activities: • Still very interested in languages, and ways of expressing program ideas. • Software engineering issues, and optimizing and managing software teams.

  5. Course aims (be able to…) • Recognize the essential details of different programming languages. • Critically evaluate information from software engineers/programmers working for you. • Assist in choosing a programming paradigm or style for a project.

  6. Additional material • Programming Languages: concepts & constructs by Ravi Sethi, Addison-Wesley, 1996. • The Practice of Programming by Brian Kernighan and Rob Pike, Addison-Wesley, 1999.

  7. Time-line Over 5 weeks, you get three of 2 hrs lectures/week. At least an additional 2 hrs own research/week • Procedural programming. • Functional programming. • Object-oriented style. • Scripting. • Domain-specific programming. • End assignment (opdracht).

  8. Mini-assignment (10% mark) (Use the WWW.) Choose 3 procedural languages. With all three, identify (examples!) and contrast their versions of the concepts presented today. Some languages: C, Pascal, Modula-2, Modula-3, Fortran, COBOL, Ada, … At least one full A4 page, by email.

  9. Procedural programming What is it? • Closest to the actual operation of a computer. • Sometimes called ‘imperative’ programming. • Focus on the ‘actions’ which take place. • Sequencing the actions. • Not focused on what the actions actually operate on.

  10. Control-flow Flow charts: • Nice visual way to express sequences of actions. • Can be found in many other application areas. • Express three main ideas: • Sequencing. • Choices. • Loops, repetitions, re-doing something. • (Maybe) separation of common applications.

  11. Critique (of flow charts): • Cumbersome (especially when large). • Difficult to reason about. • Difficult to understand when large (complexity and layouts). • Not good for typing-in.

  12. Text notations: • Sequencing: semicolons, new-lines, … • Choices: • If-then statements. • Else clauses. • Problems with them. • Loops: • While. • Repeat/until. • Definite iterations. • Breaks and continues. • Freak: goto.

  13. Expressions What happens in an ‘action’? • Computing an expression using constants or the current values of some variables. • Update of at least one variable with the resulting expression value. Other uses? • Decision points in choices and loops use Boolean expressions.

  14. Types Values are grouped into types: • Safety (when do assignments make sense?). • Values versus types. Variables are declared: • Memory can be allocated. • Checking (for safety) can be done. Type-checking can be static or dynamic: • Efficiency. • When are errors discovered.

  15. Basic types: • Enumerations. • Integers. • Reals. • Subranges. Conversions: • Subtyping. • Automatic conversions: coercions.

  16. Constructed types: • Why? • Large amounts of data need/have internal structure. • Arrays: • Sequence of values of one type. • Layout. • Uses. • Initialization. • Multiple dimensions.

  17. Records: • Grouping (aggregation) of values of various types. • Layout. • Uses. • Initialization. • Pointers: • Notion of indirection. • Others: sets, trees, …

  18. Procedures (and functions) • Programs usually contain parts which look similar. • Similar parts can be split out into a separate piece, called a procedure. • Procedures should at least have: • A name. • A body. • Using a procedure is conceptually the same as simple replacement.

  19. Most interesting procedures have some special variables (parameters): • A baking-oven operator needs to know: • What to bake. • The temperature. • The duration.

  20. Variables: • Global. • Local. Parameter passing mechanisms: • By value. • By reference. (How are these specified?)

  21. Related issues: • Scope of a variable. • Nested scopes. • Lifetime of a variable. • Dangling pointers. • Recursive procedures: • Which variable is accessed?

  22. Advantages: • Abstraction: the meaning of the procedure can be thought-of at a different level. • Implementation hiding: the implementation (body) can be changed (within reason). • Libraries: complete functionality pieces can be put into libraries.

  23. Pros and cons • Well understood. • Large body of programmers. • Lots of choice of languages. • Good tool support. • In some sense, corresponds to people’s ‘recipe’ thinking.

  24. Functional programming Notion of purity. • No variables which ‘hold’ values, or get assigned-to. • The value of an expression depends only on the subexpressions and the operator. • Expressions cannot have side-effects. • (There are some exceptions to this.)

  25. Expressions in ML Local declarations: • A declaration can be done as close as possible to its use. • This can be done for functions and values. e.g. let val x = 2 in x+x end or let fun f(x)=x-1 in f(8) end

  26. Types in ML • ML is very strongly typed. • Basic types: boolean, int, real, string. • Tuples (pairing, products, etc.). This is the same idea as records. • Operations on pairs: extraction/projection.

  27. More types in ML Lists (of anything): • null • hd • tl • a::x (concatenation). This is right associative. E.g. int list

  28. Still more types in ML Since functions are so important, defining their types is too: int->bool Type declarations: type intpair = int*int;

  29. Defining a function (No procedures!) In principle: fun <name> <parameters> = <body>; E.g. fun plusone n = n + 1; (Parentheses around the ‘n’ also possible.)

  30. More functions in ML Try the following examples (some recursive): • Absolute value. • Length of a list. • Factorial. • Even.

  31. Type inferencing • ML is (usually) smart enough to figure out the types of things. (Type inferencing.) • Polymorphism is also possible. • What is the type of hd? • How about fun I(x)=x;? • Sometimes overloading confuses it: fun add(x,y) = x+y;

  32. Function patterns fun first(x, _) = x; or fun len([]) = 0 | len(a::y) = 1 + len(y); Cover all possibilities: fun head(a::y) = a; hint: what happens with head([])?

  33. First class functions • Functions can be passed as arguments too. • How would you apply some operation to every element of a list? E.g. fun map(f, []) = [] | map(f, (a::y)) = f(a)::map(f, y);

  34. Using first class functions… Doing absolute value on a list. map(abs,l) Or, if you don’t have absolute value yet: let abs x=if x<0 then –x else x end in map(abs, l) end

  35. Longer example of functions(mini-opdracht) Consider how to make a list of prime numbers • Make a list of numbers up to n. • Remove the ones which are not prime. • Use some kind of filtering (sieve). • Walk the list, sieving out the non-prime ones.

  36. Pros and cons • Very pure; easy to debug. • Relatively few programmers available. • Lots of programming languages. • Tool support is quite poor. • Corresponds closely to mathematical notions, but sometimes not to intuition.

  37. Object-orientation Is this really a new paradigm in programming? It can be built on top of: • Procedural (imperative) programming, or • Functional programming, or • Scripting, …

  38. Driving factors • Software is very expensive to build. • You would like to be able to reuse parts of your programs. • The operations (procedures/functions, etc) are usually very closely wrapped-up with the data (and the way the data is structured).

  39. Modules • These are a powerful way to separate the operations from the data. • More importantly: separate behaviour from implementation. • Examples: • Representations of patients, … • Methods for sorting records, adding more information to records, …

  40. Modules… Information hiding: • From a piece of software, only expose the minimum required (the interface) by other pieces of software. • Keep the details of how the implementation works hidden.

  41. Example: Object Pascal (Delphi) unit card; interface type suits = (Heart, Club, Diamond, Spade); colours = (Red, Black); … implementation … end.

  42. Correctness of modules If you have objects everywhere in a program, what guarantees that they’re arranged correctly? • (Imagine a complex relationship between objects.) • Structural invariants are used to express when something is correct. • They can be checked, and an error given if bad.

  43. C++ modules (classes) • Class header. • Public members. • Constructors and destructors. • Member functions (aka methods). • Private members.

  44. What more to object-oriented? Consider a set of geometric shapes. If you have a collection of shapes, how would you draw them? Typically, you need case statements in some big procedure.

  45. You can introduce subtyping instead. • Make one type for each geometric shape. • Arrange for their subtyping. • How can we arrange the procedure now? • Place the relevant program-parts close to where they are used.

  46. Subtyping in C++ • Subtyping is usually known as inheritance. • A class B can inherit from class A: • B gets all the same data-fields as A. • B perhaps adds more data-fields. • B gets all the same operations. • B can add its own versions of some operations. • The correct version of an operation is selected based on the type of the object. This is known as virtuality.

  47. Pros and cons • Thinking corresponds very closely to what you do in the real-world (you can model real things easily). • Can be done with underlying procedural or functional programming. • Good support of tools. • Many programmers have a good grasp of it.

  48. Final assignment • Write an essay of 5 to 10 pages, in 10pt type, single-spaced, with reasonable margins. • Dutch or English. • Write an essay contrasting the three main programming paradigms and domain-specific languages, including: advantages of one over another, advantages in practice, …

  49. Final assignment… • Make sure that you do not simply repeat aspects presented in class; there is more than enough information on the web for this. • Submit before 15 December, electronically to b.w.watson@tue.nl

More Related