My perspective

I’m not planning to write anything in length or in detail. Just wanted to share my high level views and ideas on every day things I come across. I hope these blogs will serve as tips for specific problems and as observations. Thanks for the space, Tumblr!

Crazy!   Silicon valley for you!

kurtvarner:

In two days, I will leave my cushy life in Los Angeles to live from my car in Silicon Valley. Yep, that’s right. From my car. It’s going to be one of the most adventurous things I’ve ever done, and will be a challenge to say the least.

Why am I doing it? Trust me, a lot of serious thought went…

Some of the most frequent methods you might be using if you are a Web Dev. - .live(), .bind() and .delegate(). I’ve used live() and bind() quite frequently but never understood the performance implications of these methods. This blog nails the Pros. and Cons. of using them.

http://www.elijahmanor.com/2012/02/differences-between-jquery-bind-vs-live.html

Happy Coding!

bijan:

When I see startups that only offer FB Connect as the singular sign in option, I always get squeamish.

I tell founders they should support FB Connect but they also support Twitter sign-in as well as giving users an option to sign in the good old fashioned way : user id & password.

Sometimes…

iheartchaos:

There’s all sorts of magic to be had with numbers, and many mathematicians have made entire careers in finding these little tricks that are mostly useless, but fun anyway. Unfortunately, a lot of calculators are going to truncate the results of this trick, but if you manage to get a hold of…

I have always been under an impression that if I wanted to implement a cache I can always use WeakHashMap! No. WeakHashMap is not a cache. Simply because it uses weak references for keys and not for its values(that you might want to cache). WeakHashMap is an excellent choice if you want to store meta data about an object that you dont have control to change it.

For e.g., WeakHashMap<Socket, countOfOpenClients> or WeakHashMap<Thread, memoryFootPrint>

But if you want to implement a cache, you might want to consider a structure like Map<K,SoftReference< V > >. Since V is referred by a soft reference(and not by any strong reference), V will be garbage collected under low memory situations. Cache’s get(K) will return null and so the cache might want to reload the value from the disk and store a soft reference to it.

If you dont want an infinite capacity cache, you can always have a maxCapacity and also implement remove() method to decide which candidate has to be removed. You can also heat up an item based on some strategy. One such is  LRU.

Andreessen’s predictions for 2012.

courtenaybird:

“I like to say that the first generation of e-tailers was really good for nerds. Amazon for me is—I love it—it’s like the biggest warehouse superstore of all time. It’s just awesome, and I love wandering up and down the aisles and it’s like, ‘wow, look at that.’ If I do enough searches I can discover anything.

The new generation of e-tailers are much more appealing to normal people—people who like to go the mall, have fun with their friends and try on clothes and compare clothes, and go home and brag to their roommate what they got on sale, and all the rest of it. A lot of new startups are not only very viable but also growing very fast because they provide a very different experience.” 

I had a habit of not using iterators when using/iterating over data structures until today when I was using a SparseArray in Android. Here is an explanation on why everyone should consider using iterators or if you are writing your own data structure you should implement your own iterator.

When using a SparseArray in Java, you can iterate like the following:

private void print(SparseArray sArr) {

    int len = sArr.length;

    for (int i = 0; i < len; i++) {

        Double value = (Double) sArr.fetch(i);

        System.out.println(value);

    }

}

This could be a poor implementation of print function for a sparse array where many slots would just be empty/null/zero. Here is where iterators help us to use the real potential of the data structure. Sparse array normally stores its elements along with the index value. In a linked list based implementation, these both will form a node along with the next pointer pointing to the next available value/index. Look up for values in between these two nodes will return zero or null. This is the most important thing one has to remember regarding how sparse arrays are implemented efficiently. The above code does n’t do any good for us since it does a look up for all the elements. An iterator based usage will look like:

while (iterator.hasNext()) {

    value = (Double) iterator.next()

    System.out.println(value);

}

Iterator pattern is an important design pattern that every data structure developer should implement to enable the users to fully exploit its potential.

Today, I happen to solve a problem that frequently involved a 4-level look up to a different process and get a reference and then use it for good. I stumbled upon this holder pattern that helped me to optimize a lot. Basically, you might have to create a static inner class for your holder. In Java, it could like this,

static class MyHolder {

static final Class_Name sReference = Manager.Stub.Interface.getService(); //IPC

} and then in future, if you want to query again, you can use MyHolder.sReference.query()

While reading further, I found a great demo from Android dev page http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List14.html

So, look for frequently called external api in your code. Cache their results in your class if you can.

On a side note, I have also seen some programmers writing for-loops like,

for(int index=0; index<bigArray.size(); index++) {}

Whenever I code review, I always -1 this line :) I dont like spending o(size-of-bigArray) everytime(sometimes compilers could cache them for you) in the loop. I would use a local variable ‘holding’ the size instead. int len = bigArray.size();

Happy coding!

Great post! Got to try this out.

falicon:

If you’re already using EC2, you’re probably familiar with SSH’ing to your box via Terminal with something like this:

ssh -i your_ec2_pem.pem your_ec2_instance_username_like_ubuntu@your_ec2_host.com

But did you know you can avoid this by creating a ~/.ssh/config file with details like this…