Brilliant, Crazy, Cocky

I am reading this book called Brilliant, Crazy, Cocky ... by Sarah Lacy of Techcrunch for the past couple days. It's a great read if you are interested in knowing about the economy of developing countries, how the entrepreneurs there think and act and how they are different from developed countries (mainly USA and silicon valley in particular), the history and evolution of the technological companies there.

I just finished reading the chapters on Israel and China. I didn't know much about Israel's tech scene, so it was kind of good. The chapter on China was really surprising. From outside, you would think of China as this communist country where people don't have any freedom and all the tech companies out there are pure copycats of Silicon Valley. From what I read, there are lot more companies who are solving China's problem. Another thing which was interesting to know is Entrepreuners there are going after more customers, more users (basically landgrab) vs quality and differentiation. One thing which helps is to have good infrastructure and China has and is continuing to build it. If you want to know how things work in China from inside, this book is a good read.

I just started reading the chapter about India and I can already sense the author not too happy with things there. I am looking forward to reading the rest because I am from India and pretty sure I can relate to some things and completely disagree with lot of her viewpoints. Will keep you posted!

 

Mahesh Murthy

Code Retreat

I was at an Obtiva sponsored code retreat today (Thanks John for letting me know about it). It is basically a day long event where you are given a problem and you implement a solution in any language. You do it by pairing with other programmers. Each session lasts 45 minutes after which you discard the code and restart again by pairing with another programmer. So, you are implementing a solution over and over again but with different programmers and in different languages. It really was a fantastic experience. We worked on implementing Conway's Game of Life. You can read more about Code Retreat here. If you come across anything like it, I highly encourage you to attend it.

One of the big things I learned was TDD is not as hard/annoying as I thought it is. I always lose patience and just implement my code and then write tests for it. But really once you put in a little extra effort and switch to TDD mode, you will gain a lot out of it. I am by no means saying I am a hard core TDD guy now and I don't want to be one of those. In some cases, you just have to ditch it and go do your own thing. But for lot of cases, it really helps you design your system better and build a better, stable, easily testable, maintable piece of software.

That was the lesson of the day.

Cheers,
Mahesh

Selecting Elements

[Update: All the html and formatting turned into a total cluster fuck once I saved it. I am too lazy to fix it now. I will figure this out for next week :P]

Last week I started learning jQuery and had decided to do one chapter a week. So, here is the second installment. This week is all about selecting elements in the dom. Last week I just read through the entire chapter and then started blogging and it didn't really turn out great. This week I am writing as I read. We'll see how this goes. The pattern for this post is going to be in the form of a series of Problems and Solutions. You can test your jquery skills by coming up with a solution without looking at the answer. So, here we go.

Problem: 

Category

<ul id="nav">

<li>Anchor 1</li>

<li>Anchor 2</li>

<li><span>Anchor 3</span></li>

 </ul>

 

Given this html, select the anchor elements within each list item.

Solution: jQuery('#nav li > a')

 

Problem: 

<div id="content">

<h1>Main title</h1>

<h2>Section title</h2>

<p>Some content...</p>

<h2>Section title</h2>

<p>More content...</p>

</div>


Given the above html, select all h2 which come right after h1.


Solution: jQuery('h1 + h2') - I have to say this is non intuitive.

 

Problem: 

 

<ol>

<li>First item</li>

<li>Second item</li>

<li>Third item</li>

<li>Fourth item</li>

</ol>

From above, select the 3rd <li>. Bonus, select <li> at even indices. 

 

Solution: jQuery('ol li:first')

Bonus: jQuery('ol li:even')

 

Problem:

<li>

    <input type="text" />

    <input type="password" />

    <input type="button">Enter</input>

    <input type="text" />

 

</li>

 

Select elements of type "text"

Solution: jQuery(':text')

Those were very basic ways in which you can select elements in a dom. Once things get complicated, the best thing to use is the filter method.

Ex:

 

  <li style="width:100px">First item</li>

  <li style="width:200px">Second item</li>

  <li style="width:300px">Third item</li>

  <li style="width:400px">Fourth item</li>

 

 

 

Let's say you want to select li whose width is between 150 and 350. Here is what you would do:

jQuery('li').filter(function() {

width = jQuery(this).width();

return width > 150 && width < 350;

});

filter basically applies the function to every element returned by the selector (in this case jQuery('div)) and returns a new list whose elements match the function.

Now, let's say you want to use this in multiple places. There is a slick way to create a new custom filter that selects div with the width between 150 and 350 and use it wherever you want. So, to do that,

jQuery.expr[':'].customWidth = function(elem, index, match) {

 

width = jQuery(elem).width();

return width > 150 && width < 350;

 

};

Now, once you have defined that, you can use it like you use inbuilt filters like :text, :radio etc.

So,

jQuery('li:customWidth') should return 2 elements whose width is 200 and 300.

 

That's it for the day. I think I will remember lot more from this week because I was writing, playing with it as I was reading. See you next week with third chapter! 

Plus!

I was scratching my head thinking about what to write today and then I saw a tweet from Jeff that google plus is now open to everyone with a google account. The entire blogosphere for the past week has been flooded with Google plus, so was time to check it out.

Overall I like it and I am pretty sure I will use it regularly going forward.

Things I liked are:

- Circles: Facebook has groups you can create but it has all this bullshit like open group, secret group and so on. Circles is just group. Nice and simple. It is almost like secret group except that no one other than you knows what group your friends are in and the stream/feed activity just shows up on their wall seamlessly based on what they can see/not see. Majority of people probably don't care for this but I am not very "social", I don't like to post everything to everyone. So, circles really comes in handy for me.

- Hangout: Finally I can hangout with my family and do video chatting. There was no good way to do 3 way video chat and now that problem is solved!

- UI: Pretty slick. They have done a neat job on the look and feel.

Things I didn't like are:

- Facebook does a really good job of showing lot of posts in a page. I can see may be 4 or 5 posts before having to scroll down in plus. And there are too many things on the plus page. Wish I could just hide lot of those little widgets lying around. 

That was my initial reaction playing around for 20 mins with it.

See you tomorrow!

Mahesh

ps: I have 9 circles already :-)

3 vim tips of the day

Three vim commands I learned and have started using from today:

1. Marker: m<char> and `<char> to mark a line and jump back to it from anywhere else.

Ex: Open a file in vim and then go to some line and type mt. Go to some other line and then type `t and you are back to the line which you had marked before. It comes in handy sometimes when I am jumping back and forth between methods in a file.

2. retab: This is courtesy of John today in the chat room. It converts all tabs into spaces.

3. bwipeout: Sometimes I have so many files in my buffer that I end up closing the entire vim session and starting up again. That kinda sucks. Well, now I know I can just do a :ls and :bwipeout and specify bunch of files which I want to remove from my buffer.

Ex: Open a bunch of files in vim and do :ls. You will see a list of files in your buffer. :bwipeout <file1> <file2> etc to delete those files.

Every time I see blog posts and articles listing all the cool vim commands, it gets overwhelming for me and I don't end up picking up any. So, I will just add commands slowly one by one to my repository.

jquery

I am not a big fan of doing anything related to UI when I am writing a piece of software. Whenever I have to do some html, javascript work I spend half my energy convincing myself I have to do it before even seeing how easy/hard it is. Atleast at work, html part is taken care of by our UI expert. So, that leaves javascript. For javascript, we use jquery mostly and I can get around and understand the code and do small things. However, I still don't have a good handle on it. So, I decided to finally open my jquery cookbook which was languishing in my ebook folder for over a year now and start reading it.

Just so I don't lose interest half way through the book, I decided I will just do one chapter a week. So, expect a blog post on jquery once a week (Initially I thought I should go all out, blog everyday about it but just at the right time, the cells in my cerebral cortex activated and sent a pulse with the packets containing the history of how I work and that sealed the deal. So, once a week it is).

Anyway, this is what I learned/revised/refreshed from first chapter of jquery today.

1. jQuery is a library which makes interaction between html dom and javascript easy. It is like a wrapper. When you perform any action against dom element(s), your response object is always wrapped with a set of jquery methods. It's a very simple concept. What makes it powerful is that the wrapper has a rich set of methods you can use to manipulate dom elements in different ways and it is extensible. i.e, you can write plugins to add stuff to the wrapper.

2. Avoiding un- necessary DOM traversing is a critical part of page performance enhancements. When- ever possible, reuse or cache a set of selected DOM elements.

3. Do you know the difference between window.onload and jquery(document).ready? I have used both of them so many times but had never bothered to know what exactly the difference is.

window.onload in your javascript basically blocks until the entire contents of the page is loaded. That includes all dom elements and assets. However, jquery(document).ready executes right after dom elements are loaded. Make a difference in total page load times.

The first chapter went over how the jQuery APIs are organized and different ways to fetch, filter, manipulate dom elements. May be next week I will come up with a project and then really use those functions so that it sticks in my mind. We'll see.

 

That's it for today. Happy Independence day to my American friends!

 

Cheers,
Mahesh Murthy

Restart

This blog has been collecting dust for 6 months now. Time for some vacuum cleaning! I have been meaning to restart blogging since I have come back from my adventure but somehow hasn't happened. Anyway, this is my nth attempt at getting back to regular blogging. If I can't think of anything to blog, I will blog some random garbage. That is the plan this time around :P

Today, I was searching for something in rails and came across a couple of handy array extension methods in active_support. Thought I will write it here so that I will remember it next time I need something like this and also if you didn't know about it, now you know!

The extension I am talking about is grouping.rb. There are two methods there:

1. in_groups_of

  #   %w(1 2 3).in_groups_of(2, '&nbsp;') {|group| p group}
  #   ["1", "2"]
  #   ["3", "&nbsp;"]

2. in_groups

  #   %w(1 2 3 4 5 6 7).in_groups(3, '&nbsp;') {|group| p group}
  #   ["1", "2", "3"]
  #   ["4", "5", "&nbsp;"]
  #   ["6", "7", "&nbsp;"]

Take a look at the implementation, it is pretty straightforward. It is just a wrapper around ruby array slice method with some simple math to fill the gaps. That was the lesson of the day!

Adios,
Mahesh Murthy