brandontreb.com Tips And Resources For Software Consultants

Programming Tip Of The Day #1 – Ternary Operator

So, I though I’d start this series called Programming Tip Of The Day to write about useful things I come across in programming. Both to educate my readers and as a personal archive of ideas and tips.

I will kick it off today with a quick rant about the ternary operator. I <3 the ternary operator. It’s quick, efficient and saves a lot of ugly code.

For those of you who don’t know, the ternary operator is made up of 3 elements: The condition and two results. It is of the form:

(condition) ? (result if true) : (result if false);

This is much nicer than an if statement. So here is a brief example about how a ternary operator can replace an if-statement.

if-statement

<code class=’c’>if(isSnowing) {
    iMustBe = "cold";
} else {
    iMustBe = "warm";
}</code>

Same thing using ternary

<code class=’c’>iMustBe = isSnowing ? "cold" : "warm";</code>

That is so much easier to read (IMHO). You can even do clever things in printing. Here is a small example in PHP for using the ternary operator when doing an echo.

<code class=’php’><?php
  echo "I am a ".((height > 72) ? "tall" : "short")." person!";
?></code>

Most languages support the ternary operator. Check out this wiki page if you want more info.

Happy programming!

Mac OSX Tip: Hotkey To Hide/Show the Dock

While mashing the keyboard (as I often do when debuggin poorly written outsourced code), I stumbled upon a very useful OSX hotkey.

⌘-option-d

This command will cause the Dock to go in and out of auto-hide mode.

Not the most useful, but very helpful to geeks like me who rearrange their desktop every day.

Increase Your Twitter Following Using Your WordPress Blog

twitter_bird

Download TwitPop Now

TwitPop is a WordPress plugin I wrote with one goal…To make you more popular on Twitter. There are sites that spring up from time to time claiming to get you more followers on Twitter if you follow X amount of people on the follow train. Well, now you can create your own Twitter train on your wordpress blog and really get more followers.

The best part is, you add your username in the admin panel and EVERYONE FOLLOWS YOU! Think of the possibilities… You could be a Twitlebrity.

To add to the excitement, everyone that uses your TwitPop plugin will Tweet a link back to your blog. This promotes your blog as well as your Twitter account. Check out how TwitPop works below.

Instructions

Log in to your Twitter account below. You will automatically follow the people that have visited this page before you (no more than 20).

Then, your Twitter username will be added to the list and you will be followed by the next 20 people to use this plugin.

[twitpop]

Download TwitPop Now

First learn computer science and all the theory. Next develop a programming style. Then forget all that and just hack.

George Carrette [1990].

PHP Class To Interface With Many URL Shortening Services

With the explosion of Twitter and such services, the need to have long URLs shortened has drastically increased. Naturally, many services to shorten URLs have sprung up as well.

Most of these services (that are worth using), have some sort of API that will allow developers to send a long URL to them, have it shortened and then returned. This type of service allows other developers to write applications that have the ability to shorten URLs right inside of them.

Recently, I had to write a PHP class to work with URL shorteners. So, I made it support multiple shortening services and have decided to share it with you.

The class is called URLShortener.class.php and it supports the following shortening services:

  • j.mp (bit.ly)
  • tr.im
  • TinyURL
  • is.gd
  • u.nu
  • Linkyy
  • Your own custom shortening service

Here are some examples of how to use the class in your own applications:

<code class=’php’><?php
     /* Basic: */
     $s = new URLShortener(‘tr.im’);
     echo $s->shorten("http://brandontreb.com");
     
     /* Using API Key and Login */
     $s = new URLShortener(‘j.mp’);
     $s->login = ‘brandontreb’;
     $s->APIKey = ‘R_2a413ebd15254a72b500ec2ce83f982d’;
     echo $s->shorten("http://brandontreb.com");
     
     /* Custom URL */
     // Just make sure you put %s and it will be 
     // replaced with the long URL
     $s = new URLShortener(‘custom’,
        ‘http://b1t.me/api/shorten.xml/%s’);
     echo $s->shorten("http://brandontreb.com",false);
?></code>

You may download my URLShortener PHP class here.

Feel free to leave any questions or comments in the comments section.