Thursday, May 30, 2013

Why should you have your code "neat"

It is a painful truth that most of my fellow developers don't care about code readability and maintainability.  Sometimes its frustrating to make changes on an unformatted, in other words poorly written code.

Code that we write represents our brain and I consider my code as how an artist considers his painting.

After reading an article on Style matters, I thought of writing this post on few important readability factors and how important it is to have your source code neat.

Top 5 readability & maintainability factors
  1. Documentation with proper comments
  2. Formatted code with proper indentations
  3. Organized file folders
  4. Proper naming conventions
  5. Never use deprecated code
Comments & Documentation
A good developer learns a project just from the code; javadoc plays a significant role in the learning process. Imagine that all open source projects and JDK code with no comments and documentation.  I am sure that it would at least take 10 times more effort to figure how to use them. A good javadoc in your application saves enormous amount of time for fellow developers.
All IDEs shows help texts for a method or class on mouse over for quick assistance, remember they all come from the javadoc of that method or class.  When you leave your code with no comments, then you are letting some fellow developer to be handicapped in some way.

Formatted code with proper indentations
When my sister write Java classes in notepad for her college project,  it is acceptable to have a code which doesn't have a decent formatting. Considering the modern IDEs that we use, formatting takes a few seconds. It has to be a habit to write formatted code and not for the sake of your manager or your lead wants it.
To the least there are options to do a format at your project level which formats all your classes in the project.  For example using eclipse, "Right click on your project -> Source -> Format". Boom all your code is formatted decently with the basic code formatting settings, however you can go and edit the settings as you want and also you can share it across your team as an xml file.

Organized file folders
During several code reviews that I have witnessed project documents kept under "src". One common answer I heard from developers was its easy to access. I was shocked to see project documents going inside your deployable files. A good java project should have well organized packages which very well represents OO design being followed. Writing everything in the same folder makes it unmaintainable as the project grows.

Proper naming conventions
In most cases the people who maintain the code is not same as the author. They may not be knowing the project as good as the original author, or may be less skilled programmers compared to the author. Having very good class, method and variable names makes it very much easy for the person who maintains it. Reading java code should more or less be similar to reading english. Following descriptive naming convention helps to understand the code much faster. Using i, j, k, abc, xyz as your method or variable names through out your project makes it almost an unsolvable puzzle for people who follow. Sometimes it makes it tougher to debug for even the author who wrote it.

Never use deprecated code
A method or class is deprecated when a code is "buggy" or "inefficient" or for a "bad coding practice". And it is represented with @deprecated annotation. A good API would always give an alternative better option before deprecating the existing one. Look for the alternative option and never use deprecated code.

Note:  All the points discussed here are for code written Java and may be applicable for other programming languages too.

Wednesday, May 22, 2013

Collection of very interesting unix commands

How to get your top 100 unix commands

history | sed "s/^[0-9 ]*//" | sed "s/ *| */\n/g" | awk '{print $1}' | sort | uniq -c | sort -rn | head -n 100 > commands.txt

How to copy a file to all subfolders of a directory using unix command

find . -type d -exec cp pathtofile {}/ \;

How to delete all .svn folders from current directory using unix command

find . -type d -name '.svn' -print -exec rm -rf {} \;

How to list all network connections (including which app they belong to)

lsof -i -nP

How to run the last command as root

sudo !!

How to execute the previous command until it is successful

until !!; do :; done

How to display summary of git commit ids and messages for a given branch

git log --pretty='format:%Cgreen%H %Cred%ai %Creset- %s'

How to list all file extensions present in the current directory

ls | perl -lne '++$x{lc $1} if /[.](.+)$/ }{ print for keys %x'

How to get your top 10 commands with number of times you have used it

cat .bash_history | cut -f 1 -d\ | sort | uniq -c | sort -r | head

To generate list of usernames from an svn logs (Run from top level of the svn project)

svn log | grep -E "r[0-9]+ \| .+ \|" | awk -F"|" '{print $2}' | sort | uniq > ~/authors.txt

How to get process details from its pid

ps -p $pid

How to open firefox profile manager

/Applications/Firefox.app/Contents/MacOS/firefox-bin -p

Credits:
https://docs.google.com/forms/d/1XNMoSdfYFe_WkPfU--M88oL00PDLIOAo1HxjhZvZYJ4/viewform
Mr. Robg
My previous post
http://stackoverflow.com/questions/1102986/most-powerful-examples-of-unix-commands-or-scripts-every-programmer-should-know
http://www.commandlinefu.com/commands/browse/sort-by-votes

Monday, May 20, 2013

Few must know unix commands

After spending more than 9 years of my life in microsoft os,  I feel great now to use my Mac notebook.  Especially I have fallen in love with the terminal and thrilled to learn the power of unix.  I curse myself for not being an unix user so far. Well, its never late to start. :-)

As a beginner I thought of sharing my learnings to my fellow unix beginners with few basic commands that you "must" know as an unix user.

Few basic "must" know unix commands

1. ls - list files and folders

What is ls?  A command to list all files and folders in a directory.
Usage: ls [-ABCFGHLOPRSTUWabcdefghiklmnopqrstuwx1] [file ...]

Explaining few commonly used parameters of lsls command args

2. pwd - present working directory

What is pwd?  A command to display the present working directory path.
Usage: pwd

3. chmod - file permissions changer

What is chmod?  A command to display the present working directory path.
Usage:  chmod arguments file
Permissions to a file can be of the following three categories,
  1. Read
  2. Write
  3. Execute
Let me try explaining this using numeric permissions,

Read

400 - read permission to owner
040 - read permission to group
004 - read permission to anybody

Write

200 - write permission to owner
020 - write permission to group
002 - write permission to anybody

Execute

100 - execute permission to owner
010 - execute permission to group
001 - execute permission to anybody

All permissions are in 3 digits,
xyz - indicates owner
xyz - indicates group
xyz - indicates anybody

For example:
chmod 777 filenmae means read-write-execute (full) permissions for anybody to the file.
chmod 644 filename means read and write permission to owner and read access to rest of the users to the file specified.
chmod 755 filename means read-write-execute permissions to owner and read and write access to others for the specified file.

The best way to learn any unix command is to do a "man" of that command.  For ex: man ls

Thursday, May 16, 2013

Git for beginners

Getting started with Git

I would advise to read through what is Git, and have a decent understanding of SCM process.
The first question everyone would get is why I should move away from my current SCM systems(CVS or SVN).  Well there are lot of articles around this topic if you google,  just a couple of quick reasons I would give to go with Git,
  1. Works the best if you are working decentralized.
  2. Stop questioning and just "Go with the flow" :-), git is grabbing more focus recently especially after GitHub's strong support.

Lazy comparison of Git vs SVN commands

I thought a simplest way is to compare Git vs SVN was through the basic commands used for day-to-day development.  Though we use the UI tools for regular activities, I am listing down the comparison by commands (lazy).

Git vs SVN commands
Git vs SVN commands
Two notable operations are the checkout and commit.  For any Git operations "clone" is a prerequisite, so to do a checkout initially you must clone the repository. If you notice a commit is not an actual commit to the repository instead it is to your local.  You need to do an explicit push to the repo after a successful commit. Remember everything happens in your local and not in your repo.

Tuesday, May 7, 2013

What is maven batch mode

Maven batch mode

Batch mode is to run Maven in a non-interactive, continuous integration environment. When running in non-interactive mode, Maven will never stop to accept input from the user. Instead, it will use default values when it requires input.

How to run maven in batch mode

Pass one of the params below to mvn command from your terminal,

--batch-mode, B

Monday, May 6, 2013

How to rename a file or folder in mac

Rename a file or folder in mac

If you were a windows user and new to mac, you would wonder how to rename a file in mac.  

The first thing you would try to do is a function f2, which wouldn't have helped.  The next effort you would have taken would be to do a cntrl click to look through the options.  That wouldn't have helped either.  May be you would have then tried "duplicate" or "alias" option and vent in vein.

Select the file or folder you want to rename and press enter key,  here you go now your will be able to  rename.

Friday, May 3, 2013

Struts 1 reaches End of life

EOL for Struts 1

It has been a great framework since the first release in 2000.  I have used it in many of my projects, but its available no more for enhancements.  :-)

As per Apache there will not be any security fixes as well in the future.  Apache's Announcement has the complete details if you are interested to know further.

Wednesday, May 1, 2013

How to delete all svn folders using mac terminal

How to delete .svn folders

On the process of importing my svn repository to my git (without history),  I wanted to delete all .svn folders from my project without deleting the rest of the content.  Here are the steps to do that,

cd /the root folder of your project
find . -type d -name '.svn' -print -exec rm -rf {} \;
  1. cd --> change directory to the root folder of your project.
  2. find . --> find in the current directory
  3. -type d --> file type directory
  4. -name '.svn' --> file name .svn
  5. -print --> print what matched up to this point (the .svn dirs)
  6. -exec rm -rf --> exec the command rm -rf (thing found from find)
  7. {} --> place holder for out from find
  8. /; --> / to escape ; and ' is to tell find that the command for exec is done