In this case I highly recommend watching the videos as I go through how we can use data structures, algorithms,
and big o-notation to improve and refactor our code.
Transcript
Godot tutorials is not sponsored by or affiliated with the Godot game engine.
So what exactly
should you
learn next? And I want to
separate this into two categories. One is understanding the basics of computer science, and the second is understanding software project development for software, project developmental point you to get and get up once you've understood, get and get up. Take a look into understanding trunk based development, understanding it and trunk based development will help you work in groups. But in this episode, I'm going to cover basically the basics of computer science, which is data structures, algorithms and Big-O notation because we can use
these to
improve our ability to refactor. Let's start with data structures, data structures, understanding code, and it's ruined. Data management and
organization data structure
teaches us about the pros and cons and the different ways of organizing data. The two data structures that you have learned so far, if you took any of my previous courses, is the array and
dictionary arrays are
ordered collections. While dictionaries are unordered collections, both are used to manage data, but their purposes vary. If we take a look here, we have two different ways of organizing our values.
Zero, 10,
20, 30 in an array. We can group them on a single line or if the language supports it, we can push the values to the back of the array. Arrays are also useful for any type of management where we would like to control how data is ordered. Dictionaries are great when we need a key to associate itself to a value
and when we don't
really care about the order of our values,
both can
be iterated by for loops and gdscript. Again, arrays are great at organizing the placement of data in the order you want, such as organizing an array from smallest integer to largest integer
or organizing
strings by alphabetical order.
Arrays can be made more powerful
with the creation of two arrays and three days
arrays.
The negative of arrays, however, is that you are unable to access a specific piece of data outside of knowing where the placement of that data is in the array. And lastly, accessing two and three arrays can get quite complex. Dictionaries, on the other hand, are basically the polar opposite of arrays. Dictionaries are great when you want an easy way to identify and access a piece of data.
For example,
if we just want the first name of a person, we can just use dictionary, first name or dictionary last name for the person's last name. Again, because dictionaries are key value pair. As long as you know what the name of the key is, you will always get the value associated to that key. The negatives of dictionaries is that there is no way of organising the order of data. And this is again, because we have a key value association, we only care about the specific value to a specific key rather than the order or values are in.
And the less negative is that keys must
be unique, although this
is a pro or con, depending on how you look
at it.
Nexxus algorithms, algorithms is simply step by step procedure to be followed to reach a desired result.
If you followed my
courses from the beginning, you were introduced to a few ways that will lead us in building
algorithms.
The most common one is the for loop, which is great when you need to iterate over an entire data center collection of data. Again, we can use for loops for dictionaries and arrays. Algorithms don't have to be complex. As a matter of fact, if you use the for loop to iterate through an array, then you've used an algorithm A for loop inside of another for loop is an algorithm and it's a great algorithm. If you want to iterate over a two dering to iterate over a three D array, you use a for loop inside a for loop which is inside yet another for loop.
Leslie, even though big notation is outside the scope of this
series and this episode,
since I am doing a soft intro into data structures and algorithms to improve or optimize any algorithm you build yourself, there is no way of avoiding the understanding of at least the basics when it comes to Big-O notation and how it applies to your code. As a matter of fact, when starting out with learning Big-O notation, I recommend to learn to
count code
and count steps in your code. Counting code is different than counting steps because counting code lets you know how many lines are needed to be written. Counting steps lets you know how many lines are needed to run to complete a specific task. However, just understanding or learning to count code will vastly improve your ability to refactor. Anyway, let's go into a soft intro into big O notation again. It doesn't have to be complicated. As a matter of fact, you've always been exposed to big ol notation. If you watch any videos on coding. Let's take a look at this example with big notation.
We say that the function code
test, which takes in
zero arguments or has zero parameters, takes two steps to complete. And that is because each print statement, each line of. Is a single
step, and since we
have to print statements, that means function test takes two steps to process and complete.
Now, let's change
this up a bit and add a parameter to our function. So here's a trick question. How long does our function take to run and complete if we have an array with a length of one thousand, basically one thousand items inside Iraq?
And the answer
is it still takes two steps to process and complete our function tests, despite having
an argument more
specifically an array past inside our test function that has one thousand items. Now let's add two more print statements and our code takes four steps to complete.
And as you notice
here, it's getting a little bit annoying that I have to mention how many steps a function takes to complete.
And so
is there an easier way to describe all of these
functions? And yes,
we can say that our code runs at constant time. And if you want to get into Big-O notation, we would say, oh,
one or all of one.
And this is because no matter how big our argument, more specifically, no matter how large our array grows, our function will take the same amount of steps to complete.
The number of
steps that function test must take does not grow or shrink. Therefore, we can say that it runs constant time. Now, again, we don't really need to know big notation. Just focus on learning to
code because we can use
that to
analyze our code
in order
to refactor it or find
areas in our code that need refactoring. Before I go over a small coding example which I will not upload to GitHub, let's take a look at how we can use data structures, algorithms and Big-O notation to improve our ability to refactor our
code, more
specifically dictionary for loop and just basic counting code. So here we are. Let's go ahead and see how we can at least use the basics of algorithms, data structures and big notation counting code to help improve refactoring.