Programming Languages and Software Engineering

advertisement
Programming Languages and
Software Engineering
Topics
• discuss some of the underlying
commonalities and differences among
programming languages
• explain what there is to learn about
software engineering beyond
programming
Learn Programming in Ten Years
(Peter Norvig)
• it takes time/practice/experience to learn
to become a good programmer
• you can learn syntax in one semester (or
from a book), but...
• need to built up a repertoire of problems
you have previously solved, so when you
face a new problem, you can draw upon
your prior experience, you’ve seen it
before – “design patterns”
• Norvig’s suggestions:
– Get interested in programming, and do some
because it is fun..
– Program. The best kind of learning is learning by
doing.
– Talk with other programmers; read other programs.
– Work on projects with other programmers.
– Work on projects after other programmers.
– Learn at least a half dozen programming
languages.
– Remember that there is a "computer" in "computer
science". Understand how it works.
– Get involved in a language standardization effort.
• In CSCE 121, we teach C++ because it is
widely used for large industrial
applications.
– Many additional computational concepts are
introduced in this course, such as how
compilers work, object-oriented design, etc.
• There are many other languages - do you
need to learn them all?
•
•
•
•
•
•
•
•
•
Assembler
Fortran, Pascal, COBOL, Basic
Ada, Modula, PL/1, Simula, MatLAB
Haskell, LISP, Scheme
C, C++, C#, Objective-C
Smalltalk, Java, Javascript
PERL, PHP, TCL, Python, Ruby
csh, awk, sed
Prolog – a logic-based language for making
inferences from knowledge bases
• Postscript – a stack-based language for printers
source: http://calvinx.com/tag/programming-languages/
Two Interesting Questions
• Why are there such a bewildering array of
languages?
– historical artifact – some languages evolved into, or
inspired, other languages
– some languages started out as experiments to test
new ideas or features, like OO and message-passing
in Smalltalk
– some languages are more suited for certain
applications, like Fortran for numerical computing, or
PERL/Python for processing text files
– there are special-purpose languages like Postscript
(or MatLAB, Simula) that are custom-designed for
certain tasks (printing, simulations...)
Two Interesting Questions
• Isn’t C++ overkill for writing low-level code like
drivers/controllers/firmware? wouldn’t C be
much more efficient with less overhead?
– this is a common misconception
– by this reasoning, why not use assembler?
– modern C++ compilers have many optimizations that
make code fast/competitive
– most importantly, C++ allows code to be expressed in
a more comprehensible (generic, factored) way that
facilitates maintainability and long-term use of code
over many years
• these issues often outweigh a few extra microseconds
• the original programmer might be long gone, and someone
else has to find and fix a bug
Programming
• The same algorithm can be implemented in
many programming languages.
in C
#include <stdio.h>
void main(int argc,char*argv)
{
int a,b,temp;
sscanf(argv[1],”%d”,&a);
sscanf(argv[2],”%d”,&b);
if (a<b)
{ temp=a; a=b; b=temp; }
while (b>0)
{
int r=a%b;
a=b; b=r;
}
printf(“GCD is %d\n”,a);
}
in Python
in Java
import sys
if __name__==“__main__”:
public class GCD {
public static void
main(String[] args) {
int a,b,temp;
a=Integer.parseInt(args[0]);
b=Integer.parseInt(args[1]);
a = int(sys.argv[1])
b = int(sys.argv[2])
if a<b:
b,a = a,b
while b>0:
r = a%b
a,b = b,r
print “GCD is”,a
if (a<b)
{ temp=a; a=b; b=temp; }
while (b>0)
{
int r=a%b;
a=b; b=r;
}
System.out.println(
"GCD is "+a); } }
• Point #1: Once you understand the general principles (which you will learn in CSCE
314), you just have to learn the “syntactic variations” in each language
• Point #2: There are some unique language differences/features including: usercreated types, polymorphism, list comprehension, exceptions, function objects...
• In CSCE 314, you will learn the structure of
languages and what unites them, so you can
eventually learn to program in any of them
• major classes of languages
– imperative/procedural languages (“block-structured”)
• Pascal, Fortran, C, Ada...
– functional languages (“expression evaluation”)
• Scheme, LISP, Haskell...
– object-oriented (encapsulation, “message passing”)
• Smalltalk, Java, Objective-C, C++
– logic-programming
• Prolog
– (of course, some languages blur these distinctions)
• I already showed examples of procedural programming
• here is the functional version of factorial and GCD
written in LISP:
– (this is an interactive command line I typed into)
>(defun fact (n) (if (<= n 1) 1 (* n (fact (- n 1)))))
FACT
>(fact 10)
3628800
>(defun gcd (x y)
(if (< x y) (gcd y x)
(if (= y 0) x (gcd y (mod x y)))))
GCD
>(gcd 112 40) // “running a program” is done by “evaluating an expression”
8
• interpreted vs. compiled languages
– interpreters – read in lines and execute directly
– compilers
• convert source code to low-level language (.exe, assembly
language, executable CPU instructions)
• have to learn about regular expressions, parsing, syntax and
semantics, optimization...
– compare Python and C
• block structure
– think how the “body” of a for loop or if statement is marked in
different languages (“}”, “endfor”, change of indentation)
• type systems
– can users define new “types” and operations on them? like
structs and classes, example: Complex #s
• object-oriented languages
– classes, inheritance, polymorphism...
• extensions for: concurrency, exception-handling, realtime applications...
• Here is an example of an object-oriented
definition of Rectangles in Java
public class Rectangle
{
int height,width; // interval variables
// initializer
public Rectangle(int a,int b)
{ height=a; width=b; }
int area() { return height*width; }
}
static Rectangle BoundingBox(
Rectangle A,Rectangle B)
{
int h=max(A.height,B.height);
int w=max(A.width,B.width);
Rectangle C=new Rectangle(h,w);
return C;
}
public static void main(String[] args) {
Rectangle P=new Rectangle(3,2);
Rectangle Q=new Rectangle(1,4);
Rectangle R=BoundingBox(p,q);
System.out.println(
“P:(3,2) area="+P.area());
System.out.println(
“Q:(1x4) area="+Q.area());
System.out.println(
“R:(3x4) area="+R.area());
}
27 sun> java local/testRectangle
p:(3,2) area=6
P:
q:(1x4) area=4
R:
r:(3x4) area=12 h=3,
w=2
h=3,
Q: h=1, w=4
w=4
Learning Unix
(part of CSCE 312/Systems)
• why learn this? is it necessary?
• many command-line development tools
– (compared to IDEs like MS Visual Studio)
• Unix has well-defined concepts and principles
– executables, flags, pipes, streams, sockets...
– multitasking
• many powerful tools for software development:
–
–
–
–
–
editors like Emacs
compilers like g++
make files (for multi-file projects)
debuggers like gdb
source-code control like CVS or SVN
Software Engineering (CSCE 431)
• software engineering usually involves working in teams
• requirements gathering/writing specifications (with customer)
• UML (Unified Modeling Language)
– diagram out how your modules work and interact
• libraries – reuse of code
– e.g. APIs for image processing, network access, speech recog...
– large projects have many dependencies
• testing – defining test cases as important as writing code
• documentation
– explain how things work, why it was designed that way,
ASSUMPTIONS, alternatives, limitations...
• version control
– issue new number with each change to file, e.g. v3.0.12
– critical so you can identify and undo mistakes
• life-cycle models
– waterfall, rapid prototyping, extreme programming (partners)...
– metrics for tracking/estimating number of bugs over time
Download