Tuesday, January 31, 2012

Learning a programming language

No matter what craft you're practicing, two things are very essential for making progress with that craft and getting better at it, wiz. Theory and Practice.


Theory:


1. Read (fundamentals of language)
    Reading about the language you're about to learn is the best way of starting off. Grab a book for fundamentals of that programming language. If you're a beginner, its better if you don't skip and keep reading it from the beginning. If you have prior experience of programming another language, it'll be easy for you.
  
2. Read the example code
    Reading the example code and trying to figure out what its doing is best practice for learning any programming language. Once you get to know about the language while reading, you can skip to reading example code whenever you get bored of reading theory.


Practice:
 
3. Type that example code yourself and run it.
    As you type the code yourself into the editor, you'll start getting a hint of whats going on in the program. And also you'll get familiar with the syntax of that programming language. The best practice is to add your own comments while rewriting code and try to explain WHY something is done.
    For example:
int calSquareArea(int side)
{
 return side*side; // returning square of 'side'   <-- Isn't it obvious? Duh! Not so  helpful, is it?
}

int calSquareArea(int side)
{
 return side*side; // formula to calculate area of square is side*side    <-- Ooh, now I get it!
}

4. Change it.
    Now that you know what the nuts and bolts of example code are, you can tweak it a bit and run again. As the program works differently each time you tweak and run it, you can make it to whatever you want. Its quite an useful skill to modify already existing code and make it do whatever it is that you want to do. You'll learn a lot by this trial & error method of learning a little bit everytime you change something in the program and re-run it.

5. Write your own code.
    Writing your own code is like making your own world with your own terms. You're the God of that world, you get to decide what happens in it. Sounds awesome, doesn't it? So start writing small piece of codes to do some tasks or solve problems. Personally, I would recommend to go after small games like tic-tac-toe. Write a huge number of such small programs. And then move on to taking big projects.

Sunday, January 15, 2012

Recovering a (in use) removed file in GNU/Linux

    This has never happened to me, before. I downloaded a video to my $HOME/Downloads directory and I was watching it in VLC-Player. For some reason, I paused it and started managing files in my $HOME directory. Later, I removed all files from my $HOME/Downloads directory.
 
    Now, here is the game. I removed the video along with the unwanted files which was still in use by VLC. I didn't realized it that time. I got back to vlc and resumed the playback. After few moments, my jaw was dropped. I didn't really know how did it worked. So, I started working to get that video back. I knew it was still in the memory, but, how to get it back!


    I thought, there would be a 'save' option in the File menu of vlc player. Unfortunately, it wasn't there. So, I had to start digging in. Few days ago, to monitor the 'files in use,' I'd learned the lsof (list open files) command. And it extremely useful to find the file descriptor of that file.

  Here are the steps I followed, to recover that file:

1. Finding the file descriptor:
I wanted the PID number of the process  by which the file was opened. Simply, I got it using ps and grep.

 $ps -e | grep vlc

2. Now, I'd the PID of vlc. And I wanted the FD number of that file.

$lsof -np PID-of-vlc-player

3. Now, simply I wanted to copy this file from that file descriptor which was in /proc file system.

$cp /proc/PID/fd/FDNUM $HOME

    I had my video now. Actually, I could download that video in this time, but, recovering that file was kind of a challenge. That taught me a lesson, too. :-)

Saturday, January 14, 2012

Being elite with 10,000 hours of practice

         So I read in a book called 'Outliers - The Story of Success' that it takes around 10,000 hours of practice in any field to achieve mastery. Speaking of which, I'll be hardly at couple of hundred hours of coding or even less than that (OH YES! I'M A NEWBIE!). But I love coding. Whatever time I've spent on working on my academic projects, or coding in lab while practical hours or at my localhost; I loved it.
         But believe me, being good at studies and being a programming addict at same time isn't possible. So I sort of killed my will to quit college and spend hours of programming at home, as I need to take the degree. Only then I'm gonna get a 'well-paying' job. But apart from working for money, its my hobby and I'm a big fan of Open Source Community. As they say
" The elite software developer is the programmer who spends all day pounding code at work, and after leaving work he writes open source software on his own time."
 Still, what I can do at my best right now is spend as much time as possible on coding. If I spend 5 hours a day coding, it'll take me 5 and a half year to be an elite programmer. Althought it isn't possible literally, I'll try my best to spend most of my time coding at my home. :)

                 Happy Coding everyone. Stay hungry ! Stay foolish ! :D

Edit:
          A really nice applet to keep track of time you spend coding or working: Project Hamster(Time Tracker)