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!

No comments:

Post a Comment