It is Sunday as I start to write this but I’m sure it will be posted just after midnight Monday morning, bringing a wrap to my first week of code bootcamp at Tech Hire. My classmates and I commiserated together all weekend over our Friday night homework assignment. We were tasked with going through all of the examples of code we’d seen and worked on that week and cleaning them up, paying particular attention to implement objects, arrays, and constructors when possible.
This was a huge task that involved a crazy amount of troubleshooting and problem solving for me, but I do feel like I came out of it much more confident in these concepts. We also were introduced to Git, which I know will take a while to learn, but I’m happy there is a process in place for version control. When we did our pair assignment on Thursday I kept thinking that there must be a way to simultaneous write new code while protecting old code, and sure enough there’s Git.
In an attempt to prove to myself my knowledge (or lack thereof) of these concepts I’m going to try to break down objects, arrays, and constructors.
Objects are items that we create that possess multiple parameters. For example, a circle is an object and it will have parameters that define its location, appearance, and actions. We can declare objects literally, which is how we did it initially, but it’s a lot neater to do so by using dot notation, wherein we list the objects parameters within itself.
See code below:
When we define an object like in the second example and create keys that can be accessed via dot notation, we are setting ourselves up to more easily call that object and manipulate it. By having the fill and ellipse functions entirely filled with variables instead of hard values, they can easily be modified.
Now a constructor is a type of function that defines an object and its parameters as a function, so that it can be called using an array. For example, if you want the circle to repeat 10 x but don’t want to copy and paste the code above 10x, you could use a constructor and an array.
Here is a picture of this code with comments to better understand the syntax and purpose.
Arrays and constructors are kind of too complicated to go to just for printing a simple circle. When you compare the code of our very first example to our most recent code, one might wonder why bother at all? But if you plan on adding motion or repetition to an object, constructors and arrays quickly prove their worth.
Now what we’ve actually been doing with our fancy constructor code is printing 200 circles at once. They all happen to have the exact same location and size, so they are displaying on top of each other and only appearing as one. But when I apply a randomizer to the x and y location…..
we get something like this
Now there’s a lot of ways we can play around with this. We could make our circles move and create a conditional statement that changes their direction as they hit a boundary. We could randomize the fill and get something like this:
But the fact is that we have a contained object that has parameters defined with variables that can be recalled and modified much easier, and the syntax is cleaner and easier to maintain.