All posts by Abhishek Nagekar

A Visit To The Taj Mahal

Taj Mahal

So in the last week of July, 23rd, on a Saturday, I was wondering what should be done tomorrow, Sunday that is. It was going to be my last weekend in Delhi, and I wanted to make it memorable. I had the option to spend the evening with my colleague, but I also had the option to visit Taj Mahal. I had always admired Taj Mahal, I mean, who doesn’t. This was my chance to visit it. It somehow felt like I’ve earned it.

And visiting Taj Mahal was not just important because I really wanted to see it, but because I was scared to go all alone to Agra to see it. I was really scared to go there, and the entire Saturday afternoon I was thinking, Taj Mahal can be visited anytime, let me just spend some time with my colleagues, for they might not be there always. But deep within, I knew that the only reason I didn’t want to go was because I was scared. I was scared of going all alone to Agra, a foreign city which I knew nothing more than the name itself. Let’s see. If I manage to wake up early, I’ll think of going I said, and slept.

Sunday, 24th July, here is a page from my diary.

Woke up at 6. Plan was to go to Taj Mahal but knew nothing above it, how to go, where to take train (from). Nothing.
Thought ‘let’s go to sleep, will call Sukhpreet and make a plan later. Taj Mahal can be visited anytime.’ But something kept telling me this opportunity won’t knock twice, and years later, you’ll remember this decision. Or you won’t.
Anyway, got ready, (had) bread sandwiches (the cheese ones) and got going. Took metro to Rajiv Chawk and then changed to blue line for Indraprasta. Rickshaw to H. Nizamuddin station and then Mangala express.
Right now sitting in the sleeper boogie with general ticket with earphones in my ears and writing this piece.
All of a sudden, I feel so courageous, so different. This day is going to be awesome, I can already feel it.


Awkward selfie.

By the way, I randomly saw the train standing on the platform ready for departure. I went and sat in a reserved coach with IInd class tickets.

And you can bet it was an awesome day. A great weather in Agra welcomed me, and quickly got along with a great person there. He was the rickshaw driver, whom I paid Rs. 500 for a day’s rent which included

Station -> Hotel (for lunch)
Hotel -> Taj Mahal
Taj Mahal -> Agra Fort
Agra Fort -> Shopping center
Shopping center -> local Hanuman temple
Temple -> another-shopping place
Shopping place -> Agra station

Amazing. Bought a Saree for mom, which was handmade with bamboo threads by the prison workers! Also some petha.

On my way back, did the same thing. Went and sat in the first train I could spot going towards Delhi; in the reserved compartment; with a second class ticket. But not so lucky this time. The TC fined me with Rs. 300, but also gave me the seat, so I guess it went okay. Reached my flat by around 9:30.

All in all, a great one day trip. I feel it isn’t necessary to describe Taj Mahal here, but the experience, the people and the difference of cultures was just amazing. That day, I was so proud of myself, can’t describe it here. Closing this essay with a quote I read somewhere and only believed on that day, Working makes you money, but traveling makes you rich.

Dropping The Master Plan

Back when I was in my first and second year of engineering, I was very optimistic about my future. I liked the idea of starting my own venture, and I really believed that by the end of my graduation, I’ll have something that would sustain me without having to look for a job elsewhere. Believe it or not, I really tried to make that happen. Two tech blogs, numerous startup ideas, bug bounty dreams and a lot of side projects, all for various reasons, but a common denominator was to become self sustained.

But again, that was what I had envisioned. Of course, it didn’t work out as planned. There wasn’t sufficient maturity in me then, and if only everything was that easy, isn’t it? By the first half of third year, I knew things were not going to change. It takes a great deal of talent to create something amazing at such an early stage. I didn’t have it. Suddenly, the belief I had in myself, my hard work and my skills fell apart. I was no longer confident about my future, and I feared failing way too much to be comfortable with that fact. I was vulnerable then.

To add to the agony, people around me were planning things. They were joining GRE and TOEFL classes to get into a great foreign university. They were doing everything that would get them a certificate, and validate their skills in front of someone; Java, Android and what not. I didn’t have a single certificate for any extra curricular. I remember thinking, How am I going to prove to somebody that I know something about anything. I just can’t.. The deterioration of thoughts accelerated even further when people started to ask each other what are they doing after graduation. Job? Masters? I thought they were too few options to be given to just about anybody regarding a career path. Why was no one talking about starting something after college, or maybe preparing to work at some dream company, or perusing research in the field of their liking, or working as a freelancer for whatever intrigues them. Am I missing something obvious here?

The What are your plans after college? questions started hitting me as well. I had to take a decision from the two options that were presented to me. Job or masters. I feared a job, and took masters. M. Tech. from some IIT would look cool, I thought. Started researching about GATE examination, and decided to go for it.

I told my decision to a few friends, and immediately realized that M. Tech. wasn’t the masters everyone was talking about. At least most of them. This masters is M.S. and it is done by going to the United States and studying there. Masters in India is not cool. It is just like Bachelors, in a specialized field. I don’t have a great opinion about the bachelors study in India, and if masters was going to be like that, I am better off not doing it.

So I decided to go for masters. I knew it was going to be very expensive, but quickly learned finance is available. Okay, so if we stretch a bit, I might get into a college in the US, do my masters, work part time to finance my day-to-day expenses, get into a good company, work for a year or two to repay the loan, and then, life is sorted. A better lifestyle, more exposure to good startups and companies, a good salary and everything I would ever ask for. I would be able to give a better lifestyle to my family, fulfill their dreams and more. Now that’s called a plan. More than anything, it felt safe. Plus all the other smart people are doing it so cannot go wrong with this.

By the end of July, I was totally sure about masters. Abhishek and Dhananjay were going to leave for it soon, and I was thinking about the schedule for my GRE and TOEFL. I had convinced mom and dad, and they even had started to talk about the great things I would see and feel a year from now. I would be the first person from my family to fly abroad for higher education. Or fly abroad. Or fly, for that matter. I really liked that feeling. All sunshine and rainbow.

Later in August this year, I spoke with my mentor about this grand plan of mine. I knew for sure he’d be excited. He was not. He started questioning my decision, and I think he realized that me going to the US was more like the only option I saw, rather than a solid goal in itself. We spoke for sometime, and the more I spoke to him, the more I realized that the other over ambitious plans of mine were not at all over ambitious. They were just as possible as the other safe options, only I’ll have to work a little harder to get to them. The extraordinary people I look up to and admire are just ordinary people who did something extraordinary. And given that you only get one shot at life, let’s aim for something similar, something full of uncertainties, something that is fun and something that I actually want to be doing! That was all the motivation I needed to pursue my heart. That conversation opened me up to the numerous possible career options I had as an engineer.

I created a decision matrix to evaluate my options and find out the best one for me. It gave me my end goal. Now I was free to peruse it the way I wanted. It definitely saved me a lot of time and money. But most importantly it gave me back what all of us are taught during our childhoods. The you can do anything if you try spirit. I am glad I took everything I came across with an open mind. I tried my options, and chose what was best for me. Thinking over it again, I think there is risk involved in everything. The safe path was never safe. The current path is full of risks as well. But it is just a matter of what risks I am more comfortable with.

Finally, a great learning experience. Now the question I ask myself, will I ever consider a masters, again? I remember talking to Sukhpreet about the same. He’s the guy you want to bet on, who knows how to get work done and deliver. He had told me there’s an inner calling that helps you to take decisions. Masters is one such decision. He never had that calling. He has learned all that he needed while working. I haven’t had that calling yet. If ever I do, then yes, why not. Until then, I’ll be sticking to self learning, a deeper version of it. Let’s see how it goes! Thank you for reading.

Fun With Infrared Motion Sensor

Very few of the projects I’ve done actually had any real life purpose. The rest were just Can I do it? projects. Here’s one more to that little list. So last year, the fan switch in my room changed its position, from the spot where it was in a one arm distance behind my desktop computer table, to a spot where I had to walk three steps, climb my bed and walk another three steps to reach. Too much effort for something that is required to be done tens of times in a day. Naturally, I didn’t bother to turn it off whenever I left the room, and at times, the fan used to stay running long after I had left, until mom or dad noticed it and turned it off again, just before giving me a nice stare. Something had to be done.

I had this PIR module that I had bought a couple of years ago with a friend. PIR [for Passive Infrared] sensor triggers when a hot body comes in its range, like a human being. I had to make use of it somehow to control the fan in the room. I had to Google just about everything about this project, and slowly I discovered that you cannot just put the AC mains and DC circuits on the same breadboard, and expect them to be nice to each other. As a result, relay was discovered. [The one I have used is a more compact, cheaper version].

So a relay to act as a switch to turn the fan on and off, a PIR sensor to detect me entering the room and something in between to interface the two. Arduino should do it. So next is to code the arduino such that on receiving a Truefrom the PIR, the arduino would close the switch, activating the relay to close the switch of the fan, and stay like that for a couple of minutes. I didn’t want it to go on-off every 3 seconds, hence the timeout.

Next was mounting all of it together. A breadboard would’ve been unsafe, as per the Internet, hence I bought a perfboard for the purpose. I soldered everything to its place, and the final result was something like the following. Oh but first, some code.

The code which was shamelessly ripped off Arduino.cc

Here is how it looked [Heavy images. Patience!]



My desk that beautiful day!

Now Aditya would’ve told you that this project is what electronics people do when they’re like 5. Nevertheless, I was too proud of this. Not just because I could do it, but because I needed it.

So did it work? No. The range of the PIR sensor was a bit too small for the entire room, and I had to literally dance in front of it to trigger it. Solution? Multiple PIRs to cover the entire room. Also, I realized how easily I could just add the tubelight to the same circuit. Just add another relay and connect it to the LDR sensor and set it to trigger when the daylight fell below a certain threshold.

Suddenly, automating stuff in the room seemed a bit too simple, and it really is, even for an electronics novice like myself. The Internet gives you that power. As always, thank you for reading, and pardon any silly technical mistakes that I must’ve made in the post (even better, correct me ;).

College Vs Startup – Working On Projects

First of all, wishing all you fellow Indians a very happy 70th Independence Day. This day is very dear to the hearts of us Indians, and although I enjoy watching the Republic Day parade more, I certainly enjoy seeing the beautiful tricolors all around the place.

I was asked to compile a list of all the differences between working on a project in a startup and doing the same in college. Of course, we have projects every semester the way (some) startups have products, and we are made to follow some guidelines while developing these projects (like planning, documentation and testing methodologies), but there are some subtle differences. Here is a paste of all the points I wrote at that time. I thought it would make for a useful read for some of you. It is about an organization, addressed to a real person and talks about some other people too, if you were wondering about the names involved, and trimmed to only the parts that matter here.

Differences between college and startup projects

  • The most important factor for me were the stakes involved. In college, making a mistake or leaving some edge cases incomplete were quite acceptable since we were the users of the product. Here at Fourthlion, since a lot of people are involved in making a project happen, plus the people we pitch the product to are some high level state officials, the margin of error tolerated is reduced. That actually brings out the best in you, since you have to push your limits in order to accomplish the new quality standards that you never knew existed.
  • The second factor is deadlines. There are deadlines in college projects, but the deadlines aren’t connected to further set of events. Here at Fourthlion, me completing a project on, say Monday is directly linked with the product getting pitched on Tuesday, and that sense of responsibility makes you want to meet those deadlines to avoid wasting other people’s time.
  • Then there are people around you. They know how to make things work. For example, Sukhpreet. I learnt what it is like to be organized, from him. He writes down each and everything in his diary or in a spreadsheet. I bought a diary just to get that habit, and it is already helping. I saw what serious documentation looked like when working on the first project, made by him. I understood how important initial user feedbacks are, which I had never taken ever in my life till now. These are the things Fourthlion taught me even though I had never planned to learn them. This is what I feel is the effect of working with great people. You try to copy them, and acquire their good habits.
  • You get to know how much can be accomplished in a day. In college, if someone asks me how much time will it take to complete this product, I’d say I don’t know. Because there are a lot of different things that a student does apart from the project. Here, I have a fairly good estimate on what I can do in the 8-10 hours of work everyday, and hence I can give you a good estimate of how much time the project/component should take based past estimates.
  • There’s a good deal of pressure working here that I never felt in college. A reason for that could be there’s real people’s real money and time involved. The pressure actually feels bad at first, but just looking a week back, I hardly think I would’ve been able to accomplish whatever I did, if there was no pressure involved.
  • At this startup, the users are real people who just want a beautiful working product and don’t really care about the cutting edge tech stack I use, which wasn’t the case until now. That has given me a new way to think about the code I write, the more user oriented way. And since I am the only person editing and adding code, I feel much connected to the project than otherwise would have, which is not necessarily a good thing in the real world with multiple developers collaborating.
  • About DoNew, I’d honestly admit that I didn’t really see the point of sending updates on what I do, initially. But I like to take leaps of faith when the people involved know much more about how things work than I do. Now when I think, I feel like the updates were more for myself than they were for you and others. I feel I have started to keep a track of my own progress through the updates, which has formed a habit. Just to give you an idea, after writing the update to you, I write myself a personal update in a diary, with things I did wrong and things I can improve upon. It has made a great deal of difference to me personally. And I don’t think I will ever break this habit of tracking my own progress and shortcomings.
  • When I was skeptic about whether I would be able to handle the real world development here at Fourthlion back when I was in Mumbai, you told me to try, build, make mistakes and to believe that you can. That ‘believing that you can’ has been an important part. For example, when I heard about integrating Asana and building a project on top of it that people can use, I was a bit scared. But I made my mind to keep working as much as I could with all dedication and not really worry about the end result. And now, slowly but steadily, it is taking shape of a practical app, which I am really proud of!
  • Lastly, the importance of having a mentor. I didn’t understand it before when I read about people having mentor. I don’t need to say much about this one now!

As always, thank you for reading!

The Project Lumos Experience

This thing started about a year ago, and I didn’t write about it because I haven’t thought about it, or probably didn’t think it was that significant part of me. But today, when I look back, it probably was the best thing I ever did.

Let us go back and see how it all happened, and how it shaped me into becoming a better version of me. It started in the first week of September, 2015, just before our Engineers’ Day celebration. I came to college one day, when Ankit, my buddy, was looking for me. Without me asking, he told me that some senior has come and he was quite unhappy with the preparations. Then he dropped the bomb. “He was looking for our technical head”. I was supposed to be in college by 9 am, and I wasn’t there even by 1. And yes, I was the technical head. Damn!

Okay, I thought, I’ll have to face him. I was scared. One of my biggest disappointments with myself is that I don’t act even though I know things are not going very well. I knew the Engineers’ Day was all fun and no tech, but I didn’t really care at that point. I just preferred to ‘go with the flow’ and mind my own stuff. Maybe that was the reason I was scared. I was scared because I wasn’t taking up the responsibility. I was scared because I still acted like a kid. I was scared because I took a position in the committee that I knew I didn’t deserve.

Anyway, I thought. Let’s face it. I met him. His name was Abhishek. Abhishek Juneja. Oh nice, same name, awesome, I thought. We started the conversation, and he was not that scary anymore. In fact, he was cool. Totally cool. He spoke about things (read technical things) that only 2 other people in the entire college I know, could speak of. Super cool. He knew his stuff well, and he gave us pointers about the things that we could organize in our college like seminars on various topics. I wrote them on a piece of paper, and kept that with me for the next 6 months, not doing a single thing from it. I was never a person who could act, as I said. I always was someone who wants to act, wants to change, wants to achieve, but never does.

The meeting with Abhishek lasted some 2 odd hours. I remember my friends waiting for me on the stairs. They didn’t ask much about it. We went home and delivered a great Engineers’ Day later. That piece of paper in my bag occasionally reminded me of Abhishek, but that was it.

Fast forward to December 2015. Last exam of the fifth semester, I was all in the mood to go home fast and start working on the startup that was getting delayed since it began in August. Cherrylogs. Outside the college, Manas, our GS (short for General Secretary) gave me a quick update. ‘Abhishek wants to talk to you and me.’. Hmm, interesting. ‘Maybe Abhishek is coming to our college for some seminar, like he said’, I thought. The call happened, and Abhishek, along with someone else I don’t remember now (I think it was Prashant), explained to us how our syllabus and technical education is not enough for students passing out to actually work in the industry. It was true, but how could this call help. Then he laid his master plan to make a portal, a knowledge portal where students can find resources to learn anything they wish to.

That idea was, for me, fundamentally flawed. Why would someone who doesn’t bother to Google his/her interests, visit the portal to get links to resources that Google would’ve otherwise easily provided anyways. I kept telling him that. It was not a very positive call, and quite honestly, I just couldn’t understand what Abhishek meant half of the times. Slow grasper, I am.

At the back of my head, I was trying to figure out what is this guy’s motive. Has he heard about Google Adsense recently and got real excited? Does he want to shine his CV? Is he hiring for some startup he has just started? What can it be?

We spoke for sometime, and the explanations from Abhishek were not helping me understand the gist of the project. At the end, it was decided that with or without us, they are going to build the portal, and that would be it. I think we (me and Abhishek) had some more calls after that. I told him that I have to visit my native place (which I was visiting after a long gap of 2 years, first time after getting into Engineering), where I wouldn’t be able to work, and even after that I had my startup, Cherrylogs, to focus on. That was rude, selfish and arrogant. But then, I just didn’t understand the point of making that portal.

Fast forward to first week of January 2016. I am near a beach in someplace, Karnataka, enjoying the holidays, and I get a call from Abhishek. He expected me to get home by now, but few more days is what I tell him. He asks me again, would you work and help us build this thing. I reply in negative, same reasons repeated again. The call drops due to some reception problems, and then no more calls are made after that.

I come back home, start usual college, and around mid January, I am told that a ‘seminar is going to be held by our seniors’. Cool. ‘It is Abhishek Juneja,…It is on 23rd’. Oh damn. I had planned for a trek to Sandhan Valley on the same day. I wouldn’t be able to make it. It was publicized by our committee, and I, a member, wasn’t available on that day. I felt bad, but then forgot about it. The seminar went well, and I heard about it from people. So apparently Abhishek had built the portal all by himself, managing his office work at the same time. Good for them, I thought.

Days went by, and I had completely forgotten about the project. I had seen the website Abhishek made, and it was good. Not much to it, looked fairly static. Sometime in mid February, Dhananjay calls me out of my classroom for a conversation.

If there is one person I respect the most in my college, it is Dhananjay. He’s simple, friendly, thoughtful, humble, helpful and technically fluent enough that we had great conversations with each other without losing out on what the other guy is saying. This was particularly important because there are just two people in the entire college with whom I can have a heart to heart technical talk without dumbing down on the details. And he was good at teaching. Many people are experts in their fields. But very few can share their knowledge in a way people learn and enjoy learning. Dhananjay is one such guy.

I go out to speak to him, and he asks me about Project Lumos and the conversations I had with Abhishek Juneja. Told him everything I remembered of. He then explained me certain things, and although I don’t remember what are the things that he said, what I do remember is that the chat with him gave me the feeling that this is something important and I have to be a part of it. I remember telling him that doing it all alone won’t be feasible for me, to which he said, ‘Don’t worry. We’ll form a team’. And that was it. Single most value adding decision that I had taken for myself in a while. If that seemed like an overstatement, continue reading.

The next couple of weeks were team creation, planning and a lot of things which I don’t need to get into the details of. It was fun. We had weekly calls to report the status to Abhishek. He was the Project Manager for us. Dhananjay was good at conveying the messages from Abhishek to the team of 15 odd enthusiastic people that we formed. We also started weekly sessions on random technical topics to talk on in front of the team, so that they pick up faster. All in all, I was enjoying the experience. It was also due to Project Lumos that I can speak better in public. Seeing Abhishek and Dhananjay do it gave me that confidence needed. The way to teach is also something I’ll credit Dhananjay for teaching me. He is probably the best teacher I’ve known.

During this period, we had several meetings with the faculty for discussions over computer lab approvals, certificates for students participating and other stuff. It was during this time that I got to know Dhananjay and Abhishek closely. I always had a guilt that I don’t know my seniors well. Especially with someone like Dhananjay as a senior, I felt that I should talk to them more often, interact with them, learn from their experiences and more. I never actually did that, and I always felt bad that I didn’t know any of the seniors personally. That changed with this project. I got to know Dhananjay and Abhishek well, and that was a proud feeling for me. Yes, I know two of the best seniors one can get, and I am very lucky for that.

With the help of Dhananjay, we got in touch with Rahul Kulkarni, who later became my mentor for learning and implementing Data Science and who pushed me into this internship in Delhi at Fourthlion. The influence Rahul Kulkarni has on me is not something I can put into words here. I have learned a lot from him, during these days and later when I’ll go for my internship.

Towards the end of the semester, some more people joined in, but most never actually work with us or joined us in any sessions. Exams started and we all partially disconnected to prepare for exams. My last exam was on 26th May, while the second year folks had their last exam on 10th June I suppose. It was only after that, that any actual work was to begin. But then, some things happened and I came to Delhi for the internship. Ten of the students, along with Abhishek and Dhananjay went on to build a knowledge portal that went fairly well, considering that the students working on it were building their first project.

Today, 25th of July. Four more days to the end of my internship. I am thinking about the number of things I have learned in the last six months. Insane. I have learned how to talk to people, how to talk in public, how to teach, how to listen, how to manage time, energy and utilize it efficiently, explored widest of the technology domains, created and deployed some good applications, got a state’s chief minister see the application I made, visited Agra, Taj Mahal, traveled in an aeroplane, made numerous great contacts at Fourthlion, met Sukhpreet who is one of the best individuals I have had the fortune of knowing. Damn! All of this, each and every one of it was because I agreed to work for Project Lumos with Abhishek and Dhananjay. Is it fair to say now, and in no way an exaggeration, that agreeing to it was probably one of the best decisions I had ever taken for myself, if not the best.

It is quite late, but it is now that I have understood Abhishek well. He is one of the jewels that is yet to be discovered by someone. He has had his past, but the things that he has achieved ever since, amazing. He is honest to the core, straightforward and never thinks once before appreciating or criticizing someone. He rather undervalues himself, for he thinks he has something less than that what most people do. The fact is, he is far richer in his attitude and character than most people I know, helpful, hardworking and selfless. In the end, I think this is what matters. Skilled human-robots are dime a dozen these days. Attitude is what separates the individual from the herd, and he’s kind of a guy you see and say, ‘Dude, he’s going to make it big in life’. Yes, exactly that kind of a guy.

I need not say more, for I have poured my thoughts into this piece. This came out because of an email that Abhishek sent out on Saturday, and I kept thinking, this guy has no damn clue how much of an influence he has on our lives. So Abhishek, and Dhananjay, this one’s for you. If you ever read this, know that you are SUPER AWESOME and SUPER COOL! Your fan forever, Abhishek.

Sixth Semester And Then

I intended to write this long ago, at least a month ago. But then, I am doing it now. Even now, to be honest, I am writing this to just to save my per-month-an-article-streak. So here I am, sixth semester and then.

Semester 6, especially the end of it was too much fun. For one, my friend Aditya had returned, two, I got an interesting mentorship under which I had started to learn data science, and then, the exams were really good. Then there was Project Lumos, something to look forward to after the exams. All set. But life took a sharp turn, when on May 25th, I was offered an internship at Fourthlion by Rahul sir, my mentor for the data science learning. It was a shock, a good one, and I replied in affirmative after talking to my parents, friends and my project lumos buddies.

The next two days came and went like anything. Bought a couple of new tshirts and other essential things. Got the room set for the next two months, got flight ticket and I was all ready to launch. It was very, very exciting. I have not travelled a lot, and going to a new city, like Delhi, in an aeroplane for the very first time was a dream, coming true. I left on 29th, reached my room at 8 in the evening. It was all good.

The next one month proved to be one of the best of my life. I have enjoyed a lot, learnt a lot, interacted with people a lot, made a lot of new friends, and built some really cool products. I just couldn’t have thought all of this when I was in my hometown.

I am exactly one month into this internship, and one more to go. I am looking forward to more fun, more work and more amazing people to meet in this one. Also, I will publish all that I have written for myself in this month here, because there are a lot of things worth sharing, good and bad experiences, differences between working in a startup and in college and many more. Countless. Thank you for reading!

_Generic in C And Generic LinkedList Implementation

Tada! Finally a non-Javascript post for those of you who stopped visiting my blog because of my sick Js addiction. So my lost friend Aditya is back, and while talking to him, I got to read a piece of code that he wrote when we were in third semester, last year. The aim was to build a generic container to store different datatypes, in C. So yes, literally storing [21, “abhishek”, 3.142, ‘z’] in a single dynamically allocated linkedlist. That was pretty bold for us then.

For making that happen, he made use of the _Generic macro that C11 introducted for generic type selection. For those unfamiliar with it, I’ll begin with explaining the basics of the macro. For those of you interested in the linkedlist code, scroll down to the bottom of this article. You can always check out the C11 draft [section 6.5.1.1 Generic selection pg #96] (the standard costs around $60 but is essentially the same thing).

The _Generic macro follows the format _Generic(controlling-expression, association-list) where controlling-expression is the expression whose type is to be detected, and association-list is the dictionary of type-expression pairs where type can be any object type and the expression can be any expression, constant or a pointer to any function, pretty much anything that can be evaluated.

To put it simply, compare it to the switch statement for type detection. For example,

#include <stdio.h>
int main() {
  char *teststring = "my char array" // target object
  char *type = _Generic(
    teststring,
    int: "int",
    int*: "int *",
    char: "char",
    char*: "char *",
    float: "float",
    default: "default"
  
  printf("%s", type
  return 0
}

$ gcc main.c
$ ./a.out
char *

The _Generic macro can be wrapped around in a nice looking function call, say detect_type(). Compare this to a (pseudo) switch statement which evaluates the case that matches the switch params

// Pseudocode may resemble Javascript. Reader discretion advised
var someobj = "text here" // target object
var type // to store the typename
type_switch(someobj) {
  case int:
    type = "int"
  case int*:
    type = "int*"
  case char:
    type = "char"
  case char*:
    type = "char*"
  default:
    type = "default"
}
print(type)

So that is it. A compile time type identification in C with a simple macro. We can do pretty amazing stuff with it using pointers to functions. For example, a generic print function, that can take in any type of argument and print it. I have added cases for integers, characters and string, but others can be added just as easily.

That was neat, wasn’t it. Now back to Aditya. He used the same macro to create a dynamically allocated linkedlist. I am pasting the gist here so that you can read his beautiful code.

File linkedlist.c

File linkedlist.h

File main.c

To compile it, run ~$ gcc main.c linkedlist.c -o linkedlist on any machine with GCC installed (I’ve tested it on version >4.9). Run it with ~$ ./linkedlist and you should see the different elements that we’ve added printed on the standard out. You may also pack the linkedlist into a statically linked library, by running.

~$ gcc -c linkedlist.c -o linkedlist.o
~$ ar rcs linkedlist.a linkedlist.o
~$ gcc main.c linkedlist.a

Interesting, isn’t it? Want to thank him, or have any comments or improvements? Drop them into the comment box. Thank you for reading.

External Links

Being Stupid

So my struggle with long distance trains doesn’t seem to stop. This time, just like the last time, my train got late. By 30 days. And I am to be blamed for it.

Like Will Smith says in ‘The Persuit Of Happiness’, ‘this part of my life is called being stupid‘, after trusting a hippie with his expensive scanner and realizing how bad a decision it was. I had the exact same feelings this morning about myself. Let’s rewind two weeks. I had to book train tickets for attending a wedding function at my native place, Karwar, on 29th April. As always, I did so from the irctc.co.in portal, and got three confirmed seats for May 28th. Awesome, I said, knowing how hard it is to get tickets during the holiday season. Got the confirmation email with all details and I was good to go.

Today, in the morining, we were all set. All dressed up and ready to start our day journey. We don’t usually do day travels since it is too hot in the non air conditioned coaches that we usually book our tickets in. Anyways, it was going to be fun, I thought. Called up the railway enquiry number 139 to make sure train was on time. Surprisingly, it said the charts weren’t prepared yet. Odd, because the train left the originating station a full day ago. Must be a technical glitch, I thought. Made sure I took everything; Misty, mobile phone, charger, water bottle and what not. Great. We left for our train that was scheduled to be departured at 08:10 in the morning.

The station is just a few kilometers away. We reached there at 8 sharp. Reaching there, I immediately started to look for the platform where our train was expected to arrive. The indicator didn’t say anything. Must be a fairly new train, I thought, trying to bury the little doubt I was starting to have. Let’s ask the station master, said my uncle, and so we went to the station master, to get some solid confirmation.

The station master’s office was a large room with a table in the center. More like a bollywood movie’s police station. The man there was wearing his shirt then. We entered and he asked us what was the matter. I asked him about the status of our train, numbered 22656. He thought for a second before quicky replying that the train only ran on saturdays. Damn! That wasn’t possible. I had a reserved ticket in that same train and it showed today’s date. Station master asked us for the ticket. I showed him the eticket on my mobile phone. Now he was confused. He called up someone on his intercom and asked if there was this particular train running today. The other guy said the same thing, this train only runs on saturdays. Umm. Weird. Station master tried to confirm it by asking if there was any additions to its trips. The answer must have been in negative.

Now we three were real confused. We tried to reason the possbile explanations. ‘Maybe the train is so new that even the station master doesn’t know about it’, said my uncle. Meanwhile the station master rang another number, this time of the previous station, called ‘Vasai Road’. He asked if any train with the said number had left the station or is scheduled to leave. No, was the reply. We thought for another few seconds when suddenly, with a little frustration in his decenly polite voice, the station master said, ‘This ticket is for 28th May’. I went silent for five full seconds. With my tongue stuck out, all I could say there was ‘sorry sir’. I and my uncle came out silently, glad that he didn’t swear at our stupidity. Okay, MY stupidity. We came out, and had the greatest burst of laughter. For him, it was funny. For me, a little less towards funny and a lot more towards the incoming embarassment. I, with my 17 years of schooling, couldn’t tell May from April. Stupid. Very, very stupid. We went to where my parents were waiting. I told them, smiling and trying to make a poker face simultaneously. My mom, got so furious that she actually started to laugh. Things were pretty serious, and yet somehow, that was so stupid that no one knew how to react.

We thought for some time then, and decided to go in some other train in the unreserved compartment, which, if you know, you know the struggle and if you don’t, you’re pretty lucky and rich. I am still in the train, tired, sleepy and dirty with sweat and dirt all over me. I’ll reach in three or four hours, and then it will be life a usual.

Googling And Asking Better Questions

Like many of the things you do in your lives, Googling is an art. Getting to the right page that you were looking for, with the right set keywords and clicking the right links, all this seems to be quite random and uncertain, but once you get into the groove, things become natural and predictable. Finding that random song you heard on the street is no big deal, and neither is finding the article that you had read couple of years ago, the only part of which you remember was the author’s last name. Information is growing each day, and having the right set of tools always within you is so much important. Yes, there’s that shiny history feature in you latest Chromium browser with sync, so that you never lose your tracks, but knowing how to find something on a totally random public PC without having to look into your history is very convenient, also cool.

But then, you might ask, what is the point of remembering a whole new set of rules just so that I could type in proper queries on Google’s homepage? Yeah, if you think about it this way, not much. Every other site has a search functionality built into it. But, think. Googling for a computer problem with site:stackoverflow.com and searching the same problem on stackoverlow’s search is somewhat different. Stackoverflow doesn’t invest as much time on search optimization as Google does. That is the case when stackoverflow’s search is one of the best you can find on the Internet. Think about others. Now, every site having its own search has its own small set of advance search syntax. Would you go around memorizing rules from each and every site that you visit? Are you not better off memorizing for just one site and using it to find everything else? In my opinion, yes.

And learning how to google or how to search in general doesn’t just mean memorizing the dorks, although it does certainly help. It generally means asking better questions. What does that mean? Well, in general, the more you research before asking a question, the more are the chances of you getting a proper and to the point answer. That doesn’t just apply to online searches, but also when asking for help to your fellow classmate or colleague. The research helps in a number of ways. Firstly and most importantly, it can spare you the need to ask for help by finding the answer yourself. Secondly, research also helps to properly understand your problem’s causes, such that any similar problem can be solved with the same approach. Thirdly, it creates a psychological difference in the mind of the answerer when he or she reads a “this code doesn’t work” and a “this code throws a undefined variable error on line 42” and a “the ‘name’ variable on line 42 is undefined as the async function on the previous line hasn’t returned yet.”. As you see for yourself, the third problem definition is far more probable to get answered, and the first one is likely to get ignored (or worse, downvoted, if you’re on StackExchange).

I have people who send me program source code files in emails and the only line of text inside of the email being the obvious title that ‘the code doesn’t work’. That is where the problem lies. When all you say is ‘it doesn’t work’, then the only reply that you deserve is ‘congratulations on figuring that out’. It’s that simple. And to understand the importance of it, you need to value the time of others.

So how does one turn from a naive help addict to his or her own problem solver? The answer is simple, know what the problem is and start searching for already answered questions. You always have documentations and manuals at your disposal, but let’s face it, it is not easy to get hold of manuals if you’re new to the subject. And even if you’re well versed with the topic in hand, you can always save time by searching the popular forums first. So what do you do? You search for the exact problem message, or keyword, keeping it as concise as possible. Trust me, the query string is all that matters, because rarely you’ll encounter a problem that somebody else didn’t face and asked somewhere. You want to keep the variable count low, and so if you’re learning A, use popular libraries and operating systems B and C that would keep the entropy factor low. As an example, if I’m not hacking into linux core and simply programming Javascript, I prefer to use Debian and Nodejs stable, just to make sure that when I fall into a ditch, the ditch is a popular one.

In case you still didn’t get a working solution, now start to dig into documents and manuals. It is usually the case when the component that you think is broken, isn’t actually broken. Look out for platform specific notes and details. Troubleshoot your way backwards to the line where your code broke. If you manage to find that out, then you know the problem. Start by making a fresh google search for that error. In most of the cases, a forum thread might have the answer, which you can find easily with [SOLVED] in their titles. There are also many instances when if you are working with an open source dependency, you’ll find the issue raised by somebody on the project issues section of their repository [like github issues page]. Reading those has helped me countless times.

If even then, in the rare (very rare with computer problems) case that you still haven’t found a solution for your problem in the online forums and documentations, start by properly defining the problem. It is time to ask for help. Always, ALWAYS remember, the people you’re asking questions to, are busy people. If you want to have a percent chance of getting your question read by experts, value their time. The essential things that must be included in every help request are,

  • Precise problem definition with the problem and the current and expected results.
  • Steps to reproduce it. Code snippet that points the line of error (You should never paste 200 lines of code into the question).
  • What have you tried yet. List of the things that didn’t work out.
  • The platform you’re running, everything relevant to the problem. More the detail, better is the understanding of your situation by the reader.
  • A paste of your latest logs relevant to the error. Learn to use pastebin like sites to paste large text files.
  • The links to those similar problem-answers that didn’t work for you.

Add or remove things depending upon the situation. It may or may not apply every time. Important thing here is, try to be a problem solver. Don’t be part of the problem, but try to be part of the possible solution. It is okay if no one answers, but hang there for the first 30 minutes at least, in case someone asks for more details that you didn’t provide.

This should really answer your question, and in case not, don’t lose hope. There are hundreds of specialized forums that you can head to, if the problem is that important to you. Back to Googling, what is it that some people better at finding stuff on the Internet than others? Now you know the answer to that. See, this is how it generally works. Most good questions are answered. Now that you know how good questions are asked, you know what is inside them. You can make a pretty educated guess about what an existing question may contain. Now your task is as simple as just using those keywords to find that answer. This works. Trust me.

Concluding it, I hope this post was informative. Ask wise questions. People really appreciate when you ask good questions. They say no question is stupid. That is not an excuse to not trying to search from the existing resources, or being a naive help addict. Keep exploring.

WebSockets

If you’re like me, then you must have gone nuts on hearing about WebSockets for the first time. We now have a totally independent protocol that will not terminate connection after a request-response cycle, which means all hacks to keep a persistent connection from the browser (long polling, I’m looking at you) would now be part of the history. Welcome to the world of WebSockets.

Let’s start with the Wikipedian and Mozillian definition of WebSockets, to get some initial traction.

Wikipedia says that..

WebSocket is a protocol providing full-duplex communication channels over a single TCP connection. The WebSocket protocol was standardized by the IETF as RFC 6455 in 2011, and the WebSocket API in Web IDL is being standardized by the W3C.

Mozilla Developer Network says that..

WebSockets is an advanced technology that makes it possible to open an interactive communication session between the user’s browser and a server. With this API, you can send messages to a server and receive event-driven responses without having to poll the server for a reply.

To begin with, let us all make sure we have websocket support by clicking . Assuming you do, let’s begin. Web browsers provide us with the WebSocket class. To create a websocket, we simply need to

let ws = new WebSocket('ws://server.com/endpoint');

This gives us a websocket object which supports methods like send() and close() and event listeners like onmessage and onerror.

Creating a nodejs instance running a websocket server is easy with modules. I did try to do it natively, but it went too tedious. Finally I settled for ws module from npm. The server side code looks trival.

On the browser, things are even simpler.

// call the object constructor
var ws = new WebSocket('ws://nagekar-ws.herokuapp.com/'
// attach a message listener
ws.onmessage = function(msg) {
	console.log(msg
}
// send some message
ws.send('hello server!'

Once we call the constructor, an HTTP GET request is generated to initiate the transaction. You’ll find that these headers are sent by your browser.


GET / HTTP/1.1
Host: nagekar-ws.herokuapp.com
Connection:Upgrade
Pragma:no-cache
Sec-WebSocket-Extensions:permessage-deflate; client_max_window_bits
Sec-WebSocket-Key:ut0NjBQxxBnmtUStHfMUDw==
Sec-WebSocket-Version:13
Upgrade:websocket
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.108 Safari/537.36

The ones to notice are the URL, which begins with ws:// protocol. Cross domain requests are allowed. This initial step requires the use of HTTP(S) or web, hence the name ‘web’ sockets. The ‘Connection:Upgrade’ and ‘Upgrade:websocket’ and what requests the server to change this connection to a socket one. The Sec-WebSocket-Key header is what client sends to the server. The server crafts another key with this and returns it back in the response header. A response header looks like this.

HTTP/1.1 101 Switching Protocols
Connection:Upgrade
Sec-Websocket-Accept:7CAJHdL7QMG4ceqd0M1O8SDbM2Q=
Sec-Websocket-Extensions:permessage-deflate
Upgrade:websocket
Via:1.1 vegur

On receiving the Sec-Websocket-Accept header, the handshake is completed, and now the client and server are connected via persistent sockets, ready to perform both way message delivery.

Click ‘Connect’ button to initiate WebSocket handshake. The server will respond with a timestamp every 2500 milliseconds. Also, the server will echo back everything to send at it. The close button will call the WebSocket.close() method which will terminate the connection.

Message Box






Pretty interesting, isn’t it. Things get even more interesting when you use a readymade library built on top of WebSockets, such as Socket.io. It supports all browsers (through fallback to XHR/polling), has better looking APIs, is easy to use and allows you to broadcast without having to worry much about raw connections. If you’re going to use these WebSockets in production, there’s no reason not to use Socket.io. Using native websockets is not recommended if you’re planning to support some legacy browsers. Even Mozilla terms it as an ‘experimental technology’. Check out the compatibility table for more information.

I hope this post was informative. I did learn a lot while researching on this topic, and I’m glad I did. As always, thank you for reading. Please drop a comment in case of feedback or correction.