Oh the Things You’ll Learn. How to Set Up your Project for your First Solo Build and Get it on Github.
When you begin your first solo project, it can be daunting. Previously, my coding bootcamp had always provided a built in local text editor for all our labs and exercises. When you start your first project, you’re full of ideas on what to build, excited to show off how much you’ve learned and you open your local text editor to face…a blank screen.
You know how to code, but how are you supposed to get to coding if you don’t have all your directories and files already set up for you?! We’ve all been there, or at least I have. I learned quite a bit on this project and I hope that I can teach you from my experience.
Warning, this is going to be a long post. I may break this up into two separate posts at some point!
How to Create your Directory to Store your Folders and Files
To start off, make sure you have a Github account. Github is a version control tool coders use to reference past versions of this code. You were working on a project and had it running smoothly at 9 PM, you update and tweak a few things, it’s 3 AM, you hit run and error. Not just one error, A LOT of errors. Panic ensues because you don’t know when your code took a left turn and went so wrong. Well enter Github. While you code you can save the current version of your code, and if your code breaks beyond repair, that’s okay because you can go back to past versions of it.
- Open your terminal. If you have windows, I’d recommend setting up a WSL local environment. I use WSL: Ubuntu.
- In your terminal you’ll need to ‘cd’ into the right directory. ‘cd’ stands for ‘change directory’. Since I’m going to be coding a new project from scratch, I want to set everything up in my “projects” folder. Below is my end goal, but how do I get there from the terminal?
There are shortcuts you can set up to automatically start in a folder such as the “Coding” folder listed above, but for now we’ll do this the long way. So how do I go from my terminal looking like this:
To this:
There are a few basic terminal commands you’ll learn quickly.
- “cd ..” allows you to go to a parent directory (i.e. My “Coding” folder contains my “Flatiron” folder, so if I’m in my “Flatiron” folder and I want to get to my “Coding” folder, I type “cd ..”
- “ls” ls stands for list files. If you want to know all the file options within a folder you type ls. The terminal requires you to be exact to change directories, so this is a handy one to know. Once you know the folder you want to cd into, just type “cd [insert folder name here]”
You’ll use cd .. in the beginning to get to a directory that you know your destination is contained into and then keep using a combination of “ls” and “cd [name of directory]” until you get to your destination.
Pro tip: if you start typing out your folder, try hitting ‘tab’ and it should autocomplete the folder name for you!
Note: my terminal doesn’t start me in the “C:” folder, and I don’t know what the code or bashrc options do, so I start off by cd-ing back to a further parent directory to get my bearings. I do know that my “c:” drive is within the “mnt” part of my computer, so I go there and I’m off to the races. Here’s how I did it:
I skipped a few steps here to go from my “Coding” folder to my projects folder, but you can see me cd-ing into them below. I skipped the ‘ls’ steps because I knew exactly where I wanted to go. Pro tip: you can type multiple steps in one like I did below.
Okay, my apologies for the terrible formatting, medium doesn’t make it easy to post all those streamlined together, but that took some time to learn and I hope an over abundance of screenshots helps the next new coder out!
Now that that‘s over, here’s how to set up your own project. You’ll type all of these in your terminal. Instructions below the screenshot!
- Make your own folder to host all the code for your project. My project was was called “cli-api-census-county-demographics”. In hindsight, that’s way too long. I’d recommend choosing something simpler! “mkdir” stands for make directory and it creates a folder for you from your terminal.
In your terminal type in “mkdir [insert name of your project here]” - cd into that folder you just created and then set up your directories that you will use within your project. My project needed a bin, lib, and config directory, but yours may not, so keep that in mind when making yours.
- For Ruby users only — type “bundle init” to create a Gemfile where you will list all the gems you require in your code.
- Create your README.md markdown file by typing “touch README.md”
- THIS IS OUTSIDE OF YOUR TERMINAL: Go to Github and create a new repository (“repo”). Since you’ve already created your readme.md file, when you do this, DO NOT initialize your new repo with a readme. Once your repository is created on Github…keep going.
- Back in your terminal, type ‘git init’ to initialize your GitHub Repo you just created.
- Type “git add .” This will add every file, directory, etc that you’ve created on your local environment and add it to your repo (well…it’s not there yet because you haven’t pushed it to Github, but it’s ready and waiting to be added to your Github Repo!)
- Type “git commit -m “first commit”
*Note: this is how you list your version control I mentioned above. More on this later. - Type git remote add origin [insert link from your GitHub repo that you created]. Screenshot of this below step 11.
- Type “git push -u origin master”
- Celebrate!!!
You’ve connected your first project to Github and you’re ready to code! Now you just have to create whatever folders you need to code and start coding!
A Git crash course on commands and how to create version control throughout your project.
A few commands you should use early and often are “git add .”, “git commit -m” and “git push”. This is how you actually create and catalog the different versions of your code as it evolves. Every time you try to switch ‘topics’ in your code (i.e. write a new method, create a new class, etc) you should update your git history. That way you’ll if your code breaks right after it was working, you know it has to do with that exact thing you were just working on.
Pro tip: I normally use all three of these one right after the other. I add, commit, and push it all at once.
- “git add .” You’ll write this in your terminal after writing a few lines of code or a method. This adds the changes you made in the working directory to your staging area. The staging area is just a fancy term for telling Git that you want to include updates to a particular file in your next commit. This does not affect your repository in any way and doesn’t change your Git repo significantly because they aren’t actually recorded in your repo yet.
- “git commit -m “[insert a comment about what you did in your code here]”” That’s a lot of quotation marks all together, so check out below what it looks like in action. When you push your updates onto your Git repo, your comments within the quotes will tell your future self or other team members what updates you made to your code during this time.
- “git push” This will ensure that your code officially makes it to your online Github repo.
Try playing around with “git log” and “git status” as well!
A Highlight I learned from my Project creating a CLI to interact with an API.
If you were wondering if I learned anything cool doing my project, the answer is lots! Here’s my favorite method I used, the self.send method within an iteration.
Now that you’re ready to make you’re a pro with Git commands. I want to share one really fun method that I enjoyed using when creating my command line interface for the US Census API program that I built. I used the HTTParty gem to download the data with the US Census API and the output is … messy. In order to get the downloaded data into a usable format, I created a key/value hash for each variable of data that I wanted to look at and then used the #collect method to push those hashes into an array that I could work with.
However, once you have that array of hashes, how are you able to access that data?
For those reading my code below, there is a negative 10982398134% chance you’ll need to have attr_accessors labeled like I do. Since the US Census didn’t label anything, I wanted to know what each part represented.
Here the counties_array argument that is passed into the create_from_collection Class method takes in the array I mentioned above that is filled with hashes from each county in the United States. Within the iteration, the county hash gets sent to the initialize method and is iterated through again. This time, I use the self.send method to call the writer method for each attribute listed in the attr_accessors and set it equal to the value of each key from the hash. Note: Self here refers not to the instance of each county, but the CountyData class itself. Below is an example of what the county hash looks like before going through the initialize method and how the #send method calls the writer methods to set each attribute to a value.
In this example, since the instance above is ‘self’, if I called self.name, Washington County, Mississippi would return and if I called self.tot_pop, 47086 would be returned. I thought this was kind of the coolest thing ever, but then again coding is pretty darn awesome. If you made it this far, I’m really impressed, because gosh this is long, but you deserve a medal. I hope you learned something and maybe even enjoyed yourself as well!
Until next time, peace.