How to write clean code : a summary

November 1, 2012 at 9:54 am (Programming, Styles) (, , , , )

This is basically a summary from Clean code : A handbook of agile software craftsmanship written by Robert C. Martin. This is done as an assignment work which is targeted at making programming students to read books for themselves. If this violates any copyright laws please let me know, although i had used my opinion as possible.

You can find the original book at this amazon page.

Note: This blog article is still under construction.


Names can talk for themselves

  • If a variable, class or method name needs a separate comment to identify itself, the name probably needs to change.
  • Names should not include misinformation. For example, a grouped set of subscribers should not be identified as ‘subscriber_list’ if it’s not a list.
  • Names should not look similar at the first sight, as people who are reading code often don’t read the full name.
  • Avoid using l and O in small variable names as it could be easily misread for 1 and 0 ( one and zero).
  • Do not use misspelling as a tool to create new names (ex: listenToAdapter, listenToAdaptor).
  • Use pronounceable names as possible, which could be easy when at the code reviews.
  • Remove obvious redundant words from the names like the, a , an, class, var.
  • Names should be unique enough to easily search, if needed. This is strictly needed if the scope is high.
  • No need to shorten names as we have more advanced languages and GUI tools now days.
  • Use nouns (or nouns which can be used as verbs) for class names and verbs for method names.
  • Any name should be generally understood by any programmer of any demographics.
  • Use one word for a similar concept throughout the code base.

Permalink 1 Comment

Change of address – from to

February 3, 2011 at 12:44 am (1, Linux News) (, , , , )

The site is in the process of moving to in the hope to enable additional services than just wordpress.

All the new posts will be posted to the new blog address above; so it is recommended to visit the above site.

Possibility to redirect to by automatic redirection is under consideration, but it comes at a financial charge. At the mean time, you may need to click the above hyperlink to visit the new site.


Malshan Peiris (lmpeiris)

Permalink Leave a Comment

Play music on command line

January 19, 2011 at 12:53 am (Audio / Video, console based, linux apps, Linux codecs) (, , , , , , )

There are many popular audio players for linux; to name a few they would be XMMS, audacious, amarock . But all of these players are GUI-based; and they does not feel like ‘home’ when you are so addicted to using the command line. So i did some searching on possible options, and i found many. I’m only listing two of the best here, mplayer and Music On Console (Player) or MOC. The command line players have the advantage of less crashes, less usage of resources, playability in CUI only OS’s, remote logging to play sound files on a remote machine. The MPD (music player daemon) server can stream audio using any sound output server to a remote machine.

Music on Console (MOC or MOCP) (homepage)

moc player

MOC 2.44 running on my fedora 14 (64 bit)

The MOC has a decent UI for a CLI program. Consists of two panes, one displaying the music files in the current directory and the other showing the current playlist. Play stats and tag info are displayed in the bottom, with the progress bar. Navigation within playlist and song can be done using keyboard. It’s updated support for pulse, OSS and alsa output.


for debian based sytems use, sudo apt-get install moc

for rpm based systems, it is unlikely that moc would be on the repository. Try yum install moc and see. If that’s the case, we need to compile from source which can be downloaded from this link. You would need following packages

for mp3 support: yum install libmad-devel libid3tag-devel

for wma,mp4,aac,ra support: yum install ffmpeg-devel (This caused a compile error due to ffmpeg-devel relocating its .h files .. so please don’t do this if you know how to use linking). To activate support for other codecs, see README.

then decompress the file, go to the folder and type usual

./configure && make && install as root to compile and install. See my article on compiling from source for more information.

After installation to run, type

mocp <filename>

press q and type mocp -x to end program. Otherwise the server will still be running🙂



Mplayer running on console

This doesn’t need much introduction; which is as my perception is the best multimedia player ever, which can play videos too. Apart from the gmplayer (or mplayer-gui), the mplayer command can be used to play music on command line, with showing in depth info. Mplayer can play any audio file, it’s heavily stable and takes less resources. You can see my previous post on mplayer.

to install,

sudo apt-get install gmplayer (for debian based systems)

yum install mplayer-gui (for rpm based systems)

Permalink Leave a Comment

cowsay:How to make a cow talk on terminal startup

January 17, 2011 at 6:04 pm (console based, linux apps) (, , , , , , , )

Cowsay/cowthink (visit homepage at )

My hacked terminal startup using cowsay and figlet

Now you know what they use in Linux Mint to make a cow “talk” when the terminal starts🙂. I’m going to tell you how to do it in a much more awesome way. The programs they use are cowsay and fortune-mode. Cowsay can show any string, a text output of a command,program to appear as a dialog generated by an ascii art (including cow).

You can install program by

sudo apt-get install cowsay (in debian-based systems)

yum install cowsay (rpm based sytems)

Now try these in the terminal

cowsay “$USER is my boyfriend” ($ shows string value of environmental variable USER)

ls | cowsay (output of ls is piped to cowsay)

There are options to decide the appearance of the cow and its eyes. For example the switch -d will cause the cow to appear dead. But the most important hack can be done by -f which forces to use a files called cowsay files, so that we can get anything other than cow. Type

cowsay -l

to find the path of these files. Try each file with following syntax

cowsay -f <cowfile name, without extension> <string to display>

ex: cowsay -f tux $USER

The cowfiles can be edited to appear as we want. Only edit the stuff rounded by the circle. Do not edit anything else, and save by giving a different file name. Or we can simply download an ascii art from internet and replace the stuff in rounded area.

Editing a cowfile - only edit stuff on the red area


You can download ascii arts from sites like or simply you can build your own using these programs I’m not going to mention about them here. But the program Jave5 is a good one with a easy to use GUI, and runs on linux.

Linux mint uses an additional program called fortune-mod to get’s stuff. Other than the command cowsay, cowthink can be used to appear like the cow is thinking, not speaking.

Fortune-mod (The classic fortune cookie program)

Fortune-mod is so far the funniest FOSS program i have come across with. It generates a random text string of a joke, a saying by a great person, something useful etc… Can’t be saying it all just try it by yourself. I haven’t seen the same thing twice after using it for some time.

Sudo apt-get install fortune-mod (for debian based systems)

yum install fortune-mod (for rpm based systems)

to use type fortune in the command line.

This can piped to above cowsay easily

ex: fortune | cowsay

a fortune | cowsay piped output

for other options of fortune, refer man pages.

Making the cow talk

To make this show on terminal startup (or run any program) you can put the above command in your bashrc file.

gedit ~/.bashrc

and add above commands, as you wish in it. You can add figlet command too. Open a new terminal to see the change.

So, mine produced something like the first picture in this post by these commands

cd ~/Desktop/figlet222/

./figlet -c -f script “lmpeiris rocks”

fortune | cowsay -f love

cd ~

Permalink Leave a Comment

Figlet: Show text in ascii art

January 17, 2011 at 2:06 pm (console based, linux apps) (, , , , )

Mmmmm…….. it’s me after a long time writing on this blog. I’ve almost forgotten that there was a blog of mine just waiting to be refreshed by it’s author. Within the last year i changed my PC and the operating system twice, and now using the Fedora 14 x86/64 edition. So I guess we should start from some ascii arts on the terminal to show up whenever it’s started. This type art is familar to the Linux Mint users who will see an animal saying something in ascii art upon start of each terminal. However, it requires use of multiple commands. This post is part one.

Figlet – print a text as a logo

Happy new year on figlet version 2.22

How to install – figlet is not provided with most of the Linux distributions, and it’s not included in their package hosting servers. Hence using yum or apt-get simply may not work. If that’s the case, we need to compile it from the source. You can download the source from here. Decompress it and change in to it’s directory in the terminal and type;


to compile figlet. For further information on compiling from source, see my article. Unfortunately, there’s no make install here so we need to change in to figlet directory always to use the command (you can copy the binaries and man pages in to correct directories but this is complex).

cd <path to figlet folder>/figlet222/

./figlet <the text you want to show as a logo>

figlet features

I’ ll be mentioning only the important ones here.

Change style/font : A sub directory called fonts holds some .flf files which contains style info.

./figlet -f <fontfile_name> text

ex: ./figlet -f script Happy new year will produce something like above picture.

change alignment : To center -c , -l for left and -r for right

change right to left: -R

layout: -t will fit the text to terminal width, -P paragraph mode

figlet using some tweaks above

There’s a lot more; see figlet man page for more details.

Part 2 of the post is about using cowsay and fortune-mod. Will be followed soon.

Permalink Leave a Comment

Next page »


Get every new post delivered to your Inbox.

Join 42 other followers