How to unit test an onFocusChangeListener … don’t.

I am fairly new to unit testing in practice. I’ve read about it over the last couple of years and even given a talk on different frameworks for python, but I’ve never really dug into it enough to use it in my daily practice.  At work we are starting a new mobile project and I decided to pull some of my reusable code out to a library project. While doing this, I thought it would be a good idea to start putting everything under unit test. In just a few short days I have learned a tremendous amount and found a couple of bugs that could have come back to bite me In the future.

The title of this post is “How to unit test an onFocusChangeListener … don’t”. What do I mean by that? I have a custom Android view which extends EditText to format a phone number based on a certain mask. I wrote a test which should have fired the onFocusChangeListener. I’m using Robolectric to do my testing and couldn’t get the listener to fire. I looked online and found a great comment from one of the Roboletric devs which say you shouldn’t be testing the listener. As a developer writing unit test, you aren’t testing that the platform works. You are testing that YOUR code works. I realized that I should be testing the method call to the formatPhoneNumber() method, not trying to trigger the actual listener. One problem I had was the formatPhoneNumber() method was private. So how do get to that method to test it?  The answer is reflection. *Note: creation of the Method and PhoneNumberEdit are repeated due to needing to reset the shared preferences before the PhoneNumberEdit is created. Other test in the test class use the default objects created in setUp(). I wanted to show how the activity is build with Robolectric.

The signature for my formatPhoneNumber() method:

private String formatPhoneNumber(String mString) {…}

Using reflection, you can invoke the method:

PhoneNumberEdit numberEdit;

Activity activity;

Method formatMethod;

@Before

public void setUp() {

    activity = Robolectric.buildActivity(Activity.class).create().get();

    numberEdit = new PhoneNumberEdit(activity);

    try {

        formatMethod = numberEdit.getClass().getDeclaredMethod("formatPhoneNumber”);

        formatMethod.setAccessible(true);

    }catch(Exception ex) {

        ex.printStackTrace();
    }

}

@Test

public void testNumberFormattedWithCustomMask() {

    String number = "2055551212";

    SharedPreferences prefs = ShadowPreferenceManager.getDefaultSharedPreferences(activity);

    // reset numberEdit so we have correct prefs.
    prefs.edit().putString("phoneFormat", "555-555-5555").commit();

    numberEdit = new PhoneNumberEdit(activity);

    try {

        formatMethod = numberEdit.getClass().getDeclaredMethod("formatPhoneNumber");
        formatMethod.setAccessible(true);
    }catch(Exception ex) {
        ex.printStackTrace();
    }

    numberEdit.setText(number);

    try {
        formatMethod.invoke(numberEdit);
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    assertEquals("205-555-1212", numberEdit.getText().toString());
}

The lesson learned is when unit testing, be sure your testing is focused and test only the code you care about. You shouldn’t be testing the framework/environment unless that is the code you wrote. Just remember this: Small, focused test and you should be off to a great start.

Practicing deliberately

I just finished re-reading The Passionate Programmer, by Chad Fowler. I posted about this book, the first time I read it back in September of 2009. One of the topics I focused on this time was the idea of practicing your art.

Programming is a lot like painting and music. While there is arguably more science to programming, there is a strong art component to it as well. You can look at two pieces of code that do the same thing, but chances are you will better appreciate the one that is more aesthetically pleasing, easier to understand, and as a result easier to maintain. Writing maintainable code takes experience and structure. Often times, when you look at a piece of ugly code, it was ugly because the developer was learning something new. Essentially, they were practicing on the job. Like in music, practice isn’t meant to always be beautiful, but a performance is. As developers, we should look at the time we spend at work as performance, not practice. We get paid to perform, as scientist and artist. Practicing is something great developers do on their own time. This may be learning a new language to broaden our understanding of the technologies available (adding more tools to our toolbox) or learning a new framework or portion of the language we already work in. Maybe it means learning how to better automate our workflow and builds, so we don’t spend so much time doing the same task over and over. Whatever it is we want to learn, thinking of ourselves as artist and practicing our skills away from the day job is critical to developing as a great programmer.

In keeping with the idea of programming as art, thinking about practicing as an art can help us to better apply learning to technology. As a musician, sitting down behind your instrument with no plan is a waste of time. You end up playing a few scales or some beats on the kit, maybe playing along with your favorite song, and walk away at the end of your session no better than you were when you sat down. While your skills didn’t atrophy, you didn’t really learn anything either. It wasn’t time well spent. I’m certainly guilty of this both behind the keyboard and behind the drums. It is said that it takes about 10 years and/or 10,000 hours of deliberate practice to become an expert at something. So how do you practice deliberately?

* Determine your goals. Maybe it is reading a specific book, or working on a learning a new framework. Set your goals before you sit down at the computer. Determine the minimum amount of time you want to spend practicing and don’t let yourself get sidetracked until you have at least that amount of time in. Not setting goals is a sure way of walking away from the keyboard with nothing accomplished.

* Realize that deliberate practice is often slow and error prone.  While learning something new, give yourself the freedom to make mistakes and go slow. Don’t move ahead if you don’t understand what you working on and why the code you are writing works. This is really common when working from a programming book. It is too easy to copy and paste code samples and push through the material too quickly. If you don’t understand what you are working on, how do you expect to be able to efficiently use it in the future.

*Stay focused. Don’t turn on the TV. If you can, avoid interruptions like children and email. Leave your phone in another room so you aren’t tempted to check the latest Facebook post.

* Write down your achievements and insights. You learn by doing and in the process often have insights that can help you in your job. Write them down. Don’t trust yourself to remember everything the next day, especially since it will be unlikely you will have the chance to implement your ideas right away.

* Look for ways to continue practicing the skills you learned. If learning a technology that is different than you use at work, find a small program you want to write for yourself, whether you will use that program or not. The act of doing is far more educational than the act of observing (reading for instance). If you really enjoy the technology you are learning, look for an open source project you can contribute to. There has never been a time when it was easier to find and participate in new technology. This is also a great way to find a person or group who can help mentor you. Don’t have a project but you want to try solving specific problems with the language you are learning? Check out Project Euler  or the Ubuntu Programming Forums for specific challenges. There are many sites like this that can give you ideas of short programs you can write to depend your understanding of the language.

Sources: 

* How many hours a day should you practice?

* The Passionate Programmer

 

 

 

 

 

It’s all Groovy man…

I have 3 weeks before my first Android class starts with Udacity. Considering I have been an Android developer for several years, I expect it should be pretty easy for the first month or so. In the meantime I am going to spend my extra hours working through Groovy in Action, from Manning.

I previously mentioned that I attended the Android Developers Conference. One of the main things I took away from the conference was the shift in technologies from Eclipse/Ant to Android Studio/Gradle. Gradle has some powerful features which will allow me to more easily accomplish multi-project builds. One of the new items I am working on at my day job requires the ability to have a core product, with “flavors” built for specific customers. With Gradle, the setup and building of these should be fairly trivial. It will require some special code in the build files, especially working with version control, but I believe spending the time to learn Groovy will make the process much smoother.

Groovy has grown to be a popular JVM language. While some languages such as Scala have eclipsed Groovy’s popularity, a Java developer can’t go wrong by spending the time to learn it. If you are an Android developer and see yourself moving to Android Studio, you should start now.

On an unrelated note, I also finished reading RESTful Java with JAX-RS 2.0, 2nd Edition. I plan to release a review of that book within the next few days. *** Spoiler alert: It was good and would provide a nice reference if you work with the JAX-RS libraries.

Introduction to the Design of Everyday Things

While searching the Udacity.com site for classes that looked interesting, I ran across one on design. It is called the Introduction to Design of Everyday Things. It is taught by the guy who wrote the definite book on the subject, Don Norman.  I found this to be a really interesting class. It wasn’t specifically about the subject of software, but rather how humans interact with objects all around them and how good design considers both the affordances the human interaction allows and the signifiers used to make things more clear. The final project in the class was to design the interface for a time bank application. This application was to allow you to trade “hours” rather than money and have people perform work for you and you to perform work for others. As you perform work for someone else, you gain hours and as others do work for you, you send those hours to them, so they can then use them for something they need done. It was a really cool concept and something that was pretty fun to try to build a UI design for.

The course got me thinking about the way we put software together. It is really critical when designing software, especially for the mobile space, we think about how intuitive our interface is. As developers, we often don’t seem to take as much stock into our UI, rather we get wrapped up in how many features an application has or how technically cool it is. Without an intuitive design, you may be the only person using the awesome app you just spent the last 3 months working on. Think of all of the programs you have downloaded or the apps you have installed on your phone and how many you quickly uninstalled when you became frustrated at not being able to determine how the thing works. This is the same pain every user feels. It is a shame when it is your app they feel that way about. So as a developer, take the time to speak with other people about your interface. Try not to tell them how it works, but rather ASK THEM how they think it works. Take that feedback and consider a redesign to make things more intuitive. The time you spend may be the difference in a really cool app that no one ever uses or the next big thing to become famous.

Finally, if you are interested in the subject, I highly recommend this class. It is really short.  I was able to complete the entire course in just a couple of days. It truly was just an introduction (covers the first few chapters of the book) and I have added the book to my wish list of books to read this year.

Continuing education

Continuing education for developers is a must. In this industry, you either continue to make yourself better or you get stagnant and wait for the time when all the upcoming kids take your position and you find yourself worried about whether you will still have your job in the future or whether you will be able to find another one if something were to happen. Today is different than any other time in history. The internet and online education makes it possible for anyone to take classes from home, from some of the best schools, for free.  When I started learning programming, the only choices were to read books and the occasional blog.  You could get great information, but it wasn’t structured in a way that really got you involved. Todays online classes are amazing, interactive and force you to be actively involved as you learn. I have a few favorite sites, but if you know of more, please post them in a comment!

I’m going to be starting this new year, early, with taking a class about designing every day things. The class is based on the first few chapters of a book by Donald Norman.  Design is hugely important in software development. Oftentimes, as developers, it is easy to get in the mindset of how an application works technically, being efficient, without thought to how the every day user will interact with your program. If your app isn’t usable to the average user, they won’t use it. All of your hard work will be useful only to you, or a select few. This is fine if you are building something to practice a new technology, but not if you want people to use what you have built.

So here we go. I’ll post about the class when I finish and possibly along the way as I learn things I feel I need to share right away. By posting here, I’ll hold myself accountable for finishing the class and absorbing the material.

Great guide to lifelong employment in technology

I don’t want to repeat everything here, so here’s the link. And for the main site.

So what now?

About me. Two simple words that are so open as to make me pause when trying to determine what to write on this page.  My old about me can be found here. So much has changed since I first started this site. I don’t want to lose track of were I started and what my motivations where at the time. So who am I today?

I’m a programmer, husband, brewer, and general technophile in my mid 30’s. For some reason, I can’t shake the burning desire to be better at everything I do, especially when it comes to technology. I’m fascinated by every new gadget, programming language, and project I see. At times, it can be so overwhelming that I lose focus. That’s what this year (2014) is about. Focus. You see, I look around and I am surrounded by people I consider great programmers. People whose knowledge dwarfs mine. Don’t get me wrong, I consider myself a pretty decent programmer. I’m fast, I work really well with others, and I understand what it is like to be a customer, a support rep, a trainer, a designer and a programmer. I’ve worked almost every point in the software lifecycle. I think this gives me a unique perspective that not many programmers have had the opportunity to explore. It was a long road to get to where I am, but I believe that has made me that much stronger. The problem? I don’t want to be a pretty decent programmer.  I want to be a GREAT programmer.

I mentioned that it has been a long road to get me where I am today. I’ve put in countless hours typing away on new programs, learning new languages (however quickly forgotten they are without constant use) and expanding my view on technology. When I think about what I am really passionate about, it is trying to help others to have an easier go at it than I did. The greatest thing we can have in technology is more ideas and to accomplish that, we need more people willing to move past being a user and becoming creators. President Barack Obama recently said, “I think given how pervasive computers and the internet is now, and how fascinated kids are with it I want to make sure they know how to actually produce stuff using computers, and not simply consume stuff.”  The world is different than when I began learning technology. Now, anyone can learn, as long as they have the willpower and the desire to put in the effort that is required. My goal over the next year is to take what I have learned and use it to continue to make myself great. I want to use this knowledge to write a book that will help others that not only want to get started in programming, find their first job doing it, but then also use that knowledge and the habits they developed to make themselves, and everyone around them better.

So, follow along with me on this journey. I’m not entirely sure where it will take me and exactly what steps I will follow. I’m going to start by continuing my education with some online classes on Android. I’ve been a mobile applications developer for the last several years, mostly focused on Android and server side Java. I recently attended AnDevCon and found that I still have a lot to learn. I’m going to be taking a couple of classes on coursera.org (1, 2) to learn even more. I am especially excited about one that deals with network operations as it pertains to mobile apps. At the same time, I intend to find a good open source project to work on and put in some extra time. Will it be an Android project? Maybe, but part of me thinks it might be a good idea to find something outside of my comfort zone. Something that will push me to my limits and force me to expand my horizons.

My old about me page

Below is what consisted of my about me page in 2009/2010.  Almost 5 years have gone by and many things have changed. I’ve reached my goals, but found new ones to propel me forward.

Motivations:

I’m starting this blog as a way to motivate me to do more to gain useful experience and hopefully help others along the way. Those of us who consider ourselves Jr Developers live in a strange world, dominated by the dreaded Catch 22.

Dictionary.com defines Catch 22 as “any illogical or paradoxical problem or situation; dilemma.”

In the software industry, this usually means that to move forward you need experience, but to get the experience, you need to move forward. You can see that puts those of us trying to learn and get experience in an extremely difficult position!

I have a plan. First, I will be starting with a subscription to Safari Books Online. I’ve been a subscriber for about a year and have found this to be invaluable to me for gaining knowledge without spending a fortune on books. Second, I’ll be finding and participating in Open Source projects to gain experience and make connections with experienced developers from whom I can gleam knowledge. I don’t know where this will lead, but hopefully this will result in me making connections and getting the experience needed to move my career forward.

Information about me:

I’m someone who came to software in their 20′s. I started my career in the military and with law enforcement. After a fairly serious on the job injury I decided a change of careers was necessary. I got into software by accident. A friend was working as a tech recruiter for a nationally known firm. He helped get me on board and I spent the next 10 months as the worst recruiter at the office. That’s right, I was terrible. What I did learn is that while I was bad at recruiting people, I was quite good at understanding the technologies for which I was recruiting. It make sense, I started on computers as a kid, programming with basic on a Commodore 64. This began a passion for technology that I now consider a large part of who I am.

I took my first job at a small company that made software for the propane industry. It’s a small market and the company did really well for itself. I worked in support and on the road as a trainer. I was there for 4 years and toward the end started learning a little Cobol. This got me really interested in programming, but I knew Cobol was not the future I wanted.

A co-worker had moved on to another company, a Java shop, and convinced me to come on board. I’m still at this company and have been for just over 4 years. I started in support and a little more than 2 years ago, moved into a Business Analyst role. Along the way, I taught myself Java and started doing odd custom programming changes and fixes. After a while, I started doing quite a bit and studied for , and passed, my SCJP 5 certification. At the time, it was my understanding that this was the last step I needed to accomplish before being allowed to move into a full time development position. Unfortunately, the current economic crisis began and layoffs started, which meant we lost developers. While I didn’t move forward, I was lucky to stay right where I was.

So, here I am. I’m a Business Analyst, Jr Developer, and knowledge nut trying to find my way toward a more full time career in development. Breaking free of the Catch 22 is going to be hard, but I hope to share my experiences with all of you, and maybe help you to break free too.

Another random update

Deanna and I have been stuck in the house, quarantined, with both the flu and walking pneumonia. I am beginning to feel stir crazy, but never the less it seems better than the risk of killing old people and small children with our plague. I decided I would take this opportunity to update on our lives as of late.

Work is going well. Anyone who reads my blog will notice that I haven’t spoken of work in a while. I’m doing what I enjoy and still look forward to going into the office each day.  I remind myself that not many people can truly say that and I am thankful for the opportunities I have seized. I certainly worked hard enough to make them happen.  I have slacked off a lot on my studying new technologies, but not entirely. You must keep learning in my industry, or you will get passed by quickly. The difference is that now that I have achieved the goals I had set for myself, I find myself with the time to focus on new goals and on my life outside of the office.

My exercise and MMA training is going very well. I wish I could make it to the gym more than I do, but my other hobbies also demand my time. Not to mention that as much as I would love to train like a 20 year old, I’m not 20 years old. I don’t recover as quickly as these kids do and I’ve found that when I push myself harder than I should, I start seeing small injuries that won’t go away.  Thankfully it has been warmer outside and I’ve found the time to get back outside and do some running after work. As much as I hated it as a kid, since basic training I’ve really enjoyed going for a nice run.  Last summer, I hit a 10 mile max distance. I would like to pass that this year and make the half-marathon distance I was going for last year.

Deanna and I found a beautiful plot of land. It was 38 acres in Springville, AL.  We both wanted to move there, but the price and the logistics of having to build a house, while still living in the one we have or the 800 square foot cabin on the property would be too much strain on our bank account. I remember what it was like to live pay check to pay check and neither of us wanted to put ourselves in that position again. We decided that rather than moving ourselves to the country, maybe there was a way to move the country to us. We have a large yard in a nice subdivision. With some work, we could make what we have amazing.

Finally, I’ve decided to stop thinking about it and make the time to start writing again. I did it quite a bit in high school and into college, but I haven’t made time for it in my adult life. I’ve started work on a novel, as terrible as I know it will be. Everyone’s first work is. I’ve also decided to focus on doing some short stories.  Much like my goal of getting my running distance up to 13.1 miles, I’ve also decided to make a goal for myself to get a short story published this year.

So there you have it, the last few months in a nutshell. My two goals for the year. I won’t call them resolutions as I would like to achieve them. When I look at them as goals, I know that I can.

Changing times

The road life takes us down can be both terrifying and beautiful at the same time. We don’t always know what the next curve will bring, and that is one of the things that makes life worth living. For me, seeing myself change over the years has been just like driving down a long road. The scenery has changed, my feelings about where I am and what I am doing has changed, and most of all, I’ve enjoyed every moment of the trip. Deanna and I are in the process of changing once again and we are really beginning to enjoy the scenery more than we did before. We are starting to appreciate actually living rather than coasting through life, what it means to actually eat, to breathe, and to survive.

People mistake growing up in the South as growing up in the country. While the South may have smaller cities (Atlanta excluded), it isn’t true that everyone from the South is “country-folk”. I grew up in the city. My parents were from the city. I didn’t learn to drive a tractor, I didn’t kill my own food. Heck, I don’t even remember my parents having a garden that was edible. Food came from the grocery store. I didn’t know the source of the food, and in the 80′s and 90′s, no one really cared. I don’t think we understood the implications of the industrially produced food that most of America now consumes. Things have certainly changed in recent years, both in my perspective and in the nation as a whole. People are joining the slow food movement, the local food movement, the organic food movement, the insert-whatever-here movement. It seems there is a movement for pretty much everything. What I find the most interesting, is that we are all really just trying to return to what eating was like for our grandparents. A time before High Fructose Corn Syrup was used to sweeten everything we eat. A time before half the ingredients on a label were unpronounceable. A time when you knew that what you put in your mouth was grown by a farmer, not a chemist.

I don’t know when the shift began for me. Maybe it is with my incredible in-laws that have shown us more about gardening and working with our hands than anyone else in my life. Maybe it is my friend Rich, who showed me just how easy and awesome it is to brew your own beer and make your own wine. Most likely, it isn’t just one thing, or one person, but the perfect storm of events that are transforming my life. It is more than just about the source of my food, but also my ability to create the foods I eat. From making my own ketchup, to stuffing my own sausage, and roasting my own granola for Yogurt. I have slowly started doing more things for myself that I would have previously just assumed needed to be bought from a store. Not only has the quality of homemade food surpassed my expectations, but my appreciation for them grows even more so. I now brew my own beer, make my own wine, make my own cheese, and try to grow my own food where I can.

Twelve years ago, when we moved to Birmingham initially, I never imagined we would be here long. Deanna always wanted to move to the city. To be in the mix of the action and noise. I just wanted to be away from the South. Things have definitely changed. We now seem to dream of a morning where we can wake up, go outside with a cup of coffee, and overlook our garden and our animals.

To this end, Deanna and I have started looking for some land on which we can build a farmstead. We have four chickens now, but we really want more. I would love some ducks. I especially want some dairy goats. We want a large garden area where we can grow the majority of the food we eat. We want to learn to be self sustaining, to provide for ourselves and potentially our families. To get back to the land.

How times change.

Previous Entries