Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Object-oriented programming opens up a ton of new ways and ideas for us to use when creating software. That means it's time for us to start thinking about the design of our code.
- Keep your code as simple as possible
- Don't repeat yourself (DRY)
- Your code should have only the bells and whistles that it needs (YAGNI)
- Write Python like Python, not like any other language
This is a great time to familiar yourself with PEP 8 and PEP 20, two documents that most Python developers use to guide what their code looks like and how they think about code while creating it.
So, before we get too far into
the mechanics of programming,
0:00
let's talk about the biggest
subject of all design.
0:04
I don't mean how your software looks
on a website or a phone, though.
0:07
I'm talking about the organization
thinking and layout of your code.
0:09
In previous courses, most of our
code has been in a single file.
0:14
This is fine for
small amounts of code, but
0:17
you'll quickly see that we don't
always have small code footprints.
0:19
Libraries and tools can quickly
reach hundreds, if not thousands,
0:23
of lines of code.
0:26
That's just not something you
want to read all in one file.
0:27
We've all been there, right,
a document that just keeps scrolling and
0:30
scrolling and scrolling.
0:32
Now, imagine you're on line
595 of some script and
0:34
see a function reference from
all the way up at line 18.
0:38
IDEs and other editors can
reduce the amount of scrolling,
0:41
but wouldn't it be better if
you didn't have to do that?
0:43
And this is the first case
that software design solves.
0:45
Well, it helps at least.
0:49
We can break our code up into modules or
files.
0:50
By putting code into logical groupings we
can make it easier to take it all in and
0:53
think about what we need
to do to do our job.
0:57
This logical grouping
goes even further though.
1:00
We already grouped related lines
of code together into functions.
1:02
Now with classes we have
another layer of grouping.
1:05
If you have multiple functions that all
work together think about putting them
1:07
into a class.
1:10
OO provides us with so many tools that
it's often hard to know which ones to use,
1:12
or, if you should even use any of them.
1:16
I don't want to just
dump a ton of terms and
1:18
features on you, so, I'll be bringing
them up as we go through the course.
1:20
I'll also be talking about how to use and
1:23
think about these features in
the design of your software.
1:25
But I can only influence your thinking so
far.
1:27
I need you to also consider how
you'd use each of these things
1:30
in the design of your own software.
1:32
An often overlooked truth of software
development is that there's rarely any
1:34
one correct way to solve a problem or
1:38
design an algorithm, so like I said
this organization starts at its
1:40
barest of bones with organizing
code into functions and classes.
1:45
Let's think back to the shopping list
app that we built in Python basics and
1:48
Python collections.
1:51
[SOUND] We had a list that held
the things we wanted to shop for,
1:52
we had functions to add things to
the list, reorganize the list,
1:55
and remove things we didn't need or
had already bought.
1:58
Well right there I see something
that makes sense for a class,
2:01
the ShoppingList itself.
2:04
I can see going a step further though and
making each thing on the list and
2:05
instance of some other class.
2:08
Maybe this class would hold the name for
2:10
the thing, how many that I wanted,
what store it was at, who knows.
2:11
There are many options for
the functionality of this class.
2:14
And that brings me to another point,
ruthless editing.
2:19
As you build classes and
methods and functionality,
2:22
keep in mind a very common acronym, YAGNI,
which stands for, you ain't gonna need it.
2:24
Keep your code lean and mean and
you'll find it easier to use and
2:29
better for building awesome things with.
2:33
It's always a good idea to keep it
simple or as the Zen of Python says,
2:34
if implementation is hard to explain, it's
a bad idea often you'll find that you can
2:38
approach software design like you would
the design of something in the real world.
2:43
Just like you wouldn't add a fish
radar to your screwdriver,
2:46
you probably shouldn't add a function
to send email to your calculator app,
2:48
practicality and logical thinking will
lead to better software every time.
2:52
As part of that practicality, remember
you're writing Python not Java or C# or
2:56
any other language.
3:00
If you come to Python from one of those
languages, you might expect me to talk
3:02
about getters and setters or
access controls on methods and attributes.
3:04
Python doesn't rely on
these design features.
3:09
And you'll rarely find a place where
they're absolutely needed in Python.
3:10
Stick to writing simple, clean code and
as little code as possible,
3:14
and you'll find yourself
a happier programmer.
3:16
All right, I think that's enough
talking about design for now.
3:19
Go back and look at the code we've
written together previously and
3:22
see if you can find places
to add simple classes.
3:24
Even if you can't add them right now,
keep those places in mind for
3:27
when you hit further through this course.
3:29
I'm sure you'll find all sorts of
places to improve our old code.
3:31
Now though,
let's get into the fun stuff and
3:34
start talking about the real wealth of
object oriented design, inheritance.
3:36
You need to sign up for Treehouse in order to download course files.
Sign up