Coderetreat

Published on:
Tags: community, ruby

A few weekends ago I attended Coderetreat in Atlanta. It was a great time. I had never heard of it before @srbiv told me about it a few months ago. Coderetreat is a day of development focused on learning. You do so through pair programming and TDD. The task is Conway’s Game of Life. We did 6 45-minute sessions, each one with a different restriction on how we developed. After each session the code is thrown away and the entire group meets to discuss their experience from the last session.

The goal of the day is practice writing clean code. Usually in our daily work we are trying to meet deadlines or otherwise don’t have the time to do things as well as we would like. We accomplish the task that we set out to do but then never go back to improve it. We discussed this before starting and some basic clean coding principles, based on Four Simple Design Rules.

Based on that description I expected the attendees to be mostly developers with many years of experience. Instead I found a lot of developers who had only been at the craft for a couple of years or less! Everyone was really supportive and excited to get a chance to learn from others and grow as developers. Everyone had something to learn and something to teach; it was an amazing thing to be a part of.

I had the opportunity guide some newer developers in the ways of TDD while also learning different methods of design from others. I had many great discussions throughout the day, especially about JavaScript and Vim.

My favorite challenge was where my pair and I alternatingly wrote tests for eachother but could not speak about design. Trying to move the system forward with only test code to guide us really made me rethink what kinds of information, both intended and not, that tests are capable of containing.

I can’t wait until the next Coderetreat, I definitely plan to continue attending.

Falling in Love With Vim All Over Again

Published on:

About about 2 years ago, I stopped using vim. I was sad, but didn’t think too much of it. I had just moved on from a job where I used vim full time. Unfortunately, it was in a language known as PowerBASIC. This language has very low adoption and there was no community tooling available. I had to handroll my own syntax and indentation files (I was able to steal from the BASIC files). I had no tags support and never felt like I really got to hit my stride despite using vim daily for over 3 years.

Starting in late 2011 I’ve been working primarily in Visual Studio. For the most part it is a great IDE with a lot of powerful features. Combined with the refactoring and productivity shortcuts available in CodeRush things still felt pretty nice. I tried using some vim keybinding addons but they were never as responsive as is necessary to work the vim way. In addition to no longer using vim at work my amount of personal development work had sharply declined. What little I did was done with Visual Studio. I started to forget how nice working in vim really was. Maybe it wasn’t really necessary, I thought. Especially not with tooling this powerful.

Then something changed. I decided to make a committed effort to learning web development. Instead of taking the obvious route of using ASP.NET I decided to go a completely different direction and use Ruby on Rails. I’m writing tons of code on in my free time and doing it all in vim. I’ve never been happer! The flexibility of Ruby is beautiful and expressive. And Rails, while sometimes confusing and magical, makes a lot of sense and helps you get things done. I’m working a ton in vim and loving it.

After being “lost” for so long vim has reminded me what a powerful really development environment is.

Power is the inverse of friction between creativity and expression

Big meaty IDEs lured me into thinking lots of features, integrated menus, and GUI tools were power. This is not to say that vim lacks features or power. It wants for neither.

Nothing else I have used has come close to vim. It has power that I have never had to pay for with my effectiveness or ability to express myself. Every time the big feature-rich IDE churns for a quarter second as it parses background files or does whatever the heck it thinks is more important than me typing I am jarred. Effectiveness drops. This aggregates over the day I find myself frustrated, distracted, and ineffective. Vim doesn’t do this to me.

Vim. I’ve missed you.

Ruby, Rails, and Friends

Published on:
Tags: atlanta, rails, ruby

For the last 6 months I’ve been slowly teaching myself Ruby on Rails. It’s been a lot of fun and I’ve loved most every minute of it. My last run at web development was several years ago before technologies like Heroku were common. I spent some time trying to learn Django but did not find the ecosystem quite as mature as I would have liked. I found myself stuck playing sysadmin on a webserver, which is a role I am neither interested in or great at. So I hung up web development, experimented with mobile went back to desktop application development.

Four years later, enter Ruby on Rails. I’m having a blast. The tools are great. The community is wonderful. And after spending every day for two years with my nose buried in Visual Studio I am thrilled to be back to vim. It is bliss.

I’m active on GitHub and attend several regular meetings and extra events with the Ruby community. I’ve made tons of new friends and enjoying all of it. I’ve hardly played a video game in months and I can hardly believe it. I’ve got motivation to work a full day and then another three or four every night. It’s all fun.

If you feel like you’re missing something in your life as a developer try to find a local group. It has made all of the difference.

RPS Contest

Published on:
Tags: python

The other week I came across a neat programming challenge called The RPS(Rock Paper Scissors) Programming Competition. The goal is to write a Python program that plays RPS against other programs. Each match is a best of 1,000.

This challenge caught my interest with its unique combination of ease and depth. While my best submission has dropped to a roughly 60% win rate there are some truely clever programs submitted. I am certain I can improve greatly and develop a submission over the coming weeks that is able to achieve a 70% win rate.

I would encourage anyone who enjoys programming or problem solving to give it a go.

Why I Love Android Development

Published on:
Tags: android

Android development strikes a sweet spot for me, as a developer seeking to be a contributing member in the software community.

I have taken a few shots at web development and it just doesn’t feel right. While it is such a visual and rewarding experience. I just can’t shake the feeling of it being the most phenomenal kludge.

It is easy to distribute and consume in an almost completely platform independent environment. I love being able to send a someone a link and say, “Hey, check this out!” and have them be greeted by a slick web app.

But despite all of this it still feels wrong.

Android offers you a much more consistent development platform while simultaneously keeping much of the ease of distribution alive.

I like how Android development feels like a merger between web accessibility and desktop consistency.

I do realize that Android has its own host of complexities such as the numerous possible devices, API versions, and the challenges of the application lifecycle.

It won’t say that it is perfect, but I am comfortable saying that it feels very right.

Android Soft Keyboard for Dialogs

Published on:
Tags: android, java

It seems like such a simple thing, you would like to show the Soft Keyboard automatically for a Dialog. You can easily find a number of ways to do this for an Activity. You can also find one often repeated, but inadequate, method being proliferated for a Dialog

1
2
3
InputMethodManager imm =
(InputMethodManager) getSystemServer(INPUT_METHOD_SERVICE);
imm.toggleSoftInput(imm.SHOW_FORCED, imm.HIDE_NOT_ALWAYS);

There are, however, several problems with this method. It does not…

  • respect the user’s phone and presence of a physical keyboard
  • close automatically when the dialog closes
  • close automatically if the user were to leave the activity or application completely

There is however, a solution if you are targeting at least API Level 3 (Android 1.5). In your onPrepareDialog(), or wherever you are displaying your dialog do the following:

1
2
dialog.getWindow()
   .setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

This will have the same result on a Dialog as setting android:windowSoftInputMode="stateVisible" for an activity in your AndroidManifest.xml

Custom Android RadioButton

Published on:
Tags: android

I was browsing StackOverflow and came across a question about making custom RadioButtons and figured I could answer so I decided to take the plunge.

To implement a custom styled RadioButton you can follow the below steps or look at the complete example source here.

Create the drawable resources for your custom RadioButton. You should have at least 4 icons:

Pressed Checked
True True
True False
False True
False False

Place your images in res/drawable or if if you have a version for each screen density place them in their corresponding res/drawable-ldpi, res/drawable-mdpi, and res/drawable-hdpi folders.

Then create a selector type XML file in res/drawable. Here is my res/drawable/button_radio.xml

1
2
3
4
5
6
7
8
9
10
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_checked="true" android:state_pressed="false"
      android:drawable="@drawable/radio_on"/>
  <item android:state_checked="false" android:state_pressed="false"
      android:drawable="@drawable/radio_off"/>
  <item android:state_checked="true" android:state_pressed="true"
      android:drawable="@drawable/radio_on_pressed"/>
  <item android:state_checked="false" android:state_pressed="true"
      android:drawable="@drawable/radio_off_pressed"/>
</selector>

Setup your RadioGroup like so:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<RadioGroup android:layout_width="fill_parent"
   android:layout_height="50dp"
   android:orientation="horizontal"
   android:checkedButton="@+id/first">
   <RadioButton android:id="@+id/first"
      android:width="50dp"
      android:height="50dp"
      android:button="@drawable/button_radio"/>
   <RadioButton android:id="@+id/second"
      android:width="50dp"
      android:height="50dp"
      android:button="@drawable/button_radio"/>
   <RadioButton android:id="@+id/third"
      android:width="50dp"
      android:height="50dp"
      android:button="@drawable/button_radio"/>
   <RadioButton android:id="@+id/fourth"
      android:width="50dp"
      android:height="50dp"
      android:button="@drawable/button_radio"/>
</RadioGroup>

I have specified dimension of 50dp as the dimension of my drawables are 50px x 50px. Also notice I am setting android:button and not android:background.

Hopefully this can serve as a nice starting point for anyone looking to create some new and interesting effects with simple RadioButtons.

Android TextView With Custom Link Text

Published on:
Tags: android

This is just going to be a quick how-to on creating a TextView with custom URL text.

To do this, you need to create a Spannable text object by using Html.fromHtml and then setting the MovementMethod of the TextView to a LinkMovementMethod. Here is an example using a dialog.

1
2
3
4
5
6
7
8
9
10
11
12
dialog = new Dialog(this);dialog.setContentView(R.layout.about);
dialog.setTitle(R.string.about_score_it);
TextView website = (TextView)dialog.findViewById(R.id.about_website);

String realURL = "http://blog.devminded.com/projects/score-it";
String visibleURL = "http://blog.devminded.com";

//The following builds the spannable item and will cause the text to display as a link
website.setText(Html.fromHtml("<a href=\" + realURL + "\">" + visibleURL + "</a>"));

//The following makes it clickable
website.setMovementMethod(LinkMovementMethod.getInstance());

Or with resources:

1
2
3
4
5
6
7
8
dialog = new Dialog(this);
dialog.setContentView(R.layout.about);
dialog.setTitle(R.string.about_score_it);
TextView website = (TextView)dialog.findViewById(R.id.about_website);

website.setText(Html.fromHtml("<a href=\"" + mRes.getString(R.string.about_score_it_website_url) + "\">" + mRes.getString(R.string.about_score_it_website_text) + "</a>"));

website.setMovementMethod(LinkMovementMethod.getInstance());

To see the full context of this example please check out ScoreIt.java

Android SQLite Database Management

Published on:
Tags: android

Just as I was trying to wrap up Score It! I noticed my Logcat window was getting flooded with error messages. Somehow during all my debugging and development I didn’t notice the pages of errors being thrown by SQLite.

java.lang.IllegalStateException ... SQLiteDatabase created and never closed

Now these errors didn’t appear to have any adverse affects on my application but I certainly did not want to release an app that I knew was spewing errors. I wrestled with this for an evening searching and trying everything I could think of.

Some people suggested opening and closing the database around all accesses. Others suggested littering all of the lifecycle overrides with open and close statements. I found the former unreasonable and the latter inadequate. I needed to keep my database connections open as much as possible because I was working with ListView and ListActivity objects.

I managed to reduce the frequency of the errors but it was still happening consistently if I followed specific steps but I decided to throw in the towel for the time being.

The next day I decided to revisit the issue and make sure I had a better understanding of the problem. I stepped through my app, using a ridiculous number of breakpoints. It soon became clear that it was not a specific line of code that was causing the problem. This error was being generated by the database object when the garbage collector was hitting it.

I experimented a bit further and came up with a solution that seemed to eliminate all of my SQLite database errors.

In the Activities that maintained a database connection I put a close() statement in the Activity.OnDestroy() override:

1
2
3
4
5
 @Override
  protected void onDestroy() {
      super.onDestroy();
      mDbHelper.close();
  }

In my database helper class, modeled after Notepad, I changed the open function from:

1
2
3
4
5
6
    public ScoreDbAdapter open() throws SQLException {
      mDbHelper = new DatabaseHelper(mCtx);
      mDb = mDbHelper.getWritableDatabase();

        return this;
    }

To the following:

1
2
3
4
5
6
7
8
    public ScoreDbAdapter open() throws SQLException {
      if (mDbHelper == null) {
          mDbHelper = new DatabaseHelper(mCtx);
      }
      mDb = mDbHelper.getWritableDatabase();

        return this;
    }

This cleared up all the errors.

Copyright © 2013 Thomas Holmes