Midwest Developer Insights

What I Like and What I've Learned

Posts Tagged ‘Development’

I Heart Karnaugh Maps

Posted by Stu on June 11, 2010

Have you ever found yourself writing a long Boolean condition in your code like the line below?


if ((policy.Type == PolicyType.AutoInsurance && policy.PolicyHolder.PriorAccidents == 0) || (policyPaidInFull && policy.Type == PolicyType.AutoInsurance || policy.IsPremium) || (policy.Type == PolicyType.AutoInsurance && policy.PolicyHolder.PriorAccidents == 0 && policy.IsPremium))

 

I Heart Karnaugh MapsPerhaps that line of code above is the first and easiest way you thought about all the conditions that have to occur in your application’s business logic. You write that line of code, test it in many different scenarios and it works so you think you have done a good job. Well, there are ways to improve upon that line of code by removing logically equivalent Boolean expressions, not to mention some style improvements that might make it more understandable.

We owe gratitude to our dear friends the Electrical Engineers for developing a clever tool, named Karnaugh Maps, to help with this dilemma, usually for cases of no more than 6 variable conditions. Karnaugh Maps (pronounced “car-no” and often simply called “K-Maps”) are a system for reducing Boolean expressions into a more simplistic form. They originated from the need to reduce electrical wiring gates, or circuit minimization, but they are still useful for the high-level software developer.

 

Benefits of reduced Boolean expressions

  1. Increased program performance
  2. Increased readability of code
  3. Less code results in easier to change code

Reducing our example expression

The 1st step is to let letters represent each of the conditions in our expression. In our case:

  • a :     policy.Type == PolicyType.AutoInsurance
  • b :     policyPaidInFull
  • c :     policy.PolicyHolder.PriorAccidents == 0
  • d :     policy.IsPremium

Then, draw a graphical square like the one pictured below. This represents each possible combination of our Boolean conditions. The boxes are blank because we have not yet entered what Boolean results we want in our resultant expression.

Blank Karnaugh Map

Let’s take the first, simplified condition in parentheses, if (a && c), and put it into the map. The result would look like the below image, because we only need to fill in the boxes where a and c are both 1.

Sample Karnaugh Map

Following this example, we can use the entire Boolean expression to fill out the whole map. The completed Karnaugh Map is pictured below.

Completed Karnaugh Map

Now circle any square or straight line of boxes since they correspond to an expression that differs by only two bits.

Circled Karnaugh Map

Because of the way we have arranged our variables around the outside of the map, we can eliminate variables based on boxes filled with 1s being adjacent to each other. In our example, the vertical “circle” exhibits a scenario where the expression should always be true as long as both the a bit and the b bit are 1, hence the condition ( a && b ). Likewise, the square “circle” exhibits true cases whenever the c bit is 1 and the a bit is 1. Because the b bit and d bit are true no matter if their values are 0 or 1, they can be deleted from the resulting simplified expression, which would be || ( a && c ). We use a similar rule to find the final d condition.

In one sentence, this rule can be summarized as “the circled boxes can be grouped together and the two variables that differ can be discarded.”

The resulting Boolean expression is:

if ( ( a && b ) || ( d ) || ( a && c ) )

which, using Boolean algebra, can be further reduced to:

if ( ( a && ( b || c ) ) || ( d ) )

We were not able to completely remove any variables, but we did simplify the expression quite a bit. The original Boolean conditions can be substituted for our letter variables, and we can rewrite the original expression as below:


bool policyIsAuto = ( PolicyType.AutoInsurance == policy.Type );
bool zeroPriorAccidents = ( 0 == policy.PolicyHolder.PriorAccidents );
if ( policyIsAuto && ( policyPaidInFull || zeroPriorAccidents ) || policy.IsPremium )

Seems easy, right? It is. And I am sorry if my steps went too fast for you. My intention is not to teach how to use Karnaugh Maps for all circumstances but instead to show you how easy and useful it is to simplify your Boolean logic.

For help with different scenarios, find the book Bebop to the Boolean Boogie – an Unconventional Guide to Electronics at your local library, check out this Wikipedia link, or you can even download software to perform the rules for you.

Happy Karnaugh Mapping!

In the mean time, I hope I was able to show you how fun and easy using a system like this can be. Feel free to post questions in the comments.

 

- Karnaugh Map Images taken from Bebop to the Boolean Boogie – an Unconventional Guide to Electronics

- Digital Logic image created by Garrett Crawford

Posted in Development | Tagged: , , , | 3 Comments »

Which Platform is the Best for My Mobile App?

Posted by Stu on April 28, 2010

I am currently sitting at the Cincinnati Microsoft office attending the CINNUG Mobile Development FireStarter. This free training session covers how to create mobile applications for Android, iPhone, and Windows Mobile phones. I do not have any immediate plans to create a mobile application soon, so what am I hoping to get out of this session?

3 Highlights I Want to Learn Today about Mobile App Platforms:

  • Pros & Cons of different platforms
  • Enough knowledge to be able to manage an outsourced app developer
  • Insight as to which platform is emerging as the leader

Throughout my career, I have been heavily focused on Microsoft development technologies. Often times recently, I have wondered if I should branch out to other languages and platforms. I have not yet done this, but if I were to move into mobile development (something I have limited experience with so far), it would be an ideal time to jump on the best platform as opposed to using Microsoft without questioning the decision. Therefore, I am happy to take advantage of this training session and glad that the format will discuss 3 different platforms.

What Did I Learn?

The training is over and I have formed some conclusions while generating even more questions. Below are summaries of what I learned.

Pros & Cons of Different Platforms

If I want to sell a mobile application then I need to make a good decision for which platform to build it. Each platform has its own benefits so it is possible that each platform could be best for certain types of applications. Below are the high-level Pros and Cons for each platform:

Android

Android Nexus One

Photo by Spieri_SF
    Pro

    Quickly gaining popularity

    Open source    

    Con

    Uses Java

    Somewhat limited for game development

iPhone

iPhone Image

Photo by William Hook
    Pro

    Most Popular

    Same OS for iPhone, iPod Touch & iPad

    Con

    Development requires a Mac

    Uses Objective C

    Only distributable through the app store

Windows Mobile/Windows Phone

iPhone Image

Photo by Brooks Elliott
    Pro

    Familiar tools and language

    Mature – platform has existed for a while

    Con

    No physical devices for new platform version (7)

    Adoption is a downward trend

 

Enough Knowledge to Be Able to Manage an Outsourced App Developer

Realistically, I am not going to drop all my plans or projects to dive in and write an iPhone application. I have enough wisdom to know that would probably be a waste of time unless I really want to learn iPhone development or if I had a great idea for an app for which I knew there was a market. Therefore, I don’t need to know at this time how to develop a mobile application. I just need to understand the highlights.

I am trying to prepare for the moment when I have that great idea for a mobile application. When that happens, I don’t want to be clueless about the next steps involved. I want to have a good idea for limitations of current platforms, which technologies are emerging, and how to move forward getting the thing developed.

Ideally, I will someday “own” a mobile application. When the time comes, my plan is to hire someone to build it, but I do not want to be ignorant to what is involved. By seeing these demos, I have been introduced to developing mobile applications. If I want to learn the details, I know where to start. If I want to hire someone else to develop a mobile app, I can now intelligently discuss the project and properly vet the person’s credentials.

 

Insight as to Which Platform is Emerging as the Leader

Perhaps the most important aspect to deciding which platform to develop for is how popular it is. Simply put, the bigger the market that my application can reach, the more sales leads that can be generated. So which platform will the most users be running when my application is finished and ready to be sold?

At the time of this writing, the iPhone is the most popular of these 3 devices. 2nd is Windows Mobile, with Android placing 3rd.

Most importantly, iPhone users consume the most network bandwidth out of all smart phone users. I believe this is a testament to the high user engagement with iPhones and consider this a forecast of its future growth. Some industry experts have opined that Google’s Android platform will emerge as the leader given its “open” paradigm is more beneficial to developers. Still Microsoft’s upcoming release of Windows 7 Phones may tip the market share into their favor.

I am by no means an expert, but my bet would be that the iPhone will be the dominant smart phone platform for the next few years. The barrier to entry for users is low and it already has a head start influencing the industry. Everyone that has an iPhone loves it and everyone who doesn’t have one wants one. I don’t know a single person who is excited for any Windows Phone news and only a few who are aware of Android’s developments.

If Apple ever allows the iPhone to be used with wireless networks in addition to AT&T, such as Verizon, look out! They will have removed the biggest barrier left to everyone wanting one.

Which mobile platform do you see emerging? Which platform would you recommend? Your opinion is probably more informed than mine.

Posted in Technology | Tagged: , , , | 2 Comments »

My First Experience with Microsoft Windows Azure

Posted by Stu on December 13, 2009

This week I received my Microsoft Windows Azure invitation token, so I began integrating it into a project of mine.  I wanted to use just the Blob Storage to upload and download large media files to the cloud.

At first, I was trying to follow this link as much as possible to get up and running.  The walk-through seemed simple and helped me to quickly understand how to use Blob Storage.  However, once I created the new Azure service in my existing solution I was quickly led to make decisions that influenced the web project I had already created:

  1. In order to get the Azure processes to run in the background while my web application ran, I had to set the Cloud Service project as the startup project. Set As Startup Project
  2. A Cloud Service project cannot just run by itselt, it requires a web role.  The simplest thing to do seemed to be to set this to my pre-existing web project.
  3. This all worked well at first.  My web project recognized my local azure service and worked normally.  However, once I tried to upload files to my local Blob Storage I received errors.  It behaved as if there was an issue with the Trust Level that the web role runs under.

Since Azure is still in CTP (meaning free) and I have a brand new account (meaning no pre-existing files have been uploaded), my solution was to test my web upload with a deployed, in “production”, Blob Storage service.  I updated my web.config file and fairly quickly was able to upload and download files from blob storage.  I was somewhat impressed with how easy it was to do.

Unfortunately, this created a whole new set of concerns.  Since I am forced to use my production account for Blob Storage, how can I test in my local environment going forward?  Once Azure is launched (reportedly on 02/01/2010), any testing I do will begin to incur significant costs and it will also interfere with production data.

After some deep thinking, some more research and stumbling across this MSDN link, I believe I backed my way into the solution that Microsoft imagined in the first place.

Microsoft has allocated a specific development shared key for local blob storage.  Therefore, one only has to use these standard configuration settings in the local Blob Storage service (ServiceConfiguration.cscfg file).

<ConfigurationSettings>
<Setting name=”AccountName” value=”devstoreaccount1″ />
<Setting name=”AccountSharedKey” value=”Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==” />
<Setting name=”BlobStorageEndpoint” value=”http://127.0.0.1:10000/” />
</ConfigurationSettings>

The piece of the puzzle that I was missing is that this Blob Storage service must run from outside my main solution.  Therefore, I put the Blob Storage service project in its own solution and attached it to a dummy web role.  When I run it, it starts the development Azure services and runs in the background.

Attaching to the Blob Storage service from my web project is easy.  I just make sure that I have the below configuration set in my web.config file.

<configuration>
<appSettings>
<add key = “AccountName” value=”devstoreaccount1″/>
<add key = “AccountSharedKey” value=”Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==”/>
<add key=”BlobStorageEndpoint” value=”http://127.0.0.1:10000″/>
</appSettings>
<configuration>

Now I can test uploads and downloads on my local machine.

Special Note:  The Windows Azure development environment uses a different URI format to access files.  In production, files stored in public containers can be accessed via this format:

http://<account-name>.blob.core.windows.net/<resource-path>

However, in the development environment, a different format is used:

http://<local-machine-address>:<port>/<account-name>/<resource-path>

See this MSDN Article for more information.

Posted in Development | Tagged: , | 1 Comment »