Kip

Some thoughts on programming style

Written by Kip on Tuesday, February 3, 2009 at 2:02 pm (EST)
Tagged as:

Time to discuss something controversial that leads many geeks to commit acts of heinous violence.  I’m talking about programming style!

So there are three basic ways to write if/elseif/else statements in most languages with C-like syntax:

Style A

1
2
3
4
5
6
if(condition1)
  statement1;
else if (condition2)
  statement2;
else
  statement3;

Style B

1
2
3
4
5
6
7
if(condition1) {
  statement1;
} else if (condition2) {
  statement2;
} else {
  statement3;
}

Style C

1
2
3
4
5
6
7
8
9
10
11
12
if(condition1)
{
  statement1;
}
else if (condition2)
{
  statement2;
}
else
{
  statement3;
}

Personally, I go for the most readable and maintainable code, so I use Style A iff all conditions and statements are trivial (by which I mean, they are short and fit on one line).  Otherwise I use Style C.  (Which you can see, for example, in that gradient-generator source I posted a few weeks ago.)

I’ve posted on my preference for Style C over Style B before, and I’m not going to focus too much on that here today.  However, I continue to see Style B promoted as the universal, be-all end-all solution.  It is recommended by Sun for Java, and by Zend for PHP.

The argument for preferring Style B over Style A usually goes something like this:

What if you come along and add a new line?

1
2
3
4
5
if(condition1)
  statement1;
else
  System.out.println("Condition1 failed!");
  statement2;

Now statement2 gets executed everytime!

The argument makes sense from an academic standpoint, but I am pretty sure this almost never ever happens in practice.  The reason for this is that anyone who has been programming for more than a month will immediately see that this code won’t work as designed.  It is a glaring bug that jumps out at you.  It is nearly impossible to overlook!  (Again, this is all assuming that the conditions and statements are all trivial.)

Now, for the last four and a half years my job has been primarily to fix bugs in a huge body of code, very little of which was written by me.  For the last two and a half years, in particular, I’ve been the guy who looks at build traces and unit test results from the previous night.  And when there are build or test errors, I have to look at recently changed code and decide who is responsible.  This means I have seen most of the errors made by a group of about fifty or so programmers.  That is to say, real-world errors made by real programmers, not hypothetical errors that might be made by a theoretical programmer.  So I think I am reasonably well-qualified to have a strong opinion on the matter.

With that in mind, I’ve never seen an error that was due to the use of Style A for trivial statements.  I have seen some downright ugly code that used Style A inappropriately, with conditions that were ten lines long.  And I’ve seen ugly code that has braces on the if block but not the else block (or vice-versa).  And I have seen several errors in Style B or Style C that result from intermingling of tabs and spaces.  This happens because simple editors like vi and Notepad render tabs that are up to 8 characters wide (per spec, I might add), but advanced editors usually break spec in favor of user-friendliness, rendering tabs at 2 or 4 characters wide (or whatever the user sets them to).  So if code was written using 4-character tabs in Visual Studio, then new code is added by someone using spaces in vi, and this happens back and forth a few times, you get indentation that jumps all over the place.  This is where I think Style C is better than Style B, because it is easier to find the matching open-brace because usually the opening and closing braces are written by the same developer with the same indentation level, even if the code between the braces jumps all around.  Also, with Style B, at a glance the code looks like indentation is wacky, since you have to actually read the previous line (or scan to the right end of the previous line) to find out if the developer actually intended the indentation to increase there, or if someone just increased indentation because they were using a different editor.

You can tell me why you disagree with me, and I am fully aware that there will never be agreement on this point; however, I’ll continue to avoid Style B until my pay is docked for it.  Someone has to stand up for what is right.

Kip

Biohazard 5

Written by Kip on Friday, February 6, 2009 at 9:35 am (EST)
Tagged as:

Resident Evil 5I just played the Resident Evil 5 demo last night, and it seems to be very similar to Resident Evil 4, which is not a bad thing.  From everything I’ve read, though, you really need to play it in co-op to get the proper experience.  An AI partner just wouldn’t be the same.

So, does anyone out there have a PS3 and interest in playing Resident Evil 5 in co-op mode with me?

No Comments
Stephanie

Emma continues to grow

Written by Stephanie on Thursday, February 12, 2009 at 8:46 pm (EST)
Tagged as:

Emma is just growing and growing!  I guess that is what babies do.  To think that in just over a month she will be one year old.  My little baby won’t be a baby much longer!  I just thought that I would take a moment to update everyone on all the amazing things Emma can do now.

Emma grows

Watching Little Einsteins
Almost everyday since Emma was around four or five months, we would watch Little Einsteins on the Disney channel.  At the beginning of every episode, the characters on the show pat their laps to help Rocket blast off.  Today was the first time that Emma patted along with them!!  It was so exciting to see her patting along and then saying a word after they said “Blast off”!  She’s been clapping at the end of the episodes during the curtain call for about a month, but it was really exciting to see her patting with the team.

Sign Language
Kip and I have been trying to teach Emma how to communicate with us using sign language.  She is really good at “good job” which is clapping her hands.  But lately she has also picked up “all done” when she is finished eating and every now and then she’ll sign “please” when she wants you to pick her up.  (More often though she raises her arms and looks up at you when she wants to be picked up.)  She also will wave “hi” sometimes when you say hi to her.  She waves to herself for “hi” and sometimes away from herself for “bye”.  Kip also taught her how to give “high five”.  I mean, who doesn’t want a ten month old giving them five.  Next we’re going to work on “pound it” (the fist bump).

Story Time
Every night Kip reads Emma her bed time stories, and he always ends with “Good Night Moon”.  When he goes through the story and the picture is the full room view, he always points at the things that the bunny is saying good night to.  Emma has picked up that habit as well.  She always points to the red balloon first and the picture of the cow jumping over the moon second.  Then she will point at random things in the room, but it is still really cute.

Fuzz on the Floor
Emma is a pro at finding any and everything that is on the floor.  I can vacuum the living room, and within five minutes of her being back in the room she can find a single piece of fuzz that might have been missed by the vacuum.  She used to immediately stick the fuzz in her mouth, but I have been trying to teach her to bring it to me instead.  This way she learns to pick up and bring things to mommy.  It is very helpful.  We do still have to have the occational mouth check to make sure she isn’t chewing on something she should not be chewing on.

Walking?
Emma has mastered crawling and is well on her way to understanding the mechanics of walking upright.  She has actually already learned how to climb the stairs.  I found her up three steps one day when I was headed back downstairs from taking something up to her room!  Of course, we’re making sure she doesn’t get near the stairs again, they’re just too dangerous for her right now.  She can walk around all the furniture in the house and pull herself up on just about anything, including the smooth walls.  She loves to play “chase me around the house”.  We’re trying to reset her boundaries.  She likes to hold onto your hands and walk around the house.  That lets her have more freedom than being confined to the furniture and the walls, but she still isn’t motivated enough to try taking a few steps on her own.  I’m not sure if I’m ready for my baby to walk away from me yet.

Shutting Doors
Emma has also learned that the doors are a lot of fun to play with.  You can bang on them, swing them back and forth, and push them closed.  She especially likes to shut the doors on you while you are doing something in the room.  Then she realizes that she can’t play with the door anymore and gets upset.  It is really funny when she crawls around the room and goes to the laundry room door and bangs on it while saying, “dadadada”.

Emma standing

So those are the really big accomplishments we’ve made in the last few months.  I’m sure we’ll let everybody know how her first birthday goes.  Be sure and check out the pictures (there aren’t new ones, but the old ones are still super cute)!

No Comments
Kip

My adventures south of the border

Written by Kip on Thursday, February 26, 2009 at 3:30 pm (EST)
Tagged as:

I spent last week in Matamoros, Mexico, which is a city across the Río Grande from Brownsville, Texas.  Being in Mexico, particularly near the border, gave some cause to be concerned.  But we made it through mostly without incident (more on that later).  I went with nine other people from my church to visit and help out a missionary we support there.  Jack (the missionary) runs a camp in Matamoros (seen here from outer space) which in the past has been used as a camp for kids and teens.  But he has more recently built some nicer cabins on the camp, and last week they were finally put to use during a retreat for four Mexican pastors and their wives.  Part of the reason we came was to help out with this retreat, with preparing and serving food.  The rest of us (myself included) were just there for physical labor.  But rather than write a super long post that will bore anyone who wasn’t there, I’ll try to condense a few bullet points.

  • Texas is big, flat, and empty.  At least the parts that we drove through.

  • The first three days, most of our group was working at Jack’s house, which was located in Texas.  This means I crossed the border quite a lot—eight times in all (four times each way).  I learned that:

    • The Río Grande is quite a misnomer.  The river is actually rather small.  And we were pretty close to the Gulf Coast, where you’d think the river would be at its largest.

    • Getting into Mexico is much faster and easier than getting into the United States.

    • Only once were we asked to get out of our vehicle while a guard took a look around, and looked in the glove compartment, but didn’t look in the trunk.

  • Refried beans are apparently a condiment in Mexico.  At one meal, we had ham sandwiches with refried beans instead of mustard or mayonnaise.  At another, there was toast with refried beans and cheese on it.  I think the best local food I had was the breakfast burritos, which were actually not spicy (in fact, none of the food was very spicy).  I also made crepes one morning, by putting jelly on a tortilla and rolling it up.  Those were pretty good too.

  • Our accommodations were not exactly top-of-the-line.  We had to shower in smelly water that was cold not clean enough to drink (even the locals don’t drink it), and we slept in cabins that were open and allowed plenty of mosquitoes in.

  • On our next-to-last day, we heard from the pastors who were at the retreat.  It was humbling to think that these accommodations that we found so primitive were like the Hilton to these couples.  I think they all still have jobs in addition to being pastors, because their churches simply can’t afford to pay them enough to live on.

  • I was surprised how well I could actually stumble through Spanish, having only taken it in high school about ten years ago.  But I don’t know how well I could have communicated without the help of Josh, who is currently taking Spanish in high school, and got quite a trial-by-fire.  When you’re speaking a subset of the language consisting almost entirely of common verbs and nouns, and not worrying so much about verb conjugation, and the person speaking to you slows down, it’s actually not so bad.

  • We went over to the Gulf Coast on our last day, where some of us explored a shrimp boat that had run aground.  This was actually a lot of fun!

  • Most of Mexico was very dirty, with litter everywhere.  I’m not quite sure why.  I mean it doesn’t seem like you need to come from a wealthy background to know not to litter.  Maybe garbage collection isn’t as reliable as it is in America, so people don’t have a choice but to throw their trash in the streets?  I don’t know, I’m just speculating.

  • We also went to what Jack called “Fishtown”.  This was a very poor fishing village on the coast.  I asked Jack what happens when a hurricane comes through, and he said the city sends buses to pick the people up, and after the storm they go back and salvage what building materials they can to rebuild their shacks.  We visited two pastors that Jack knew in Fishtown, one of which who is trying to start an orphanage.

  • After Fishtown, we came back and made lots of sandwiches, then we went to the Matamoros city dump to pass out the sandwiches and fruit and juice.  There are a lot of people that live next to the dump, who make a living salvaging what they can from the dump and selling it.  We passed out the sandwiches, along with fruit and juice.  It was another humbling experience, seeing this level of poverty that only exists in the United States for maybe the bottom 0.01% or something.

  • As we were leaving Mexico on our last day, we got pulled over by the police.  The officer claims we ran a red light, but several of us saw the light as we went through it and it only turned yellow as we were going through.  In addition, another car followed us through the light.  I didn’t catch that car’s tags as we pulled over, but I have a hunch that it had Mexican tags.  The officer told us that we would have to go to the office downtown to pay the ticket.  After putting on this show of being super strict for five or ten minutes, he said “O paga thirty-five dollars, ahora”.  (He had a firm grasp on the English language when it came to the phrase “thirty-five dollars”.)  Anyway, we ended up giving him thirty dollars and he let us go along.  We learned later that the fine for running a red light is only about five dollars, and we’re pretty sure none of that thirty dollars will ever make it to the city.  But there wasn’t much we could do.  It was one of the few times we didn’t have a local with us, and it’s not like the officer gave us any paperwork.  And we weren’t going to let him lead us to la oficina, because who knows where he would have actually taken us or what he would have done to us once we got there.  And it’s not like we can report the guy or hire a lawyer or anything.  I mean I know we have corrupt police officers in the United States, but they’re not quite so blatant about it.  I guess it was Mexico’s way of saying “thanks for visiting, and don’t come back!”

Well the bullets were supposed to keep this post from being too long but it has gotten quite large anyway.  And I’ve even left out a lot of stuff!  I guess I’ll end by saying that it was a great experience getting to interact with people in another culture, and very humbling to think about the conditions that most of them live in, compared to the comforts I enjoy in the United States.  I’ve always thought it sounded cliché to hear people say things like that, and maybe that’s what you’re thinking as you read this.  I guess I just think the world would be a better place if more people had the experience of playing soccer with the locals in another country.

I have put up a bunch of photos from the trip.  I may put up more as I get pictures from other people’s cameras, and you may have already seen these photos on my Facebook.

No Comments
RSS feeds: Kip's - Stephanie's - Both
Admin