Wednesday, August 29, 2012

Why I like garbage collection

One of my most favorite features of java is the garbage collection. Being able to create objects on a whim and then simply forgetting about them is the nicest way to program. Gone are the times when you need to clean up the object. Reference counting becomes a thing of the past. Best of all, no more segmentation faults! (this helps me sleep well at night)

Garbage collection means less time on finding bugs and more time working on the internal logic of your application. It means that more code can be completed in less time. Imagine having a person who picks up all the litter you toss on the ground (don't do this) as you walk through the park, the playground, or maybe across the street.

Alas, it's not all perfect. Tight loops and objects that never seem to go out of scope can and will cause memory problems as well. Garbage collection is also an additional overhead for the application, but that is a reasonable trade-off if you don't want leaky code that will grind the entire system to a halt much faster.

For high level programming there's nothing like GC. It's a shame I mainly work on C-based languages nowadays...

Thursday, August 23, 2012

5 years of Yawn4LifeIng

That's right. On this day 5 years ago, I created Y4L. It was a much different look and feel back then. As I think back to when it was first created, it seemed almost dark and hidden. Now it's all bright and cheerful... sorta.

Anyway, if you've been reading this blog ever since the start. Thank you. And if you just started reading and discovered that this place has been hanging around since the last decade (holy crap), remember to subscribe. The terrorists will win if you don't.

Monday, August 20, 2012

Sqlite and a quick trick

Sometimes you need to make SQL queries that don't seem to have your usual columns and rows. The other day I was working on one such example. This time, it was with C and Sqlite.
Select Count(*) FROM sometable;
I had this SQL query that would get me the number of rows in a table, but I realized that the c interface had no obvious way of getting it. After taking a short lunch break, I realized that the output on the command line interface looked remarkably similar to having only one column with one row.

So that's what I did.

Here's the code snippet:

NSString * query = @"SELECT Count(*) from essaystore;";
    sqlite3_stmt * statement;
    if (sqlite3_prepare_v2(database, [query UTF8String], -1, &statement, nil) == SQLITE_OK) {
        sqlite3_step(statement);
        int row = sqlite3_column_int(statement, 0); // it's technically a 'column'
        sqlite3_finalize(statement);
        return row;
    }

Not really a big deal, but it didn't really make any sense to me. I tried it out and amazingly, it worked.

Wednesday, August 8, 2012

N days ago Objective-C

What app could I possibly be making?
As I was working on a new iOS app, I realized that the best way to display the information that I had stored as a timestamp was to create a function that could change the seconds from 1970 to more informative messages like "3 seconds ago" or "2 days ago"

After searching the web for a while (on Bing, because I live in the Seattle area) I found this website with a very helpful snippet of code to create what I wanted. The problem was that it was in PHP! Oh dear, I'm not working on PHP, it looks like I will have to keep on searching...

Sadly after searching, I still couldn't find anything relating to objective c and this time ago function. I decided that it would be faster for me to just simply port the code to Objective-C.

Then I remembered my promise last Monday. So here is the first piece of code you will see from me on this blog. (Yes, I can in fact code)

+ (NSString *)ago:(int)time {
    char * periods[] = {"second", "minute", "hour", "day", "week", "month", "year", "decade"};
    double lengths[] = { 60, 60, 24, 7, 4.35, 12, 10};
    int now = [NSDate timeIntervalSinceReferenceDate] + NSTimeIntervalSince1970;
    double difference = now - time;
    int j;
    for(j = 0; difference >= lengths[j] && j < 6; j++) {
        difference /= lengths[j];
    }
   
    difference = round(difference);
   
    NSString * result = [NSString stringWithUTF8String:periods[j]];;
   
    if (difference != 1) {
        result = [result stringByAppendingString:@"s"];
    }
    return [NSString stringWithFormat:@"%d %@ ago", (int)difference, result];
}
The class method takes an int for the seconds since 1970. It returns an NSString but can easily be adapted to return a C string if you so incline to do so. Also, if you're one of the strange few people who do high-level programming with pure C, you would only need to change the NSDate to the current unix timestamp in C in addition to eliminating the NSString mentioned earlier.

Yup that's it! If you want this method available to other classes, I advise you declare it in the header.

Monday, August 6, 2012

I'm going to try blogging more

It's been pretty hard to keep to such a hectic life. It always seems like the more stuff I get done, the more stuff there is left to be done.

No matter what, I'm going to start blogging more about some of the work I do creating mobile apps. There will be screen shots, code snippets, and maybe entire projects. I'll also show how to work with the web and cloud as well. Integrating mobile apps with the cloud is pretty much what the future of computing is going to be all about. Imagine your entire home, controlled from anywhere in the world with your mobile phone. All the food you have in your refrigerator and pantry will be tracked, foregoing having to make a shopping list. Information gathered from all of the gadgets in your house will automatically generate a list of all the food you need to buy. The lights will turn on once you enter a room. Your sofa will automatically adjust to your own comfort. The TV will automatically turn on at the time of your favorite programming and automatically check your email during the commercial breaks. With the entire "Smart Home" there will never be the need to remember to do anything anymore. Your home will take care of it all for you. It will even clean the house when you are not at home during the day!

To be pragmatic, this kind of living is not that far off. We have the technology to do it. All we need now is the will power and the initiative to implement this in a low-cost ubiquitous way for all people to have. In addition, the public would need to be educated that this is not going to be the end of the world and that they have nothing to fear.

Wednesday, August 1, 2012

I guess stuff goes on

It really does suck that it appears none of the apps I planned to release before August are actually going to be released. My life has become more unpredictable than it has been. It appears that I just can't handle the constant 'surprises' that knock all of my planing and scheduling off track. Everything becomes de-prioritized to beat on the dead-horse. Had I the option to run the world my way, this would completely be different.

There would be total confidence that what is currently being done is of the highest priority. Random crap that occurs will be completely downplayed in an effort to preserve a stable status quo. Hey, there's no problem unless you say that there is one.