Wednesday, January 06, 2010

Anti Asserts

I couldn't help but make this name up after I thought about it in that sense.

Putting Asserts at various points in your code is a good practice. With Asserts you express your assumptions about the state of your program at the particular point in execution. When the program runs, it verifies if your assumptions actually hold or not. Asserts are the simplest form of a test case.

Consequently, what I call Anti Assert is bad. Where Assert gives you an opportunity to test your assumption at certain point in code, Anti Assert will rob you off such opportunities.

Anti Asserts are more dangerous, because we introduce them unknowingly. Where Assert is one definite line of code, Anti Assert is essentially invisible or hidden under the guise of routine code logic.

So what is Anti Assert?

Simply put - it is the unnecessary checks that we put in our code, and more specifically the superfluous NULL checks.

Why call them Anti Assert?

This is how we use asserts.

// ...

TheObject obj = x.findObject();

assert(obj != null);

obj.performY();

// ...

In the above code, if under some scenario obj turns out to be null, then we will very likely learn about it during early development/testing.

And this is what I call anti-assert.

// ...

TheObject obj = x.findObject();

if(obj != null) obj.performY();

// ...

In this code we not only omitted the assert, but we did a null check before calling a method on obj. Doing a null-check before de-referencing is considered a smart thing (especially if it is the C/C++ code), but do you realize that this only postpones the disaster?

By doing a null check we avoided a failure while calling performY, but somewhere down the code path obj might still get dereferenced, and in worst case that will happen only on customer site.

The tendency to put such cautionary null checks is justifiable in C/C++ code. In the native code a NULL pointer dereferencing will lead to segmentation fault, and unless it happens on a debug build the developer won't know what went wrong. So the coders working in C/C++ or having a prior background in those languages will tend to introduce Anti-Asserts.

In Java or other managed languages, if a pointer turns out to be null, the worst that can happen is a program crash with well-defined stack trace. With proper mechanisms, such crash reports can be easily retrieved even from customer sites. So there is no justification for introducing Anti-asserts when coding in managed languages.

One way to avoid introducing Anti-asserts is understanding the API contract carefully. If an API function is not supposed to return null, then we shouldn't put a check for it. That way, if it indeed returns null, it will be exposed by a crash (with some luck during early testing). Or the best practice would be to put an assert, with an arrangement to remove it from production code. This thread on Stack overflow gives ample information on this subject.

This is not something new, but just a re-emphasis of what we already observe but fail to recognize.

Sunday, January 03, 2010

Bono's top ten list

I just came across this op-ed piece by Bono for NY Times via my Twitter stream. It has so many quotable quotes that a simple retweet won't justify how much I enjoyed it. I would dare say this list is most interesting of the top-ten lists I have read during this turn of the decade. It includes topics from Social-Political solutions in Africa to Teleportation to Piracy to sexy cars.

Here are my favorite quotes, but I would hate it if you read them out of context, so read the full article too if you have time.
It hurts me to say this about democracy (and I know because my band is one), but rarely does majority rule produce something of beauty.
- On designing sexy cars. This has been said many times, but still many don't get it.
The only thing protecting the movie and TV industries from the fate that has befallen music and indeed the newspaper business is the size of the files.
- On piracy. I don't fully agree with the rest of Bono's comments in the same section though. 
By this accounting, your average Ethiopian can sell her underpolluting ways (people in Ethiopia emit about 0.1 ton of carbon a year) to the average American (about 20 tons a year) and use the proceeds to deal with the effects of climate change (like drought), educate her kids and send them to university
- On global warming. This is one fresh thought I've heard on the matter.
... if there’s a breakthrough in the Mideast peace process. The idea is an arts festival that celebrates the origin of the three Abrahamic religions: Judaism, Christianity and Islam. Every year it could be held in a different location; Jerusalem would obviously be the best place to start.
- On peace solutions in Middle east. Again very fresh thought.

Saturday, January 02, 2010

Make Android speak your Twits with "TwitSpeak"

Happy New Year All.

I thought what could be a better way to wish Happy New Year than to release a new Android app ... and make the source open.


So here is a fun little app TwitSpeak.

Just tap the icon and it will read your latest twit to you. How's that? Give it a shot.

And as I said, the complete source code of the app is out in the open, under Android's favorite licese APL 2.0.

http://code.google.com/p/twitspeak

(Tip for developers: If you were looking forward to integrating Twitter in your app and found the OAuth tricky to implement, you have ready to use code now. and it is under APL 2.0. Go, grab it).

You can get it from Android Market or get the apk.

Tuesday, December 29, 2009

Talking Twitter client

So I learnt this afternoon about this Linux utility called festival. It's a text to speech conversion program. Running it is as simple as

echo "Hello world" | festival --tts

Moreover, installing it on Fedora is as easy as

sudo yum install -y festival

After that, a bit of a bash and a bit of a python and I had a twit-to-speech utility running.

The code is simply this much:

#!/bin/bash

TWITTERURL="http://twitter.com/statuses/friends_timeline.json"
JSON="/tmp/twittline.json"
SPEECH="/tmp/twt.message"
PYCODE="/tmp/twt2speech.py"

read -p "Username: " TUSER && \
read -sp "Password: " TPASS && \
curl -s -u $TUSER:$TPASS $TWITTERURL > $JSON

cat > $PYCODE << "EOF"
import json
import sys
import re
urlp="(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]"
twits = json.load(open(sys.argv[1]))
for twit in twits:
    text = twit['user']['name']+' says: '+twit['text']
    text = re.sub(urlp, '', text) 
    print text
EOF

python $PYCODE $JSON > $SPEECH

while read line
do
notify-send -t 15000 "$line"
echo $line | festival --tts
sleep 1
done < $SPEECH

echo "THE END" | festival --tts

code syntax highlighting by GVIM

The above script will ask your twitter credentials, fetch latest 20 twits in friends' timeline, save into a JSON file. A short python script parses the JSON, extracts twit text and user's name from it and outputs in a sanitized format (it removes URLs, because there is no use hearing them).

The sanitized output is  saved in another text file, which is piped one line at a time to festival. In case the speech is not clear, it also shows the text in a pop-up using notify-send.

A full script with some error checking can be found here.

Saturday, December 26, 2009

ReaderScope 1.5 - quicker, better, prettier

ReaderScope 1.5 is ready for the holidays. With the latest improvements it gives the best Google Reader experience out there for your Android phone.

ReaderScope now uses non-blocking UI and the items are now loaded progressively. What does that mean? See it yourself.



As you know, the app is free in various markets. The themes (including the 'Santa' theme in above video) are part of a Theme Pack, which can be purchased for a ONLY $2.49 $3.49

Big thanks to the beta testers who helped testing the unstable 1.5 builds for over a week. Thank you guys!

Also if you like ReaderScope, vote for it at Android Arcade Facebook app.