Sunday, November 30, 2014

Building Tic Tac Toe

This Saturday, I had some free time, so I decided to do some coding.

I thought it might be fun to build a puzzle type of game. It would involve using some complex data structures and algorithms that I have been reading about.

But as I started planning, I realized building a new game from scratch seemed a little bit daunting. Instead, I decided to write a program where I could simple play Tic Tac Toe.

I figured this would give me a decent framework in which I can build more complex games later.

Version 0.1 of this game is done and available here.

Wednesday, November 26, 2014

Hello World Project

I created a new git repository called HelloWorld.

(Here is the README for this project)

Hello World

This repository contains programs written in different languages that simply outputs the classic phrase Hello World.

For each language, I'll try to keep the code very simple and understandable, yet not completely trivial. For example, in the Python program, rather than just calling 'print "Hello World" ', I called a function, created a class, then had the class do the printing.

Motivation

Provide a way to view code snippets from many different languages.

Background

I had a bunch of these "hello world" type programs littering my computer in different directories. It felt messy and disorganized. By putting them all into a single repository, it's easier for me to find things I wrote before, and it looks a lot more impressive (to me at least).

Invitation

Please feel free to send me a pull request. If you are a new programmer, this is a low risk easy way for you to contribute to an open source project. I'm pretty sure that almost any pull request will result in some merged code.

Monday, November 24, 2014

Thoughts On Unit Testing

Adopting unit testing ...

Much has been said and written about unit testing before. Those previous comments convinced me to try to add unit tests to whatever code I was writing. As I have gone from a non-unit tester to a novice/intermediate one, I have observed a few things.

Unit testing instead of print statements.

Every programmer begins by writing a "Hello World" program. By writing such a program, you learn how to compile, run your task and verify your output. As I built more programs, I still had the old "Hello World" verification process in mind, and I often littered programs with print statements. These print statements would give me confidence that things were working as designed. After I was sure a function or class was working as expected, I would clean up these print statements that I had been using to help me develop.

It dawned on me that print statements that I used during development could be replaced by unit tests. If I wanted to check if a function was being called, I could write an "EXPECT_CALL" test, or if I wanted to validate that a variable was updated in some way, I should invoke an "EXPECT_EQ" test.

Advantages to Unit testing instead of print statements.

There are lots of advantages of using unit tests over print statements. You ask a computer to verify output rather than a human (you) from reading a terminal screen. Verifying mundane things over and over again is ideal work to shift from you to a computer. You keep unit tests in your source control even after you ship your code to production. Which means that your tests can be run again and again for all of time.

The only advantage of using print statements is that it is easy to use. This is true in the absolute bare minimum short run. But for any complex piece of code, it is not true. Are global variables easier to use than classes with encapsulated data? Yes, it is easier to directly modify and access a variable that sits in the global scope. However, it is much harder to understand and debug code that relies on global variables. Over time, the hard-to-use classes that contain private data, setters/getters, and other complexities actually become easier to use. You just have to get used to it. The same goes for unit tests. At first, they seem to add a lot of extraneous code for something that is clearly trivial, but over time, they are worth it.

My way of doing test driven development (TDD)

I have heard that test driven development is the way to go. In practice, I have found it a little bit odd to write a test before doing anything. I mean, how do you write a test for a function that does not exist? Maybe this is clear to others, or maybe I am doing things the wrong way, but here is how I do TDD.

  1. Write an interface (either a class or function). The interface should have some dummy implementation so that it compiles and even runs.
  2. Write one test that checks one feature of the interface. This test should fail.
  3. Update the implemntation such that the one test pases.
  4. Repeat steps 2 and 3, until your implementation supports every feature you need.

I find that this process makes me think about a good interface, and it makes me think whether any feature would pass the old YAGNI (You Aint Gonna Need It) smell test. It works for me, for now.

Sunday, November 16, 2014

Becoming a Stackoverflow producer

Being a stack overflow consumer

StackOverflow is an awesome source of information and code samples. It is really easy to be a consumer of this data. If you search on Google, then you will find answers on StackOverflow. It is like magic.

It is good to be a producer

I think it is a good idea to post on stack overflow. While posting an answer, it forces you to really think. Will this answer be easy to understand? Are there any better alternatives? By trying to provide explanations for concepts, I have found many holes in my understanding.

There is also some less tangible benefits from posting. It feels good to give back to the programming community, and it is also fun to have an answer accepted and gain reputation points.

Becoming a producer

Being a producer looks worthwhile, but it is harder than being a consumer. I was always a bit scared/intimidated to post a question or an answer. If I post something, someone may realize how much of a fraud I am and expose my weak programming skills. It is the same fear that I have when I want someone to review my code.

I do not want to be paralzed by this fear. So, this weekend, I decided to find an unanswered question and post an answer. I spent a decent amount of time on it, and my answer is about ten times longer than the question (so I hope it was worth it). I think the trick for overcoming the imposter syndrome is just to practice exposing your code and thoughts to others. After a while, you just get used to it, and your fear of being found as a fraud is just washed away, but all the benefits of producing remain.

Tuesday, November 11, 2014

Using the Android Emulator

First try with android emulator -> too slow

When I first started Android development, I tried to use the emulator, but it was painfully slow. I figured that it was because my laptop was weak (ex: it only had 1 GB of RAM). Luckily, I had an Android phone, so I replaced the emulator with an actual device. Given my weak laptop, I could not be sure if the problem was me or the emulator.

Second try -> still not worth it

After some time, I bought a new laptop with a little more oomph (4 GB of RAM !!!!). The Android emulator performed a little bit better, but it still performed worse than just using a real device. Since the apps I was building did not vary much from one device to the next, this was just sufficient. At this point, I was close to giving up on developing with an emulator forever.

Third try is the charm

With the latest release of Android L, I really needed to use an emulator. The SDK had retired an API that I was using, and I had to depend on a new one. A factory image of the new version of Android was not available for any device I owned. An emulator was my only option, which I feared would bring my development output to a crawl.

Luckily, I found that changing two things really improved the performance of the emulator - from unusable to rather snappy.

  1. Install the Intel Hardware Acceleration Manager. This is available in the SDK Manager in Android Studio, but it did not seem to work for me. So, I manually installed it.
  2. The other thing I did was change the emulator setting to 'Use HOST GPU' and to set the ABI to 'x86' (to take advantage of the hardware acceleration).

With these changes, the emulator has been running really well.

Conclusion

If you are like me, and you have previously given up on the Android emulator, maybe now is a good time to give it a try again.

References

Changes in Android 5.0

Intel hardware accelerated execution manager

Sunday, November 9, 2014

MarkdownBlogger

A New Project

I have been working on a small utility tool that would make blogging easier for me. It's called MarkdownBlogger. In short, it converts markdown text files into html, and then posts those html files to blogger.

Why I have liked working on this project

I have enjoyed this project for a few reasons. First, it's something that I would actually use. Since I spend most of my time building things for other people, it's pretty satisfying using something that I built myself. Also, since I wrote this, I can easily modify it to suit my needs in the future.

I also like this project because I have a well-defined goal in my mind. I want it to do a few things and not much more. With this definite end-goal in mind, I see the finish line in the near future, and I can know when I am done. I think having too many projects in the work-in-progress state can leave a nagging dark cloud over your head.

Now what

Well, I'll keep this blog post short and sweet, just like MarkdownBlogger is meant to be.