Sei sulla pagina 1di 21

how to write clean code

Primary Author: Matthew Ramina


Contributors: Aneesh Agrawal, Zak Dasaro, Anjali
Nambrath, Emily Liu, Alex Yao, Mr. Hanas

Agenda

Edge cases
other minor stuff
Pragmatic code
Moar practice!

Edge Cases
ACSL wont give you every possible type of
case :(
However, the input will most likely be
well-defined.
The less-frequent types of cases are called
edge cases because they lie on the edge
of the domain of input.

Edge Cases

But they arent exhibited in


the sample output!

Edge Cases

Other stuff...
You should accept input exactly as written.
This includes spaces in the input.
You should just be able to copy and paste from the
document.
We were more lenient this time, but will not be in
future competitions
The easiest way to accomplish this is by using
.replace( , ) before the parsing
.split(,)

Other stuff...
Python 2 vs 3
Py 2

Py 3

raw_input()

input()

print hello!

print(hello!)

CSE, Codecademy

(what you really should use if youre new)

Other stuff...
Printing info at the beginning of the
program: better left for comments at the
top
Should take input five times and output five
times before terminating
If this was a problem with your submission, you
already know
In which order to take input and output is up to

Pragmatic Code
Pragmatic: dealing with things sensibly and
realistically in a way that is based on
practical rather than theoretical
considerations

Pragmatic Code
Write a program to find the distance between
two cities:
First, do some examples in your head. How do
you solve this as a human?

Pragmatic Code
Do you enumerate every possible distance?
Nope!
Tedious/time-consuming
What if you need to add a
city?
What if you make a typo?

Pragmatic Code
Do you find the distance between the two points?
Better idea!
However, this approach of a long if
statement is still cumbersome:
What if you need another city?
What if the distance between two
cities is changed?

Pragmatic Code
First, we should decrease the potential for typos and
tedious changing:
We used the same numbers as the prompt, and can easily
add another city if needed.
This means that we need to find the sum of the
intermediate distances rather than the difference
between two points.

Pragmatic Code
Then, we need a better way of mapping a letter to a
position in the list:
While this is a simple function, youll need to do it twice.
Functions are your friend to map out the steps in the
algorithm and allow you to reuse code.

Pragmatic Code
Finally, get the sum of the distance:

Pythons list slicing and built-in functions like sum, max,


and abs are very useful!

Pragmatic Code
Another method using a dictionary

Pragmatic Code
For the road and vehicle type part of ACSL #1,
dictionaries could also be used.

Pragmatic Code
This was just one example, but generally
iterative solutions, ones that involve lists and
loops, are much easier to code without error
than massive if structures.

But why??
You might be thinking But my solution
works!
Sure, and for competitions like ACSL thats ok
However, in the real world, programming is all about:
Reusability
Modularity
Efficiency (as in human debugging/readability)

Other Practice
https://www.codecademy.com/ - basically
learn to code
/r/dailyprogrammer - includes voted solutions
Project Euler - more math-based problems
Google Code Jam - more complex algorithmic
problems aimed at professionals
CheckIO - specifically Python
Sphere Online Judge

Practice
Heres some (optional but encouraged)
homework: rewrite and optimize your ACSL #1
solution to avoid large if-structures. Email me
(alyao@ctemc.org) for questions or help!

Potrebbero piacerti anche