5 Great Snacks For the Coder’s Brain

You’re coding and you’ve been in the flow for a few hours. Everything is going just fine, but then hunger strikes suddenly. Since you don’t have anything on hand, you go to the cafeteria or to a nearby cafĂ© to grab a muffin or a pastry full of sugar. When you sit down to resume coding while eating your snack, you feel good for a few minutes, but the sugar spike soon wears off. You start feeling like crap, and your productivity screeches to a halt. Sounds familiar?

OneDoesNotSimplyGrabASnack

This happens to everybody from time to time. Working with your brain makes you hungry very fast, but eating a large sugary snack to compensate and topping it with an energy drink is the least productive way to go about it. If you’re well prepared and have good quality snacks on hand, you won’t feel like crap after eating and you will be able to resume coding in no time. Great snacks are also power foods that are great for your health, so it’s worth adding them in your diet.

So here are a few snacks you should try:

  • Nuts and seeds: this is one of the best power snack to keep around since they store well and you don’t need to eat a lot to feel better. You should be making your own mixes if you can, since most trail mixes available on the market are too salty, too sugary or both. You can add all your favourite nuts, but almond, walnut, brazil nuts, pumpkin seeds and sunflower seeds are all great choices. You can add a bit of quality dark chocolate or dried fruits to the mix for more variety.
  • Fresh fruits: All fruits are also good snacks. You can bring along anything from bananas and apples, but for pure nutritional values berries are the best. I don’t recommend fruits like kiwi that need to be peeled since you don’t want to mess around when you want something quick. Fruits can also be kept at your desk for a few days if you want to bring some in advance.
  • Yogurt or kefir: Yogurt is a great source of protein and probiotics, but you should choose yogurt with as little sugar as possible: you can add your own fruits if it’s too bland for you. I’ve started drinking kefir recently, which is a fermented milk drink with even more good bacterial than yogurt. It’s interesting but it’s an acquired taste, being a lot sourer than yogurt.
  • Egg, fish or avocado: Those require a bit more preparation, but the proteins and good fats that make it worth it. Boiled eggs takes only a few minutes for a week’s supply and are easy to eat. Avocados are also fast to prepare: just cut them in two, add some mustard or lime juice and pack them in your lunch. Half an avocado is enough for a snack, and it can easily be eaten with a spoon. And you can bring canned sardines or smoked salmon, which requires no preparation.
  • Vegetable sticks: All raw vegetables that can be cut into sticks like carrots, celery and peppers are good options. If you don’t have time to cut vegetables, you can buy bags of precut vegetables at the grocery store. Pair them with some hummus dip for added protein and you’ll have a great snack on hand. This is a good way to add more vegetables to your diet, we could all do with more of them.

As an aside, if you really like muffins and can’t do without, you can make your own whole grain muffins and use less sugar. Homemade muffins will give you less of a crash than white flour and allow you to add more fruits, nuts and seeds. You can also make smaller portion when you bake them yourself, since most store-brought muffins are huge. This is a bit heavier than the other snack options, taking more energy to digest. Many people have trouble when they eat too much grains or gluten, so you should try it and see what works for you.

Which snack are you going to try? Do you have a favorite snack that’s not on the list? I’m always on the lookout for new ideas, so please share your routine!

Sign up now to get my newest articles about making applications and my ongoing side project delivered straight to your inbox.

Dealing with the outside world for programmers

To work with the outside world where business and entrepreneurs lives, you must understand that we are in an alternate universe. For a programmer, playing with technology is fun. We live in a world where building your own server and rolling out your own custom solutions to every problem is something desirable. If something is hard to master and uses bleeding edge technology, it’s even better!

But when you step out in the real world where entrepreneurs of all shape, size and technical skills build businesses and deliver value, they don’t care about what is going on under the hood. When people need a business solution, what they really want is something that makes their job easier. That’s perfectly logical: who would implement a solution that makes their live harder and more unpleasant?

So, considering this, the role of a programmer in the business is to use technology to solve problems for people. Your job is greasing the wheels of business to make sure everything goes smoothly, regardless of the technology involved behind the scene. If your solution slows down people and gets in the way, you failed your client or your employer and this is something that should be a priority for you to fix.

cin-hammer

If you don’t fix your software when this happens, people using it to do their work will find a way around it instead of working with it. A tool is pointless without people using it, and if less and less people are trusting your software, the information in it will become unreliable. Software is alive and should evolve with the need of its users: if you don’t care for it, it will wither and die.

Sometimes this will be at the intersection of business needs and fun: a new tablet application for users needing mobility or a new web site that works with the newest browsers. But it won’t always be fun and unicorns: at other times, doing your job means maintaining an old PHP web application that’s used only by a few people, but that is essential to keep the business going. Or it means making sure that your application works fine on old Windows XP desktops that are still in use even if you’re developing in on the latest MacBook.

So, you have a choice: if you want to deal with the outside world and make people glad to have a programmer around, then you need to learn that the best solution is not always the complex one. Yes, this can mean that the solution to their problem is a WordPress installation and not a custom solution that took you a year to code, but your job is to make people happy.

Will you come with me outside?

And for any entrepreneurs reading this, where are we failing you?

Saving an image in a SQLite database in your Android application

When you start caching data to a local database from your Android application, sooner or later you’ll need to save images in that database too. For example, if you’re storing reports about observations the users make in the field that will be uploaded to the main system later, it can be handy to add a picture to better describe the problem.

The first idea that comes to mind to store those images is saving them directly in the database as BLOBs (Binary Large OBjects). It can work, but it’s not the most efficient way to do it since SQLite is meant to store values and not large binary values. Also, the local database could get big pretty fast, especially if you need to save full size, high quality pictures. You can check out a benchmark on the official SQLite site at https://www.sqlite.org/intern-v-extern-blob.html if you want to have a better idea of the performance.

What you really want to do in that case is keep only the path to the image in your database and save the image in the internal storage of your application. When you need to use the image, you can simply use the path to fetch the image from the file system. I recommend you save the pictures to the internal storage since so it’s accessible only from your application and is available at all times; actually, the SQLite database itself is also stored in the internal storage.

Here is an example of how to add an image to an existing report, saving it in the internal storage and keeping the path in the database. To learn more about how to create a database and store data to it, you can read my article about saving to a SQLite database.

public class ApplicationDatabaseHelper extends SQLiteOpenHelper {
/**
  * Updates the current picture for the report.
  *
  * @param reportId the identifier of the report for which to save the picture
  * @param picture the picture to save to the internal storage and save path in the database.
  */
public void updateReportPicture(long reportId, Bitmap picture) {
   // Saves the new picture to the internal storage with the unique identifier of the report as 
   // the name. That way, there will never be two report pictures with the same name.
   String picturePath = "";
   File internalStorage = mContext.getDir("ReportPictures", Context.MODE_PRIVATE);
   File reportFilePath = new File(internalStorage, reportId + ".png");
   String picturePath = reportFilePath.toString();

   FileOutputStream fos = null;
   try {
      fos = new FileOutputStream(reportFilePath);
      picture.compress(Bitmap.CompressFormat.PNG, 100 /*quality*/, fos);
      fos.close();
      }
   catch (Exception ex) {
      Log.i("DATABASE", "Problem updating picture", ex);
      picturePath = "";
      }

   // Updates the database entry for the report to point to the picture
   SQLiteDatabase db = getWritableDatabase();

   ContentValues newPictureValue = new ContentValues();
   newPictureValue.put(ReportContract.ReportEntry.COLUMN_PICTURE_TITLE, 
                       picturePath);

   db.update(ReportContract.TABLE_NAME,
             newPictureValue,
             ReportContract.ReportEntry._ID + "=?",
             new String[]{String.valueOf(reportId)});
   }
}

If you save a picture to the database, you also need a way to get that picture to display it in your application. Here is how to get the image that was just saved to display it again :

public class ApplicationDatabaseHelper extends SQLiteOpenHelper {
/**
  * Gets the picture for the specified report in the database.
  *
  * @param reportId the identifier of the report for which to get the picture.
  *
  * @return the picture for the report, or null if no picture was found.
  */
public Bitmap getReportPicture(long reportId) {
   String picturePath = getReportPicturePath(reportId);
   if (picturePath == null || picturePath.length() == 0)
      return (null);

   Bitmap reportPicture = BitmapFactory.decodeFile(picturePath);

   return (reportPicture);
   }

/**
  * Gets the path of the picture for the specified report in the database.
  *
  * @param reportId the identifier of the report for which to get the picture.
  *   
  * @return the picture for the report, or null if no picture was found.
  */
private String getReportPicturePath(long reportId) {
   // Gets the database in the current database helper in read-only mode
   SQLiteDatabase db = getReadableDatabase();

   // After the query, the cursor points to the first database row
   // returned by the request
   Cursor reportCursor = db.query(ReportContract.TABLE_NAME,
                                  null,
                                  ReportContract.ReportEntry._ID + "=?",
                                  new String[]{String.valueOf(reportId)},
                                  null,
                                  null,
                                  null);
   reportCursor.moveToNext();
    
   // Get the path of the picture from the database row pointed by
   // the cursor using the getColumnIndex method of the cursor.
   String picturePath = reportCursor.getString(reportCursor.
                             getColumnIndex(ReportContract.ReportEntry.COLUMN_PICTURE_TITLE));

   return (picturePath);
   }
}

Finally, you need to make sure to delete the picture from the internal storage if you delete the associated record from the database. If you keep the pictures, the internal storage used by your application will grow larger with time and your users won’t have any way to delete those pictures since the internal storage is hidden from them.

public class ApplicationDatabaseHelper extends SQLiteOpenHelper {
/**
  * Deletes the specified report from the database, removing also the associated picture from the
  * internal storage if any.
  *
  * @param reportId the report to remove.
  */
public void deleteReport(long reportId) {
   // Remove picture for report from internal storage
   String picturePath = getReportPicturePath(reportId); // See above
   if (picturePath != null && picturePath.length() != 0) {
      File reportFilePath = new File(picturePath);
      reportFilePath.delete();
   }

   // Remove the report from the database
   SQLiteDatabase db = getWritableDatabase();

   db.delete(ReportContract.TABLE_NAME,
             ReportContract.ReportEntry._ID + "=?",
             new String[]{String.valueOf(reportId)});
   }
}