Archive for June, 2011

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?