Friday, March 24, 2017

My First Google Chrome Extension! And Asynchronous Functions in Javascript

A while ago I published my first Google chrome extension, which you can find here. Basically the extension is an assignment tracker that turns your new tab page into a notepad. It also allows you to highlight text in the browser and right-click to add it to the assignment tracker clipboard.

Although the new tab notepad idea has already been implemented before, I wanted a notepad that would allow me to add text I see in my browser to the notepad without copying and pasting and without opening the new tabs window. My extension Assignment Tracker solves this problem.

The Assignment Tracker was easy to implement because I was able to study code of other google extensions I found on Github, but the add assignment part of my extension was performing unreliably. Fortunately, I discovered the problem later during Incube*.

At Incube, my friends and I are working on an extension that creates a dislike button for Facebook, and I discovered Assignment Tracker's problem while trying to fix something similar. It turns out that there exists something called an Asynchronous method invocation.

In my extension, I was using the chrome.storage API to store text users added to their assignment tracker clipboard, but chrome.storage is designed to be asynchronous; other processes can happen while the chrome.storage function completes. This messed up the chronology of how text was stored and updated on the new tabs page. As a result, selected text didn't always show up on the new tabs clipboard.

I used the get and set functions of chrome.storage to retrieve and edit stored text. These functions take two parameters: the first is the variable we want to retrieve or modify, and the second is the function that will be called after the get or set function has completed.

By nesting the set function inside the second function parameter of the get, I was reliably able to modify the clipboard text to include the new highlighted text information. I have included my solution in the following excerpt:

function add(event) {
  if (event.selectionText) {
    alert("Added to Assignment Tracker: \n\n" + event.selectionText);

    chrome.storage.sync.get("data", function(object) {
      text = object.data;

      if (text === "undefined") newtext = event.selectionText;
      else newtext = event.selectionText + "\n\n" + text;

      chrome.storage.sync.set({ "data" : newtext }, function() {
      if (chrome.runtime.error) console.log("Runtime error.");
      });
    });
  };
};

I am still trying to learn JavaScript so this stack overflow answer may be able to explain the situation more clearly. Because of this annoying experience, I also learned about the bad programming practice of callback hell.


A quote really stood out to me from the callback hell article that I linked above:
"Write small modules that each do one thing, and assemble them into other modules that do a bigger thing. You can't get into callback hell if you don't go there." 
- Isaac Schlueter (creator of npm)

In the future, I will be more careful to avoid writing modules that are too big and hard to debug.

Cheers!

* Incube is an organization where a bunch of people work on projects and eat Indian food together.

Friday, March 17, 2017

The End of Midterms and UROPing

This week was a stressful week because I had two midterms that each counted for around 16% of my grade. Although my classes this semester are definitely more interesting, I miss PNR and not having to care about my grades. I read online that it is relatively easier to get Bs at MIT, but much harder to get As. Because I want to qualify for MEng, I will need to get at least one-third As.

Outside of my classes, I spent around 6 hours working on a program called audio-pass, which I am developing as part of my UROP. Audio-pass takes live sound inputs and slows it down a given percentage. It is the first program that I've worked on in C, and C is very different than Python. For example, parsing arrays is much more difficult, libraries are harder to import, and one has to consider memory allocation. Nonetheless, I am am enjoying this learning experience.

Today, I wrote tests for and improved a function that reads an array from a given index to the last modified index and writes it to another array. I also implemented a plot function that behaves like matplotlib in python, but directly calls a command-line program called gnuplot to plot data.

Here's the cute little graph that took way too long to figure out!

I am grateful for not only learning a new language C, but also for developing good software development habits such as writing tests for every function and checking for differences in code before sending pull requests on git. My UROP adviser also taught me many other useful git commands like how to deal with merge conflicts.

Life is good.

Thursday, February 2, 2017

SICP: Lisp and the Y Combinator

Today was the last lecture of 6.037: Structures and Interpretations of Computer Programs (SICP) which is the condensed version of the legendary 6.001 previously taught at MIT by famous professors Hal Abelson and Gerry Sussman, and more recently, Eric Grimson. SICP is taught in scheme (a dialect of Lisp). However, the course is more about teaching you how to think about designing computer programs than the language in itself.

SICP was an amazing class. It inspired the perfect mix of confusion and fascination that makes me want to continue understanding more, as if SICP had inducted me into a secret society of hackers who are able to appreciate computer science in a way unbeknownst to even CS majors. Ironically, I was presented with a button today that officially inducted me into the Knights of the Lambda Calculus.




I actually first learned about Lisp through reading Paul Graham's essays around a year ago before starting school at MIT. I'm a huge fan of Paul Graham and Paul Graham loves Lisp, so I developed a curiosity for Lisp, which prompted me to take 6.037. I never understood that Paul Graham's firm's name Y Combinator meant anything more until today's lecture, when the instructor talked about lambda calculus, which was introduced by Alonzo Church as a way of expressing computation (equivalent to the Turing machine).  Church's idea means that any program can be expressed solely through a series of lambda expressions. I learned today that the y-combinator is a way of supporting recursion through only implementing lambda expressions.

It was so uncanny getting the Knights of the Lambda Calculus button, seeing the y-combinator on it, and finally understanding what the name of Paul Graham's firm means. It feels almost cosmic, as if Paul Graham had inspired me to take 6.037 through his essays, and on the last day of class, I received a button that said the y-combinator on it, reinforcing the origin of my desire to learn Lisp.

Perhaps this is the cosmic way of congratulating me on my time at MIT so far: a pat on the back for everything I've learned here, and validation for my actions thus far towards getting where I would like to be in the future.

Thursday, December 22, 2016

My First Semester at MIT

I took my last final two days ago and today I found out that I passed all of my classes. For those of you that don't know, the first semester for freshmen at MIT is graded on pass/fail no record (PNR). PNR gives frosh a chance to explore college and MIT without feeling stressed out about their grades. I feel relieved that I didn't bomb my finals too hard, and I feel excited about the next semester because I'll actually get to take classes in my major.

MIT is amazing. I've never been happier by whole life. I feel constantly stimulated and am casually absorbing valuable information just through conversations around me. Never once have I been bored here, or have gone to sleep without appreciating what a great day I had.

I was reflecting on what I've learned at MIT this semester. I'm more knowledgeable about EECS now, switching entirely to Linux and learning the command line, learning Android development through my UROP, doing hackathons with my (awesome) hackathon group, starting machine learning in my own time, coding in Python, learning audio engineering, and more... Moreover, my music improved also, despite not practicing nearly as much in college. I feel a deeper understanding of music, partially due to rehearsing with my trio and analyzing the score, a superb private teacher and a wonderful chamber coach, the Intro to Western Music class I took, and the plethora of quality concerts I've gone to in Boston including multiple BSO concerts, which were nearly all free.

(Programs from concerts I've gone to taped onto my wall)

What's most impressive about MIT, however, is the amount of information I've absorbed through my friends. I've casually learned about Raspberry Pis through conversations, won one from HackMIT, and set it up as a server with a friend. I've casually learned about the fast Fourier transform through my boyfriend, as well as about the names and functions of many electronic parts. I've casually learned about basic machine learning concepts through upperclassmen on hall. More so than anything else, the amount of understanding here makes me feel like nothing is impossible.

A while ago, I had issues installing Python packages on Ubuntu and accidentally deleted Python. Because there are many Python dependencies in Ubuntu, through deleting Python, I ended up deleting essential files, and Ubuntu wouldn't boot anymore. In any other circumstance, I would've panicked, yet at MIT, I was able to go to IS&T where someone quickly helped my back up my files and reinstall Ubuntu, while simultaneously teaching me about how partitions worked. Two days later, my computer again stopped working, this time both Windows and Ubuntu didn't boot. Initially, I had no idea what the problem was, but a friend on hall helped diagnose that GRUB wasn't recognizing my partitions. He helped me extract my files from a live USB and I then reinstalled both Ubuntu and Windows.

I relate this experience as an example of how incredible it is that I can casually absorb new information from people around me, and how MIT makes it feel like every problem can be fixed. I casually learned about partitions and random terminal commands just through going about my everyday life, and I'm able to remain calm when all of my files are at risk because of the reassurance that someone on MIT is bound to be knowledgeable enough to help with technical difficulties.

I'm very excited for next semester. Given the amount I've learned this semester, I'm very optimistic that I will graduate MIT with the belief that anything is possible, an expert in topics I care about, and more importantly, the ability to solve any problem that comes across my life.

Here are some pictures of my room, since it's the closest thing I can take pictures of: