Thursday, September 27, 2012

Simplify ternary operators

One of my pet peeves in Java/C/C++/C# programming is the use of the ternary operator ?: with boolean values. It's almost always redundant, makes code harder to read and can be easily replaced by a simpler boolean expression that also has the advantage of being "branch-free" in some languages, that is, do not require the CPU to select a path of execution based on a condition, which can be very expensive. The best compilers will optimize them away in a manner similar to what I describe below, but they still don't improve code readability.

From this point onward I'll be using the Java/C/C++/C# code syntax. A boolean expression is any valid expression that results in a boolean value. This may be anything from the constants true and false all the way to complex expressions such as x == 1 && (y > 3 || z <= 1.25) && !checkSomething(x, y, z) (assuming checkSomething(...) returns a boolean).

Say you have the condition c and the boolean expressions p and q. The following table contains all possible conversions from a ternary expression to an equivalent boolean expression:

#Ternary Expressionif-then-else ExpressionEquivalent to
1c ? p : qif (c) p else q(c && p) || (!c && q)
2c ? p : trueif (c) p else true!c || p
3c ? p : falseif (c) p else falsec && p
4c ? true : pif (c) true else pc || p
5c ? false : pif (c) false else p!c && p
6c ? true : falseif (c) true else falsec
7c ? false : trueif (c) false else true!c
8c ? true : trueif (c) true else truetrue
9c ? false : falseif (c) false else falsefalse

Note that #1 may or may not result in more readable code; it is probably better to use the ternary operator in that case. #6 through #9 are poor coding practices and should be replaced as soon as possible by their equivalent boolean expressions. #2 through #5 require attention and almost certainly will result in better, cleaner code.

Also note that c ? p : q is equivalent to if (c) b=p else b=q. In other words, if any boolean variables are assigned a value that depends on the condition of an if-then-else clause, then that value can be simplified according to the equivalence table above, as long as there are no unintended side-effects.

Let's try this with an example:

boolean b = i > 5 ? j == 3 : false

or, as an if-then-else clause:

boolean b;
if (i > 5) b = j == 3; else b = false;

Looking at the above table, this matches expression #3, with:

c = i > 5
p = j == 3

The equivalent expression would be:

boolean b = i > 5 && j == 3

which is slightly shorter and certainly less confusing than the above ternary or if-then-else statements.

Friday, September 21, 2012

Now Playing: Borderlands 2

This is going to be my first gaming-related post (yay!). I had a blast playing the first Borderlands game a while ago. Picked Mordecai, grabbed sniper rifles and pistols and chose a set of skills with emphasis on sniping. Cleared the whole game, including all DLCs. Leveled to 61 (before the cap was raised to 69). Then I got bored and hacked my character to make the most insanely powerful equipment out there -- 1.2k x4 damage pistols with near-perfect accuracy and very fast firing rates (and the ability to crash the game once in a while), "god mode" shields (which displayed as negative 2 billion and never drained), insanely fast sniper rifles and even some overpowered Eridian guns, and maxed out all skills in all trees just because. I remember doing a critical hit for the maximum possible damage of 99999 with a melee attack once. Needless to say, the poor victim exploded in a shower of gibs.

Back then I didn't have a computer powerful enough to record my gaming, nor did I have an YouTube channel or a stream, so unfortunately I got (almost) nothing to show you. :(

When the sequel was announced I was pumped. I watched every trailer and every bit of gameplay and just had to play it now (er...  then). Borderlands 2 was released three days ago (September 18th), but I had to wait two more days as the game would only release on September 20th here in Brazil.

The wait was over, and I had to stream it. Unfortunately, my microphone is really bad, so I couldn't comment on the stream itself.

Here goes my personal opinions on the game:
  • Borderlands 2 has an amazing sense of humor. Claptrap's lines are among the funniest, as are Handsome Jack's and pretty much every other non-srs-bsns characters'. If my microphone was working, you'd hear me laughing about every 10 seconds or so.
  • The guns are much better polished, and each brand feels unique -- an addition I liked a lot. I quite enjoy Jakobs pistols - they let me fire as fast as I can pull the trigger, which is to say, really fast. Tediore weapons are fun; you throw these cheap pieces of garbage away when reloading and a new gun materializes in your hands while the discarded gun proceeds to explode. I also like how Bandits can't spell their weapons properly.
  • Enemies are much smarter now too, and they also feel more "natural", more "realistic". Bandits will actually look for cover or just put their faces straight into your gun, and they also seem to work as a team now. They also have better environmental awareness and try to avoid/use explosives to their advantage (at least that's how it feels to me).
  • The world is HUGE. I've partially explored three major areas and this makes about 5% of the in-game world map. Each individual area is much larger than the previous iteration's areas. You also get a bunch of side-quests right at the beginning (OK, not too early) and there's always something to do or explore everywhere. Not to mention the vistas are amazing!
  • The new UI has a bunch of improvements, but the inventory feels a little clunky and not much of an improvement from the first game. Comparing items was made easier, but equipping items isn't as simple as before. To switch a weapon, you have to go to the backpack column, click on a weapon, then click on the weapon slot you want to switch. Switching shields, mods and stuff for some reason requires the same steps as above, even though there is only one slot for each. Buying and selling items, however, is much better than before.
And now, a bit of gameplay! The video quality should improve after YouTube decides to process the videos. Oh, and sorry about some videos being cut before the actual end. You're better off watching the videos from my stream channel anyway.






You can also watch the videos on my stream channel in 1080p: 1 2 3 4 5 6 7-1 7-2 7-3 8 9 10 11-1 11-2 11-3

Also, watch me live here!

Sunday, September 16, 2012

TCPRelay 0.3 alpha 3

TCPRelay 0.3 alpha 3 is now available! Download the 32-bit or 64-bit version. You may also need to install the Microsoft .NET Framework 4. Keep in mind that this is an alpha version; some features may not behave correctly.

What's new in this version:
  • General
    • NEW: localization support for:
      • [en-US] English (United States)
      • [pt-BR] Portuguese (Brazil)
    • Send me an e-mail if you wish to add your language.
    • NOTE: only the GUI version has support for localization for now.
  • Console
    • FIXED: application no longer crashes when a connection is closed. The fix never got into 0.2.2.3 for some reason.
  • GUI
    • NEW: Localization support, as above. By default the application will use the default language set in your operating system. You can override this by passing the language code as an argument to TCPRelay.exe. The supported language codes are listed between brackets above.
    • NEW: [alpha 3] application now has an icon (thanks to Fryderyk "Ziel" Dachowski!)
    • FIXED: [alpha 2] localization should now work properly on all parts of the GUI.
    • FIXED: [alpha 3] tooltips for speed buttons were incorrectly referring to "as slow as possible".
TCPRelay finally has its own icon, thanks to Fryderyk "Ziel" Dachowski! I appreciate your work :)

Check out the official topic on XSplit's forums and see for yourself all the great feedback people gave me.