It's happening our next developer fundamentals, and that is what is good code, I mean, what is good
that seems so vague, 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 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 want to make sure our code is following a style that lets say the Python community endorses,
are we following the best practices?
And Python has this great feature of the 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 want to read
your own code maybe two years down the road, you're going to come back and look at the code.
Are you going to be able to understand it if you work in teams or for companies and other co-workers?
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 are simple.
For example, I'm using names here that make sense.
If I had variable names that don't make sense, will be really hard for somebody to read my code.
Maybe people will be confused why I have this print statement here.
In that case, maybe I should comment in here being like I 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 a 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 want to 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 deha.
Why do not repeat yourself?
You don't want to 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 twenty eight lines of code and people might be confused
why we're doing this twice, maybe instead we want to 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 going to explore these
topics.
But let's say I just came on to 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 one instead.
This is kind of confusing.
I'm just going to say, if there is a pixel here and I know that pixels are either a zero on one, then
print here because this is a truthy value that we'll just evaluate to.
True.
So that just kind of cleaned out my code a little bit better next.
Another thing that I might do, maybe I want to make this more extensible, which might mean doing something
like, say, filled variable that equals to the star and then maybe empty, which equals to an empty
string and change these to Phil.
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 there, then maybe another line of print over here that is anti.
Instead of me having to change five locations here, I can now change just this part and maybe instead
of star I want to 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 will hopefully
explore this throughout the course.
And by the end of it, you'll have all these best practices in mind.
I'll see in the next one.