Thursday, July 18, 2013

Now, I "Git" it.



Recently, I picked up two of “The Pragmatic Programmers” books, “New Programmer’s Survival Manual” by Josh Carter and “The Developer’s Code” by Ka Wai Cheung, and started skimming through them. A lot of the ideas, suggestions and lessons there was an eye opener for me. Barely 30-40 pages into both texts I was astonished to see how many vital skills and knowledge goes untaught in colleges (especially here in India).  No wonder, we are a cheap labor market for the lowest intellectual level of IT industry, which is in self-delusion of working on the next big thing, while we only do menial labor of code maintenance or simple web-design and the world is rapidly switching to mobile/cloud computing and augmented-reality.

One tool that I came to learn of is the Version Control System. Such a system is used to carefully keep all source-files and dependencies intact over the development cycle of the project. It logs all changes and can be used to “rollback” to previous development stages or versions, if required. It can also be used to “fork” a repository (the project directory containing everything relating to your project) which is another word for copying/cloning your project repository for sensitive changes or features that may otherwise break your previous tested and working code. This way you can test new code and features on existing project without ever messing up your original source-code.

I started learning Git and it was pretty simple and easy to learn, for me at least, as I’ve dabbled with some Bash Terminals on Ubuntu (Linux) and command line terminals don’t intimidate me as much as they used to. I had my humble beginning from Microsoft Visual Studio with Visual Basic 6.0.  Microsoft Visual Studio with its RAD (Rapid Application Development) IDE may have been easy to learn swiftly with simple drag and drop interface, auto-completion and other features. Actually, now, after a few minutes of recalling, I can say that I had the fortunate opportunity of playing with QBASIC and C, in my early school days.

But, here is a note of caution: RAD is not for a true programmer. Visual Studio may have it’s advantages but when something breaks (I am not talking about Bugs), you’ll have to return to the drawing board. Beware! You’ll become complacent, delusional and xenophobic. Trust me. I’ve been there. It is best to learn your craft with the smallest of tools no matter how crude or cumbersome they may seem to you. Once, you’ve built a full fledged “personal” project with them, congratulations, you’ve graduated! From “Personal Project” I mean, a problem or idea that you found independent of books/exercises and implemented it after your fair share of research and development. Then you can switch to whichever tool you want to use and use them.

Another disadvantage of RAD and IDEs are that it’ll never show you the internal system level implementations. How your files are stored or the system/hardware specific dependencies used by your project unbeknownst to you and when you migrate your project or deploy it somewhere else or on the web….Oh boy what a ruckus!

RAD is useful for simple and quick development of applications and systems to test your idea/product and prototype business models linked with that product. They can then be hacked to improve and converted to independent architecture/platforms like Java etc later on (only when you know what’s under the hood). I personally have never witnessed high performance or high scalable systems built with MS Studio. No, sir, I have not. Look around everywhere. They are either built on Open Source and/or custom built.

Returning to Git. Git was developed by Linus Torvalds and his Open Source buddies. I wont detail these unnecessary information that can be wikied. But, I’d like to add that Git has the additional advantage of being distributed and multiuser. It may have other features but, I won’t get  into its details.

Now, people, Let’s get our hands dirty!

Setup::

First, we’ll need to install Git on our systems as only a lucky few of you all would be fortunate enough to have it pre-installed on your system. If you’re running a flavor of Linux you may have it installed somewhere in there. Whatever, here’s the link to it’s setup and official site:

Tutorial::

Now that you’ve got the link to the Official Site. I presume you’d prefer the official guides rather than mine, so I’ll skip the hard part of hand crafting a tutorial and directly link you to a tutorial that I found very useful (especially for the Terminal literate):


No need to be intimidated by this command line tool, it’s available in a GUI friendly version, too. Still, some of you might want to learn the Terminal first and then start using Git, if you’ve got no prior experience with Command Line Terminals. Take another pearl of wisdom, Try the Terminal available with Linux and/or Mac OS X, not Windows. The Windows Command Prompt was never meant to be used as a powerful tool as Windows was originally constructed for the technologically impaired ordinary gentry and many developer tools were scorned upon.
If you’ve returned to this page after getting “Git”. Thank you very much. You didn’t abandon me while I was ranting on and on. A comment or a token of appreciation would work wonders for my aching back that strained, against this uncomfortable sofa, in order to illuminate you with such good ideas.

You must now try GitHub. I’ll leave the requisite research on your dependable Google search skills.

Monday, July 15, 2013

Javadoc: The Bible of Your Java Source Code

Today I'll be sermonizing on Java, my dear readers. I have observed since I began learning Java in my third semester of Computer Science Engineering undergraduate course that comments are a vital part of a program or rather the source code. But, never were we taught about good commenting standards and commenting conventions. Also, our exposure to the Javadocs was minimal. Our only interaction with the Javadoc was when the IDE (Netbeans or Eclipse or any other) would produce a small popup window with a summary about the keyword over which our mouse pointer was hovering.

It fills me with sadness that such vital skills of reading, navigating, understanding and producing Javadocs was never taught or even appreciated. Like some unspoken truth, we all kept mum and so did our teachers, that we’ll eventually learn it “on the job” or “later in life”. I seriously doubt about the many Indian, so called, software professionals knowledge on how to comment code correctly and generate usable Javadocs or read other programmer’s Javadocs. Any Java programmer worth his salt must have this essential skill of playing around with the Javadocs.

There are various commenting styles in the art of programming each style has a certain aim in mind. Some of the commenting styles can be roughly categorized as:
  • Explanatory/Verbose Commenting – Used by experts, teachers and trainers to explain in detail what each line of the code does. It is generally regarded as “How?” themed commenting. It is also used when a complicated piece of algorithm is implemented which cannot be understood without certain explanatory aid. This style is generally picked up by students from their teachers, but, is not good for day to day professional use. Most of what any programmer will code in life will be simple and easy to understand without any comments. The best code is self-explanatory without necessitating a single line of code.
  • Reasoning/Terse Commenting – Used by professionals in day to day coding, it involves adding comments only when a certain part of the code is not self-explanatory and departs from the ordinary trend of coding. For example the forking of a certain thread to handle background tasks etc. It is generally regarded as “Why?” themed commenting. In other words it is a minimalist approach to commenting. You comment only when it is most required.
  • Documentation CommentingUsed to generate documents or guide other programmers using your source-code either as an API or a Legacy code. It may detail hardware and software dependencies, performance issues, code vulnerability (Yeah, you’ll find in some legacy codes useless warning comments such as, “//DON’T TOUCH THIS CODE! EVERYTHING WILL BREAK. TALK TO BOB BEFORE EDITING THIS CLASS”, where Bob is someone who has left the company 15 years ago. Good luck with that, mate.) This commenting style is best seen in Open Source APIs, where you can seek help from Open Source communities which may take a longer time to resolve your issues and even third party licenced APIs, though they provide a paid support service to resolve your issues in the least time possible.
Note: No adherence to a single style is requisite. Often, a mix of all styles will be required to deliver a useable and malleable source-code.

Javadocs, ahoy!!

 

Javadoc is a documentation generator from Oracle Corporation for generating API documentation in HTML format from Java source code. (Yes. This line is ripped off from Wikipedia. Now, please, don’t label me as a plagiarist.)

Javadoc handles only a specific format of commenting within the Java source codes. It can be either generated by using the pre-bundled javadoc tool with the JDK or invoking it via any Java compatible IDE (I’ll be sticking with Eclipse in this blog. Go to hell, Netbeans! *Just joking! I know how many of you still depend on it and I can empathize with you.*).

I’ve attached the source-code of a simple java program (which I did not care to test run even once. Don’t bother compiling and running it. Our focus should be on commenting for Javadoc.) with screen shots of generating and using the related Javadocs of this project. [The screen-shots are available here only, as my paid internet data usage quota has expired and I am currently blogging at the speed of 3~4 Kbps. I promise to update the bundled project with screen shots after I get my internet connection recharged which is looking doubtful considering I have already paid for the recharge and the confirmation SMS or transaction SMS has not yet been received since today morning 8 AM or so.]

Now, let’s see. Step 1:

Select the Element you want to comment on. Like in the above screen grab, I’ve selected method Circle from class FigureStore.

 

Step 2:

Select the Generate Element Comment option from the drop down menu of Source button from the main menu.



Step 3:

Eclipse auto-generates the comments depending on the selected element. As here the selected method Circle has two parameters, the Radius as X and the Diameter as Y.


Step 4:

Now, edit the element comment as required. Try adding details as briefly as possible.

Step 5:

After you’ve added all the element comments you need to generate the Javadoc. For generating the Javadoc for your project, select it in the package explorer and the choose Generate Javadoc from the Project option in the main menu.

Step 6:


Now, you’ll see a dialog as shown in the above image. Make sure your project is selected as well as all of its components. The Javadoc tool is selected via the Configure button. The Javadoc tool is present in the “bin” directory of the JDK. Also, set the path of your doclet to a new “doc” directory of your project.


Step 7:

You can add a specific document title to your doclet and you may add any referenced/dependent jars and packages which mainly comprise of the JDK libraries. Then select Finish. (Yes, I don’t go any further with that alluring “Next” button.)



Step 8:

Now, sit back and relax for a few moments while the Javadoc is being generated. The time take for Javadoc generation is dependent on the number of Elemental Comments, program size, structure etc. Your Console should show something like this:

 


Alternate Method:


Here’s an alternate method to generate Javadocs. Right click on your project in the Package Explorer. Select “Export” from the drop down menu and a dialog will appear as shown below.

Select Javadoc from the Java directory and Bam! It’s done (It’ll take you to the Javadoc Generaton Dialog). 

What to expect from Javadocs?

You’ll see a new directory under your Project in your Package Explorer called “doc”. Exanded it’ll look like this;

The “index” html file is the start or main page of your Javadoc. Double click on it to open it inside Eclipse. A sample Javadoc is shown below;

 



Also, now you’ll be able to invoke small popup help windows for your project/package which can be used in other projects as well if the Javadoc is imported in there Project directories.
 

IMPORTANT END NOTE:
A good habit is to keep on making Element Comments while programming in order to keep the burden of commenting low. Once the size of the source code gets out of hand commenting becomes difficult and you’ll not be able to leverage the Javadoc prompts for other parts of your source-code.

Also, one must always try to keep deprecated (i.e discontinued) methods in future iterations and newer versions of the program/package in order to maintain backward compatibility.”

Sample Documents and Source Codes::

My Experiments with JUNG 2.0 (Java Universal Network/Graph Framework)

My first minor project was a major disaster. But, in hindsight I can salvage out some simple nifty tools from the graveyard of codes on my laptop. I had to settle for a simple "Simpletron: A Simulated Microprocessor in Java." (I've blogged about it earlier here.) All it took was firing one of our three team members and the remaining duo pair-programmed into the night.....or at least for a few hours until he too quit and went away to see a movie. We ended up showing this little nifty tool and anther tool which Chitransh had designed to go together with this one. I then finally completed and perfected Simpletron on my own.

Nexus Grapher was a tool to be added to a data-mining project which we failed to complete due to broken team dynamics and interaction, as well as, pressing deadlines. I coded this small tool from Open Source, JUNG 2.0 API. That's "Java Universal Network/Graph Framework" Version 2.0.

This project had a lot of potential and planned features that were never realized. Nexus - as we named it, was supposed to be a suite of various data mining tools which can be used on a directory full of web-pages (preferably from Wikipedia & news-sites) and text files etc documents.

Initially we planned to adopt Stanford NLP Parser to perform NER tagging (Named Entity Recognition - This link is a Wikipedia page). This would produce a Penn-treebank structure which would be parsed by a method of my nexus-grapher to generate a visible, click able, interactive graph of inter-relating facts and texts from across the webpages and texts saved in the directory.

Selecting a node with the label of a named entity would invoke another method that I wrote with my team-mate Harshdeep Sokhey which will open a window displaying a consolidated text file with (that label) related texts collected from all over the mined directory. (Like a dynamically generated Wikipedia page).

But, this consolidation and compaction method was in it's nascent state without much intelligent features of context aware auto-data updates (on comparison with recent file save) and even basic features like text-redundancy. All it could do was get paragraphs and lines, consisting of the keywords, from all over the directory and order them by file creation date.

The connecting edges of the graph was also meant to be click able to pull up a consolidated file with paragraphs of texts where both these named entities would appear simultaneously. Another feature we failed to implement. I tried switching to Apache Lucene but due to approaching dead-line I didn't have enough time to learn Lucene.

Currently, the nexus-grapher reads from a CSV of preselected named entities to generate the graph.

If you want to know more of my plans for "Nexus: An interactive data-mining & visualization suite." send me an email. If you're able to implement such a suite, very well, do inform me.

Directions/Code Walk through::
--------------------------------

This small tool is built with open source JUNG 2.0. My knowledge and expertise with it is... limited. If you encounter any troubles and have questions relating the graph and its subsequent visualization, please contact them. Here's a tutorial of JUNG 2.0, as a PDF, which I found handy and it should be enough for the Java noob to get his feet wet.


Before I get into the details of my Java project, let's first go over the basics of my C.S.V formatting and rules for, what I call, "nexusft"; nexus-format-textfile.

The first token of the C.S.V is the available roots that can be searched and all other succeeding tokens are it's children.

Now, onto Java! The project package is split into three classes:

1) naseemgraphexp2.java
2) ReadFile.java
3) search.java

"naseemgraphexp2.java" is the core class with a Main  method to run a simple graph visualization. The code is self-explanatory with comments.

A name (search-token) is sought from the user when the program runs and calls the readnexus() method. The searched nexusft file address is passed as a parameter for the class ReadFile.

This ReadFile returns the arraylist of lines of texts in the nexusft file which is tokenized and searched against the entered keywords. Only the first token of the C.S.V (if matched) is considered the root and all others are considered children. Else the method returns an error message.

The constructor method "naseemgraphexp2()" is where my iterative loop adds the tokens as vertices and their names as labels. Only the searched central/root node of the graph is connected to its relatives.

The rest of the code in the Main method is self-explanatory and JUNG specific, to build a graph and visualize it. The geniuses behind JUNG kept very clean and clear examples for me to hack with my shoddy programming skills. I've left behind some original code of JUNG examples, which has been commented, I know it's bad practice (I read in one of them, Pragmatic Programmer/Publishers book), but, it is there for all those who are new to JUNG 2.0, to see and realize the available features I did not use.

"ReadFile.java" is a straight forward file reading program to read and add each line of text into a String[] array which is returned to the calling method in naseemgraphexp2.java.

Finally, in "search.java" is the small program to tokenize each string that we previously stored in our String[] array to be matched with our search token. If a match is found, then a true Boolean is returned to  nexusgraphexp2.java's method "readnexus()" which then proceeds to tokenize this string to create nodes for the graph.

I hope, you find it useful despite this poor hacking. If you polish it and go onto implementing better features do e-mail me and credit me for this shoddy hack.

Your,
Md.Naseem Ashraf
iam.legend.n@gmail.com

Source Available at:


https://docs.google.com/file/d/0B4e1TZA7mwrNVTYyTk9fOGVuT1E/edit?usp=sharing

Screen Shot:

 

 

 

Creative Commons License

This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 United States License.