Download Lecture Slides - NYU Computer Science

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
+
Readability, Style, and Turtles
CSCI-UA.002
+
Style
■
How the code is formatted; how it looks visually
■
Variable naming
■
Use of idioms
Why is style important? →
+
Style
■
How the code is formatted; how it looks visually
■
Variable naming
■
Use of idioms
Why is style important? →
■
You'll probably spend more time reading code than writing it:
■
code you've written
■
example code from books, specifications, forums, etc.
other people's code (collaboration, inherited code, etc.)
If everyone follows the same style, it'll be easier to read and
edit existing programs = Maintainability
■
■
+
Conventions
■
Most programming languages have established conventions
for style.
■
Usually programming teams (or companies) will also have a
set of conventions, or coding style guide.
■
The goal is readability and maintainability
■
Python has a published proposal laying out good guidelines
https://www.python.org/dev/peps/pep-0008/
+
Conventions to Follow
■
Consistency! Whatever conventions you use, keep them
consistent across the entire project, or at least within a file.
■
4 space indentation (automatic in IDLE) →
if answer == 5:
print('correct')!
■
Use blank lines to separate groups of related code to enhance
readability
x = 2
y = 5
print("x plus y is ...")
print(x + y)
+
More Conventions
■
Surround operators with whitespace in expressions
# do this:
count = count + 1
# *not* this:
count=count+1
■
only use lowercase letters in variables names
# do this:
count = 0
# *not* this:
Count=0
■
separate words in variable names using underscores
# do this:
my_count = 1
# *not* this:
mycount = 1
myCount = 1
+
Idioms
■
■
An idiom can be:
■
A saying or expression whose meaning is not obvious from
its individual parts
■
An expression that is natural to a language
Generally native speakers, or people who know the language
well, know the idioms
■
it's raining cats and dogs
■
hace mucho frío
■
les carottes sont cuites
+
Programming Idioms
Similar to Natural Languages, an idiom in a programming
language is:
■
a common task, computation, or construct that's expressed
in a way that's unique, peculiar to or encouraged by the
particular programming language in use
■
understood by all who know the language and its
conventions
■
usually are the best (most efficient) way in that language to
do that thing
■
Not necessarily formally specified and usually not the only
way to do it.
+
Turtle Graphics
■
Turtle is a Python module…
■
Allows you to draw programmatically (think of a virtual pen
plotter)
■
■
Commands are given to a movable pen
■
That pen can move and draw on a two dimensional plane
■
The pen is called a turtle!
It's essentially a Python implementation of another language
called Logo
■
created in the mid 60's
■
built on theory of constructionist learning (learn by
experimentation)
+
Turtle Graphics
■
Imagine you have a turtle
hanging out on the beach
■
imagine further that it's a
robotic turtle that you can
give commands to
■
■
move forward
turn around
■
as it moves, it leaves tracks
on the ground
■
turtle graphics simulates
this (seriously)
+
Turtle Graphics
■
We can draw by writing code
■
That code is analogous to the commands that we would give
the turtle (or pen, or pointer, or whatever)
■
■
■
move forward
turn around
but in addition, we can also
■
■
■
change colors
ask for user input
etc.