Sit back, relax, and enjoy the code.

Quick Hits: Becareful when returning inc’ed vars

Posted: May 1st, 2010 | Author: abel | Filed under: MonoTouch, Programming, Quick Hits | Tags: , | 1 Comment »

I noticed this in some MonoTouch code that I wrote recently, but I think it makes sense across other languages as well.  Let’s say you write a method that returns an incremented variable like this:

private int MyAwesomeFunction(int pVar)
{
// coding magic
return pVar++;
}

…where pVar = 10, the returned value will NOT be 11!  The returned value will still be 10.  If you want to return an incremented value for pVar, do the following:

private int MyAwesomeFunction(int pVar)
{
// coding magic

return ++pVar; // or return pVar+1;
}

Just a case of postfix vs prefix.  In the previous example, the variable never gets incremented because the postfix operator would get evaluated after the line is processed, but that never happens because we’re returning the pVar.  By flipping to a prefix operator, we guarantee that we evaluate the opperator against the variable before we return it.

It’s also worth mentioning that being a little verbose here is also good.  Explicitly stating that you’re adding 1 to the variable (as displayed in the commented code) is the same number of characters and we eliminate the confusion over what’s getting returned.

Happy Coding!

  • Share/Bookmark

Quick Hits: When your .gitignore file gets…ignored

Posted: February 6th, 2010 | Author: abel | Filed under: Version Control | Tags: , | No Comments »

I made a mistake while creating a GIT repos that I’m hoping I can save you from making.  I made my first commit on a project and I realized that a bunch of files were getting in that shouldn’t be.  I created a .gitignore file, but the issue kept happening on subsequent commits.  As I learned later, you SHOULD create your .gitignore file first.  However, if you forgot, this is how you fix it:

  1. Keep your .gitignore file.
  2. Clear your GIT cache.  Don’t worry, this won’t delete any of your local files, just what GIT is tracking.
    git rm -r --cached .
  3. Add everything in your project.  Your .gitignore file will exclude what you want to ignore now and start tracking the good stuff.
    git add .
  4. Commit your changes.
    git commit -m "Now my .gitignore file works correctly!"

Happy codding!

  • Share/Bookmark

I LOVE MonoTouch

Posted: January 24th, 2010 | Author: abel | Filed under: MonoTouch | Tags: , , | 1 Comment »

Don’t get me wrong, XCode is great…in it’s own way…but as a .Net dev coming to the iPhone platform, MonoTouch rocks my world!

For the other devs in my shoes looking to play with MonoTouch, here are some things to keep in mind: Read the rest of this entry »

  • Share/Bookmark

FasterCSV and noob-ish silliness

Posted: March 23rd, 2009 | Author: abel | Filed under: Programming, Quick Hits, Rails | Tags: , | No Comments »

Here are two things I discovered this weekend:

Thing 1: FasterCSV is really cool!  It’s easy to use and does exactly what it should.  Here’s Scott Becker’s exporting tutorial.  For importing, i’ve hacked together bits from Peter Larkmund’s travels and the FasterCSV documentation.  Thanks to these tutorials, AMB will give users the ability to upload and export CSVs.

Thing 2: Default values for the win!  If you have a table that has a column for an amount, feel free to set the default value to 0.00.  Otherwise you won’t be able to do the following:

a = Account.new
a.amount += 20

…because a.amount will be nil.  You’ll throw an error and if you just updated a bunch of code, you might waste an hour trying to figure out why you’re throwing an exception.  However, if you declare a default value, you’ll be able to avoid any issues.

If you want me to go into further detail on either thing, just leave a comment.

Thanks for reading and happy hacking.

  • Share/Bookmark