in-line assignments in conditional statements

Consider the following blocks of pseudo code:

if( (conn=connectToDatabase())==FALSE )
{
  throw exception("could not connect to database");
}
else
{
  resultSet = conn.executeQuery("SELECT * FROM sometable");
}

and

conn = connectToDatabase();

if( conn==FALSE )
{
  throw exception("could not connect to database");
}
else
{
  resultSet = conn.executeQuery("SELECT * FROM sometable");
}

Both blocks of code perform the exact same function, but the first is considered poor practice because the assignment of the variable “conn” occurs in-line with the conditional statement. So, if they operate the same way, why is one bad and the other is not? Additionally, why is the one that performs the same operation with fewer lines of code bad?

It all stems from the use of the “=” assignment operator in the conditional statement. Take a look at this code:

a = 0;
b = 1;

// using conditional operator "==" to check for equality
if( (a==b) ) print "a and b are equal";
else print "a and b are not equal";

print "a=".a." and b=".b;

// using the assignment operator "=" in the wrong way
if( (a=b) ) print "a and b are equal";
else print "a and b are not equal";

print "a=".a." and b=".b;

a=1;
b=0;

// using the assignment operator "=" in the wrong way with different results
if( (a=b) ) print "a and b are equal";
else print "a and b are not equal"

// This program will output the following:
//
// a and b are not equal
// a=0 and b=1
// a and b are equal
// a=1 and b=1
// a and b are not equal
// a=0 and b=0

In the example above, the output is unreliable because of the use of the assignment operator in the conditional. While any experienced programmer should be able to catch these problems right away, a novice may not see exactly what is happening here.

This leads me the conclusion that perhaps assignments in conditionals are not functionally bad, but they can be confusing. So, I would say that if you know what you are doing, and are confident that the people reading your code know what they are doing, these in-line conditionals may not be all bad. Just don’t teach them to anybody new to programming. It’s the whole “we’re professionals, don’t try this at home” principle.

But there’s even a problem with this line of thinking. I see these in-line assignments all the time in tutorials and various texts (teaching people programming). I can’t tell you the number of times I’ve taken boiler plate code from a book, and ended up with tons of compile time warnings complaining about assignment operators used in conditional statements.

I’ve worked on projects with people who would throw you out of the development team if you wrote code that looks like the first example above. Other times, I’ve seen every “while” loop in a program look like this.

I tend to believe that moderation and readability are the keys here. Use assignments like this where it makes sense; just don’t over do it simply because you can.

Thoughts?

Pascal’s Wager

Blaise Pascal was a 17th century mathematician and physicist, who also lends his name to a [somewhat] unfortunate programing language.  Aside from the scientific realm, he also played a bit in the area of religion.

Pascal postulated that while the supernatural nature of God could not always be explained with reason, a reasonable  person could come to believe in God based on reason alone.  This is the basic idea behind the “Pascal Wager”:

Everyone must choose whether or not God exists, there is no neutral stance.  Because one must choose, it is only logical to weigh the pros and cons of each possibility.  If you choose to believe that God exists, then there are two possible outcomes:  1) God exists and because you believe you will spend eternity in bliss, and 2) God does not exist and death is the end (your belief had no positive or negative gains).  If you choose not to believe in God there are two possible outcomes:  1) God exists and you are eternally damned, and 2) God does not exist and death is the end (your disbelief has no positive or negative gains).  The two outcomes for belief in God are a net gain, and the two outcomes for not believing in God are a net loss.  Therefore, it is only reasonable to wager/bet on a belief in God.

Growing up in the Bible belt, I heard a similar argument from my fellow Christians on a regular basis.  They would explain to people who did not share their belief that “if I am wrong, then I’m still going to be ok; if you are wrong, you are going to hell.”  While I understand (and agree) with the logic here, I always had some pretty serious problems with this line of reasoning.

There is a difference in stating belief and actual belief (or faith).  Simply because a person says they believe in God does not make it so.  Pascal said that even someone that did not believe in God should live as if they did believe, and through their actions they would eventually come to true belief.  I personally believe that this idea is a bit nieve; and that simply willing yourself into belief is an oversimplification of supernatural aspects of faith in a god.

The second issue I have with this line of thinking is that it places the emphasis on a belief in God on a fear of eternal damnation.  I have always had a big issue with theologies that place the primary motivating factor for a belief in God to be a fear of going to hell.  It always seemed to me that while hell might be a consequence of not believing, it should not be the reason to believe.

Pascal’s wager is the prefect example of what happens when you attempt to explain the supernatural complexities of God with human understanding.  God cannot always be placed in the little box of logic and reason we’d like to place Him in.  The very definition of something supernatural is that it is outside of the natural order and understanding of our universe.

brushing up on some skills

While browsing the classifieds, I’ve come to the conclusion that I’ve got to refresh the skill set a little and brush up on some things I haven’t dealt with in a long time.

I’m primarily a Java and PHP developer, and 7 solid years of commercial & enterprise development experience is enough to get my foot in a lot of doors.  While my particular expertise is in fair demand, there are a few other areas that seem to be a bit more sought after right now:

.NET Framework

You cannot throw a rock at job listings without hitting an ad for a C# developer.  I’ve done a bit of .NET development in the past, but it’s been a few years; there are also some areas of the .NET framework that I’ve never played with.  The Kinect SDK is also looking pretty tempting on a lot of fronts.  So, I guess I’m going to spending some time in Visual Studio in the near future.

C/C++

Really good C programmers are getting harder and harder to find, but the demand isn’t going away.  I wish I could say that I’m one of the really good ones, but unfortunately I’m not there (yet).  My first meaningful attempts at software development were all C and C++, but I haven’t been tasked with it too much on a professional level.  However, I’ve recently been diving back into some crypto stuff I did for OS X a little while ago in C++.  So maybe I can get back into the swing of things a bit more.

Mobile Development (Java/Android and Objective-C/iOS)

There’s not as big of a demand for mobile developers as you’d guess, but theres still a good number of opportunities out there.  I’ve spent a lot of time in Blackberry simulators/debuggers, but that experience has proved to be almost useless.  I’ve got several personal projects going right now for android; hopefully I’ll be hitting the market with some encryption stuff in the coming months.  I’m technically and officially a registered iOS developer, but I don’t own an iOS device … guess I’d better get one at some point (the simulator is getting old).

Advanced SQL

There are TONS of database opportunities out there, and I work with databases A LOT.  In the last year, there’s not a major RDBMS package out there that I haven’t worked on in some way or another.  I’d say I’m reasonably proficient, but because I have to deal with a lot of different databases, my experience is a bit more generic than I’d like.  In particular, I need to brush up on some specific Oracle and MS SQL topics.

I really do enjoy learning new things (and re-learning old things), so I think I’ve got an interesting few months ahead of me…

i am the only one left

Every time someone brings up Facebook (which is pretty much constantly), I feel like I’m in that Star Trek TNG episode where everyone becomes addicted to that game that rewards the player with a “pleasurable sensation”. I’ve come to the conclusion that new status updates from friends must have the same orgasmic effect on people. Not passing judgement, just sharing an observation :)

I’m not completely spotless though. See, I once had the addiction too, but I’ve been clean for a little over a year.

It’s not that I’m better than any of you really … I just choose to extend my geeky social awkwardness to the interwebs too.

wordpress theme updates

Try as I may to switch to another CMS, I keep falling back to WordPress. My last attempt was Drupal, which I quickly grew tired of. WordPress doesn’t do everything I would like it to do, but the things it does are amazing.

I’ve been working a bit on my theme to add some extra functionality. Here’s what we’ve gotten so far:

  • Three column format: My sidebar were getting a bit too long, so I split it in two.
  • Variable width layout: Because there are three columns, I needed a bit of extra horizontal space. I was getting tired of the fixed width/centered layout, so I implemented the variable width middle column to hold the main content.
  • HTML5: I’m using quite a few HTML5 specific tags in my theme, but everything should also be ok in older browsers too. It was a bit of a beast getting rid of the XHTML-ness that WordPress puts in automatically, but I’m getting there.
  • CSS 3: Some CSS 3 eye candy here and there (rounded corners, gradients, etc).

Now that the most current version of each of the major browsers support HTML5 and CSS 3, I’m going to be relying on them pretty heavily here.

And for those who refuse to upgrade … well, sorry :)

Comments? Smart remarks?

html5

I’m making at attempt to HTML5-ize the blog here.

It hasn’t been too difficult of a task thus far, but I’ve got a few little issues to work out. Check out the validation result:

Validation Report for https://cryptofreek.org

It looks like all my problem are related to bad attribute values in WordPress generated code. Anybody know how to fix that?

[UPDATE]
Looks like I’ve fixed most of those problems :)

But, there are multiple other issues with the “rel” attribute in other tags throughout the site. Perhaps I can find a way to just strip out all of those attributes until a better solution is found…

don’t forget the “MAG” in IMAG

No, we’re not talking about some magical Apple device that attaches itself to your refrigerator and allows you to tweet and stream movies to your toaster at the same time…  IMAG is short for “image magnification”, and it is the term used to describe the video used at live events to bring you closer to the action when you’re in the cheap seats.

I’ve been in a few places recently where I though they missed the entire point of IMAG, so here are a few thoughts:

Does my venue even need IMAG?

Let’s try to remember the primary purpose of IMAG:  To bring the action on stage closer to the audience.  There is nothing more distracting in your small venue than your speaker’s face projected 10 feet tall on a screen thats only 20 feet from your audience.  If you can already see the stage on the people on it (in reasonable enough detail), most IMAG video is probably overkill.  Your video work should add to the atmosphere of whatever even is going on, not overpower it.

There is an exception to this however:  use IMAG in small venues when fine detail is needed.  If you have very intricate things happening on stage, close up video can make a world of difference.  Live performances involving complex music, artwork, visual aids, etc. can benefit greatly from IMAG even if the presenter’s are clearly and easily viewable by the audience.

For example, a few years ago I worked on an event with a tattoo artist on stage in mid-sized venue (300-400 seats).  In this case, there really wasn’t a seat so far away that you could not clearly see what was happening.  So, a (constant) close up shot on the speaker would have been a bit much.  However we had a camera set up and zoomed in on the tattoo, and it produced a dramatic effect.  It really did bring the entire effect to a whole new level.

Really, just use common sense.  There is a time and place to use IMAG in smaller events, but “just because I can” is not a good enough reason.

What’s “MAG” stand for again?

Now, the oposite end of the spectrum REALLY unnerves me.  A few weeks ago, I was at an outdoor event where I was at least 150 yards from the stage.  They had a big IMAG screen set up, but in 90% of the camera shots the image was on a 1:1 scale (sometimes smaller) with the stage.  Basically, the folks on the screen weren’t any bigger than they appear on stage.

Remember that you are magnifying the image so that your audience can actually feel that they are a part of the action.  If your attendees in the upper decks still can’t make out what’s happening on the JumboTron because the image is too small, you’ve just wasted your money.  Why do you even need the screens in that case?

google music (beta)

I just got my invite to Google Music just a few minutes ago.

I created my account then installed the software on my PC and my phone.  The PC software scanned my music folder and started to sync my songs to the great Google cloud, and now (10 minutes later), I’m listening to the music collection I’ve collected over the years streaming to my phone.

You can activate up to 8 devices on a single account.  Very cool stuff.

I think it will sync your iTunes library too … but I wouldn’t know because iTunes is the most evil piece of software ever, and I refuse to use it.

how i made my samsung fascinate not suck

Last week, I got a new Samsung Fascinate (Verizon’s Galaxy S phone).  It was the free phone at Best Buy, and it was time to add a new line to the family plan.

On the surface it’s a decent enough phone, and the hardware is great.  Sure, it’s not a 4.3″ screen with a dual core 1.2Ghz processor, but for last year’s model it’s nice.  The big problem with this phone is the software.  Here are the big gripes that I and others have with this phone:

GPS

Out of the box, my it took 3-5 minutes to get a GPS fix (completely unacceptable).  Once I got a fix, it was accurate within reason; however, others have reported that their accuracy was way off with this phone.

I’m not sure why, but an app from the Market fixes the problem all together:  GPS Status & Toolbox

Once I installed this and ran it, I was getting a GPS fix in 5-10 seconds.  I don’t have a clue why this works, but it did for me.  Your mileage may vary; in any case, it’s a very cool little app to have.

TouchWiz UI

Any UI is subjective, but there seems to be a general disdain for Samsung’s TouchWiz interface.  I personally could have lived with it, but I was not a fan either.

Install this:  LauncherPro

I had never used LauncherPro before, but I’m pretty sure it might find its way onto any of my future Android phones.  It allows you to customize your homescreens, doc, app drawer, and a ton of other things.  Very nice, and it got rid of 99% of all the TouchWiz annoyances.

Bing

This isn’t really Samsung’s fault, but Verizon has felt the need to have Bing as the default search engine and map provider on the Fascinate.  There’s really no way to change this, but installing Google’s Search and Maps goes a long way.  May others have changed their browser home page to google.com, but I haven’t felt the need.  I just put the Google Search widget on my homescreen, and I’m good to go.

In the original phone (with Android 2.1), you could not download and install Google Search.  However, since the Froyo update last month, it’s no problem.  I’m not sure how long you’ll still be able to buy one of these phones new, but mine came out of the box with Froyo.

But, after all this, the Search button on the phone still uses Bing … Oh well.

Verizon Navigator

Verizon Navigator is the default (and unchangeable) navigation program on the phone.  The icon on the Car Dock screen will ALWAYS open VZW Navigator (making this screen basically useless).

Get the Google Navigation app from the Market and put a shortcut on your homescreen.

Crazy Bloatware

I don’t think I’ve ever seen a phone with this much useless software preinstalled.  You can’t remove it, but at least with LauncherPro (above) I can hide the icons from the app drawer.

So, contrary to popular belief, it is possible to make the Fascinate tolerable (even likable) without rooting and custom ROMs.  I don’t know that we’ll ever get Gingerbread, but for now I’m happy.

how many fingers am i holding up?

Last night, me and the boy were watching a show on National Geographic called “Known Universe“.  This particular episode was all about the infinite nature of the universe and the possible existence of the multiverse.

It reminded me of a conversation we had driving down the road a week or so ago:

The Boy:  “Hey daddy, how many fingers am I holding up?”

Me: “3?” (knowing it was a trick question somehow)

The Boy:  “Nope, 2!  The other one is a thumb!”

Me:  “Hey ‘the boy’, how many fingers am I holding up?”

The Boy: “I don’t know, I can’t see your hands.”

Me:  “Well assuming that there are infinite universes, that our universe is infinitely big, there exists infinite numbers of parallel dimensions, or any combination of the above … I, and all of the infinite other me’s, are possibly holding up an infinite number of fingers … Regardless of the fact that the ‘me’ currently occupying this portion of time/space is only hold up a single finger.”

The Boy:  {silence}

The Wife:  “I have a single finger for you…”