Wednesday, October 29, 2014

That "duh" moment you realize you're writing too many lines of code

This morning is day 5 of 21 rolling out of bed by 5 am. If you haven't been reading along, the idea came from a blog post I read a few weeks back, and looking at my schedule right now, it's the easiest time to guarantee time to study and work on my code.

It is interesting that it almost becomes easier to get out of bed everyday. Prior to deciding to try the 5 am challenge, I kept setting my alarm for 6 am and it became a morning routine to silence or hit the snooze and I'd tell myself "not today, you can go back to bed and I'll get up tomorrow" and I'd sleep until 7:30 or 8 in the morning and then get up and rush for work. Now it's becoming easier to pull myself out of bed promptly at 5 and then I grab a cup of coffee and sit down peacefully and distraction free to work.

This week I've been working on my game that I started way back in the summer which I kind of "forgot" about. It's gone through many iterations and started out as a simple JavaScript game that would run via a ton of window alerts. Then once I found out that wasn't a good idea, it started to evolve and it is now somewhat GUI based and includes HTML/CSS/JavaScript/jQuery. I think once I get a working version, my plan is to even transition the core JavaScript over to Ruby.

The challenge of being self-taught is that you don't always know when you might be doing things wrong. Your solution can work, but it doesn't always mean that the code is efficient or following best practices. It may work well for one user, but if you tried to scale it up to millions of users, things would start breaking or go incredibly slow. That's kind of where I feel like I'm at right now with some of the JavaScript/jQuery code. It seems to be working, but it's not as smooth as I'd like and I don't think it's very efficient yet either.

Even as I sit here typing, I just realized one major inefficiency and something I can do to improve my code! I was going to give a snapshot of part of the code as kind of a "before & after" look at how messy I feel the code is right now. Well there is a concept called DRY which stands for "Don't Repeat Yourself". That basically means if you have a chunk of code that you would have to type in multiple places, it's best to build one function or method that handles that code, and then call your function or method when you need it. The reason that is so effective is that if you later realize you need to add, modify, or delete some code, you can do it in one place rather than the 15 or 20  or 1000's of places you might use that code throughout your program:

In the highlighted text, you'll see two lines of code that I'm using a number of places in my code. As I went to take this screenshot, that "duh / ah hah" moment finally hit me.


Rather than writing each of these lines of code 5-7 places throughout my code base (basically once per each scenario or transition in my game), I'm going to create a Function to handle this for me.

New Function called "change_div" to reduce the amount of code re-use:


Now if I need to make a simple change or add a line, I can do it in this one spot.


Calling my new change_div function with the highlighted piece of code:



I nearly didn't write a blog post this morning because I wasn't exactly sure what to write about. However, I want to keep up with it and part of #my5amChallenge is to write about the things you learned or accomplished. Well I'm glad I sat down to write this blog as I just learned something very valuable about half way through!

That's kind of how it works with coding sometimes....you get stuck somewhere and try and try to figure something out, and then it all of a sudden comes to you at the most random moment. I wasn't really stuck there, but that change is much better for an "Object-oriented" programming style which is what I'm trying to learn.

Time to dive back in the code and work on building some more of the functionality out!

Saturday, October 25, 2014

Equality and Ruby Variables - a few of my learning's from this week

I've been trying to get up at 5 am everyday this week to work on my coding, and I actually did a pretty good job except for Friday morning. I'm a huge Broncos fan and we had the Thursday night game so I didn't make it to bed until nearly midnight. After getting up at 5 am for a few days, I was just too tired and needed the extra sleep.

I did make it up at 6 am today though (even though I'm not doing the 5 am challenge on weekends) and have been working on my problems. I wrapped up my first problem which was implementing a simple Array class in Java and Ruby that has "create, delete, search, and display" methods for working with the array. I thought I would take a few minutes to at least share the things I learned and a few of the "gotcha" moments.

When trying to implement the solution in Ruby, my first stumbling block was working with & declaring the right type of variables. In Ruby, you have global variables, class variables, instance variables, & local variables. I'm going to try to briefly explain them, but to help give context I'll give a simple example of classes & objects.

If I were writing a program to control working with vehicles, I would potentially have a "Vehicles" class, and two sub-classes called "Cars" & "Trucks". Classes help control behavior and allow you to create "Objects" which all share similar behavior. I would create certain behavior in the "Vehicles" class that would apply to both Cars and Trucks, and the "Cars" and "Trucks" classes would both inherit that behavior. The reason I would do this is that both Cars & Trucks have Tires and doors, and windshields for example, and rather than specify these features in both classes, I can type it once in Vehicles and let both classes inherit those features.

Now once I have my classes, I can then create specific objects. So I have a "Cars" class that outlines the features and behaviors, but then I create a "Car" object that is the actual car itself with its own unique make, model, color, etc.

Hopefully I made that simple enough and not too confusing, but not on to explaining the variables and where I got stuck.

Global variables - are just what the name suggests, they are globally available throughout your program. So if I have a Vehicles Class and Animal Class, I might declare a variable in one of them, but I could still access that variable from the other class as well

Class Variables - class variables apply to that class & the objects within that class. So if I have a Vehicles Class and a Animal Class, and I declare a variable within the Vehicles class.. any of the objects within Vehicle can use that variable, but the Animal Class & all of its objects cannot see or access that variable.

Instance Variables - Instance variables are accessible across methods for each instance of an object. So going back to the Animal Class, I might have a "make_noise" method and maybe I need to create a variable "noise" to store information. I could have a "Dog" object that stores "bark" in the noise variable and a "Cat" object that stores "meow" in the noise variable. So basically I can use the same variable name, but its a unique variable each separate instance of the object and can only be accessed by that object

Local Variables -  local variables are only accessible within a single method. So if I had a "Dog" object, he could have different methods such as sleep, eat, walk, etc. If I declare a local variable within the sleep method, eat and walk cannot see that method. To help tie in the explanation, if I instead declared an Instance variable above, the variable could be seen and accessed in each of the eat, walk, and sleep methods.


Didn't meant to turn this into a long definition post, but sometimes I get carried away :)  The main point of all of that is that when creating my Array program, I wanted to create an Array class that could create Array objects. I was trying to write my code similar to how it was in the Java code, but for Ruby I needed to create the scope for my variables. I needed an Instance variable that would apply across that object so that in each method I created, it had access to my Array object.


The second "gotcha" or stumbling block for me was equality. In programming, = vs == mean two completely different things. With a single = sign, you are setting something equal to something. For example, a = 5 will set the variable "a" to equal 5. Whereas with two equals sings "==" you are comparing two things. So with "a == 5" you could compare does "a equal 5". If it already does, one thing can happen, but if it doesn't something else might happen. I had a few different areas where I used a single equals sign instead of a double equals sign, and it created odd behavior in my program.

One thing I've found is that when learning to code, you are trying to absorb so much new information that it can be easy to slip up and miss one minor thing, such as a single = sign versus a double == sign. I suppose the bright spot is that I can at least identify and fix my error now rather than having to post to Stackoverflow for someone else to explain it to me!

Well back to coding for a few more hours before I get my weekend started!






Thursday, October 23, 2014

Feeling tired

Day 2 of my 5 am challenge and third day in a row waking up before 6 am. I'm EXHAUSTED this morning, but forced myself out of bed and am slowly feeling more alert. I have to start getting to bed on time so I get a full 7 hours at a minimum.

Anyway, I finished up the CodeAcademy Ruby tutorial yesterday and have the first parts of my current Data Structures problem complete. Here is a quick snapshot of my current code state:



I also forgot to mention that if you're a developer or learning to be and use Github, my handle is austi003. I'm trying to work on keeping all of my work updated and I even have one collaborator (my mentor) on the Data Structures repo so that I can get used to pull requests and branches and all that good stuff. 

Alright, enough writing this morning, time to start learning! 

Wednesday, October 22, 2014

5am Challenge - Day 1 (officially...)

Even staying up a little later than I typically do, I still managed to force myself out of bed around 5:05 this morning. It's actually not so bad once you actually stand up for a minute or two and force yourself out of the comfort and warmth of bed. I let the dogs out as usual, grabbed a quick cup of coffee, and now time for a quick update before I dive back into my studies.

One thing to mention for those that have been following along... I've heard some great advice from other bloggers (about code) that I thought would be useful and I'm going to follow a similar format. That is, I'm really writing these for me as a refresher, and if I'm able to provide value for other readers great! The basic idea is that I can blog about things I learn, and it keeps a running record of documentation (or notes) on topics and problems I encounter. That way if I run into something similar down the road I can think to myself..."aha..I wrote a blog post about that way back when..." and search back and find my notes.

I'm getting to the point with code where I can start to read and understand, but it takes me longer to actually write or come up with my own code. It's very similar to learning a foreign language, like Spanish for example. I took a few years of Spanish in high school, and while to this day I can read certain things and understand the gist, it is much harder to listen and understand a native Spanish speaker, and even harder for me to form more than a very basic sentence that even a 4 or 5 year old would say.

I'm currently working on Ruby because I'm trying to get the syntax down enough so that I can implement my Data Structures in Ruby as well as Java. Java is a lot more rigid and you have to directly state certain things, whereas in Ruby you can write less code and accomplish the same thing. Ruby reads a lot like pseudo code (very similar to plain English just explaining on how a block of code would work without worrying about exact syntax).

For example, here is one simple line of Ruby that anybody can probably guess what it does:

    def greeting(name)
          puts "Hello #{name}!"
    end

   greeting("World")


As you might have guessed, the output of this simple Ruby script would read "Hello World!"

I spent about 2 hours yesterday morning and another 30-45 minutes yesterday evening both working through the CodeAcademy (www.codeacademy.com) Ruby Tutorial as well as updated my first problem I'm trying to implement in Ruby. I at least got the first part working which is really just an Array class to construct my arrays from. I got to the Object Oriented section of the Ruby tutorial which really helped because it also reminded me of Instance variables and what scope I need to provide which variables. I'm pretty sure the reason my initial solution was not working was for this very reason. I was trying to call a variable from a different method that did not have access to that variable. Here is the little bit of code so far, but I should get a lot more of it done today:



Yes it doesn't look like much for over 2 hours, but it was more studying through CodeAcademy and then refactoring my code to make this first part work, so I'm proud of it! That's it for today...time for some studying!





Tuesday, October 21, 2014

The 5 AM Challenge

5:40 AM - Tired! It's been quite a while since I've posted anything, and too be honest I haven't been very disciplined about sticking to my lesson plan either. About a week or two back, I read an interesting article about somebody that woke up every morning at 5:00 am for 21 straight business days (he did exclude weekends). The idea behind it was it takes 21 days to form a habit, but he also found that he is more productive early in the mornings. He was able to get up distraction free and do several hours worth of work before the rest of the world even wakes up. Emails, workouts, sunrises, etc.

It got me thinking and I've decided to try it myself as early morning is the best time to study and work on my coding completely distraction free. I didn't make it out of bed until 5:30 today, but it's at least a start.

So for 21 business days, I'm going to get out of bed at 5 am and from 5 - 8 am I plan to do something productive. Most days it will be studying and working on my coding problems, but I may try to mix in the early morning workout too. My plan is to write a blog post each day about something I learned or accomplished, but given my track record we'll see how well that goes :)

So here it goes...time for some Data Structures work!

Sunday, August 31, 2014

My lesson plan

It's been awhile since I've posted (or really been able to work on my coding for that matter) so I decided I would try to post an update today and lay out a general guide I plan to follow. As much as anything, this post is just to lay out my plan and to create something a little more concrete, but maybe you'll find something of interest :)

At the end of July I made a trip to Vegas and then the following weekend made a trip home & I pretty much got completely derailed. While I did do some studying this month, I got out of my routine for coding, and now its time to jump back into it!

A question I commonly hear from new developers that are trying to teach themselves is "Where do I start?" or "What language should I learn first?". There tends to be a LOT of confusion on exactly how to go about teaching yourself. I have the benefit in that I sit around a ton of bright software developers, and thus have a better idea of what I should be teaching myself. In the near future I have a whole other post on that topic so stay tuned.

For now here is my plan: 

As much as I would love to spend countless hours each week studying & coding, I do have a full time job & other hobbies to balance as well, but I am hoping to dedicate on average 15 hours per week (min 10 hours per week) over the course of the next year (or however long it takes me). Two key areas I've been told to focus on - Data Structures & Algorithms, and Object Oriented Programming/Design. My focus over the last few months was to dive in on one topic until I completed it, but one of our Engineers brought up a good point last week; if you're in school you study multiple subjects at once, and he encouraged me to do the same.

I feel fairly confident that I can dedicate at least an hour and a half each night Monday - Wednesday with Thursday left open for Trivia and Friday is sometimes a go out night. That gives me between 4.5 - 6 hours between those three nights. Weekends will be heavier and I plan to spend 3-4 hours each day on Saturday and Sunday for another 6-8 hours total. Finally I'm hoping to have 30 minutes four days of the week either early morning before work or during my lunch break which adds up to another 2 hours. If I only hit the minimum on each of those time slots it still adds up to 12.5 hours per week of study time, but I do feel most weeks I can hit more.

Based on books I'm working on at the moment, my plan of attack is:

Monday - Data Structures & Algorithms in Java - 2nd Edition by Robert Lafore

  • This is a pretty intensive textbook I plan to study through, but I will also be implementing the various algorithms in not only Java (which the textbook shows you how to do) but also in one other language, most likely Ruby. This will help me to not only solidify my understanding of the concepts, but also help me to pick up syntax of another language at the same time.
Tuesday - Head First Object-Oriented Analysis & Design

  • Same general idea, study the textbook but work on implementing the solutions as well. I believe this book does a good job of giving you various problems & projects to work on.

Wednesday - Agile Web Development with Rails 4

  • My plan is to go through the book once to build their Depot application, and then I want to go back through the book again to do my other app for storing recipes.
Weekends - Saturday and Sunday will be left a little more open but I'll probably split the time evenly between studying material in the textbooks versus working on some coding whether its the depot app or my RPG game that I still have to finish

Early Mornings/Lunch - I'll probably do either CodeAcademy tutorials or other light reading as I can't dive too deep in 30 minutes. But it will still be a good opportunity to refresh and read up on certain things.


I'm going to give this a go until I start to get through some of these books, and then I'll re-evaluate and see how its working out.

Saturday, July 26, 2014

The "ABL's" of Programming

For anybody familiar with Sales, you've probably heard of the "ABC's" of selling which stands for "Always Be Closing". For people not familiar with sales, its basically the concept that if I were trying to sell you a tennis ball, and you objected and said you really don't like green tennis balls, I would ask you a clarifying question to understand what colors you might like, and then I go right back to the close with something like "so which would you prefer, a tennis ball of color 'x' or color 'y'?" Basically, as you uncover information about your prospect, you're still pushing for the close every step of the way.

As I was driving home yesterday and thinking about a good topic to write about, the ABC's kind of popped into my head and it made me think of a good acronym that applies to anybody that codes, "Always Be Learning" or as I titled it, the "ABL's" of Programming. I thought of this both because of some of my recent struggles, but it's also been a consistent theme on a Ruby Rogues podcast I've been listening to recently.

So how does it apply to me recently? A few weeks back, I had this idea to try and program a basic Text based adventure game in JavaScript that would present you with various scenarios and options, and based on your choices you'd progress through the game. I knew it would test my knowledge of several concepts like For Loops, While Loops, Iterations, etc. Well the first weekend I dove head first into the game and felt like I made a ton of traction. It almost seemed like a relatively easy concept that I would be able to finish and release within maybe a week's time frame. Well here I am about 3 weeks later, and still no working version of the game!

While I made a lot of progress that first weekend, my code gradually became more complex, and it started getting harder to keep things straight or decide how to pull the information I needed. After getting some advice from a developer that sits by me, and studying further into the CodeAcademy JavaScript tutorial, I then realized I could re-structure my game to follow a more traditional "Object-oriented" fashion and take advantage of JavaScript's Prototypal/inheritance nature. That led to a whole weekend of diving in, getting really confused, and then slowly figuring out how to once again structure my game and make it work. However, I learned a ton and my code was far better off because of it!

Well now I've run into another issue where one of my iterators keeps looping and is not escaping, so I worked briefly with another of our Software Engineers yesterday, and he mentioned that an "alert" method I'm using does not work well with browsers, and that I should strongly use jQuery for what I was trying to do. So now after about two weeks, I'm diving into a completely different area and once again deciding to re-structure my game to make it work better and become easier to work with moving forward.

I spent yesterday researching how to implement the jQuery library, which then also led me to pull in HTML/CSS and changing my game to a more graphical nature in the browser rather than being completely text based from JavaScript functions. While it's not absolutely necessary that I do this, I do envision where this is going to make certain aspects of my game a lot easier and a lot more efficient to code. I even learned how to make my page somewhat "responsive"! That basically means that if you were to start with a full screen, and start to shrink the screen, the elements will shrink along with it so that the page remains readable and functional. Especially since a web page can be accessed by so many devices including tablets, phones, laptops, etc. all of which have different size screens, implementing responsive design is an absolute must.

So basically what started as a simple JavaScript program with basic "for loops" & "iterators" has now evolved to be so much more. At least I've learned concepts and a little bit about other languages & libraries that I've been able to apply to my code. I've faced some type of challenge on an almost daily basis, but I've learned something new every step of the way and feel so much more confident today than I did a few weeks back when I started the game!

Software Engineering is definitely a field and skill set that requires you must "Always be learning!".