Instructor: It's happening,
our next developer fundamentals.
And that is what is good code?
I mean, what is good?
That seems so vague, right?
Like when somebody says,
I'm a good coder,
I'm an excellent programmer,
he's a senior programmer,
he's an expert programmer,
he's a tech lead,
he's a genius.
What does that really mean?
Well, you can narrow it down
to these simple statements.
What is clean good code?
I kind of gave it away.
The first one is clean.
That is,
we wanna make sure our code is
following a style that,
let's say, the Python community endorses.
Are we following the best practices?
And Python has
this great feature of auto-format,
and Python has standard ways
of using spaces
to make sure that our code is clean.
I'm not using unnecessarily ugly spaces like this,
or maybe making lists
really weird and funky,
maybe no spaces in between here,
maybe I have a lot
of random comments over here.
We're keeping our code clean.
We make sure,
that every line that we have is
easily readable,
but also we don't have any extra stuff
that we don't need in there.
And this also relates
to the idea of readability.
Now, readability means
the ability to, well,
to read your own code,
maybe two years down the road,
you're gonna come back
and look at the code,
are you gonna be able to understand it?
If you work in teams
or for companies,
and other coworkers come
and look at your code,
is it easy to understand?
And here, this is obviously personal preference
but most of the time the ideas are simple.
For example,
I'm using names here that make sense,
if I had variable names
that don't make sense,
it'll be really hard for somebody
to read my code.
Maybe is people will be confused
why I have this print statement here.
In that case,
maybe I should comment in here being like,
need a new line
after every row,
or maybe they'll be confused
why we need this as well,
so depending on your style,
you might want to add comments,
make sure that the naming
of your variables are good,
and maybe making sure
that it's not only you
that understands the code.
The other one is the idea
of predictability.
And this is my favorite one,
that is sometimes people try
to be really clever with their code,
trying to have the most compact code
or using the newest features,
or some really obscure tools that,
or functions that are not very common just
to look well, frankly, smart.
But you wanna have code
that makes sense,
that does things just one thing really well,
now this code is quite small,
so it's quite predictable.
You know, it just has one pass-through
and it's easy to predict
what's going to happen,
we're going to print,
but as we get more
and more into the course,
you'll see that our code is
going to get larger and larger,
and having predictable easy
to understand code is really important.
And then finally,
this is an important principle
that you're here everywhere is,
DRY, do not repeat yourself.
You don't wanna have code
that you're constantly
repeating yourself over and over.
Again, this is a small example,
but it's very easy as a programmer to say,
"Oh, I'm going to copy this,
and then I'm going to run this again
so that if I click run,
look at that,
I have two trees now,
this is amazing."
But maybe that's not the best idea,
you now have 28 lines of code,
and people might be confused,
why we're doing this twice?
Maybe instead we wanna programmatically
have a counter here,
of maybe saying,
running this twice,
or maybe using something like functions,
which we'll learn later to make sure
that we can repeat this process over
and over and over.
Now, keep these in
the back of your mind,
because throughout the course
we're gonna explore these topics.
But let's say I just came onto this code
and I wanted you to clean it up a bit.
Again, this is just personal preference,
but these are some of the things
that I would do.
One is here,
pixels equal to 1,
instead, this is kind of confusing,
I'm just going to say,
if there's a pixel here,
and I know that pixels
are either a 0 or 1,
then print here,
because this is a truthy value
that will just evaluate to true.
So that just kind
of cleaned out my coat a little bit better.
Next, another thing that I might do,
maybe I wanna make
this more extensible,
which might mean doing something like,
let's say fill variable,
that equals to the *,
and then maybe empty,
which equals to an empty string,
and change these to fill
and empty.
Now this is extra line of code,
but for example,
if we're doing this multiple times,
instead of perhaps maybe
we're running print over and over and over,
or maybe we have
another line of print over here,
and another line of print over here,
and then maybe another line
of print over here that is empty.
Instead of me having
to change five locations here,
I can now change just this part,
and maybe instead of star,
I wanna display, I don't know,
dollar sign.
And if I run this,
all right, I get a little different image here,
but again,
I was able to just change the variable,
and the variable was used
to fill out whatever I needed done.
Now these are all things
that you do need a bit
of experience to get used to,
but we'll hopefully explore
this throughout the course,
and by the end of it,
you'll have all these best practices in mind.
I'll see you in the next one.
Bye bye.