Showing posts with label Tips. Show all posts
Showing posts with label Tips. Show all posts

Tuesday, June 24, 2008

Java One Liner - Reading a Text File at Once

Java is not meant or known for any one-liners, but who said we can't try? There you go:
String contents = new Scanner(new File("filepath")).findWithinHorizon(Pattern.compile(".*", Pattern.DOTALL), 0);

Cool, eh?

Hmm, may be not. Of course, you still have to deal with the checked exceptions + 3 import statements, etc. The so called one-liner by me has 113 characters (could be more or could be less if you consider the file path's length). I'm not sure if I would consider anything more than 80 chars as "one-liner" but hey, this is Java and this is as close as you can get.

Just for fun. Do you have anything better than this in Java? How about in Java 1.4? Now, don't write a separate method, call it, and say it's a one-liner for Perl sakes!

Friday, April 4, 2008

The Anatomy of a Command Line

Command Line

Command line needs no introduction among the computer folks. We all know what it is. Yet, many of the developers are really scared to work on the terminal; they rather prefer GUIs. Command line somehow makes them uncomfortable. They think it is tough. In fact, when they design their own application's command line interface, they forget some of the simple and basic things they should have taken care. It will be one of my agendas in this post.

You may read this post even if you are not a developer, this isn't a developer specific post.

Misconceptions/Preconceptions

There are two kinds of people who don't prefer command line:
  1. The ones who've used and quit because it looked tough at the beginning
  2. The ones who've never tried (I call it the startup-syndrome)
I went through both the phases until the bulb glowed over my head. It shouldn't be tough, computers are easy to use. After all, command line is not programming. It is aimed at the end user. The Operating System designers must have came up with some pattern before implementing these commands. Yes, the keyword is pattern.

The Basic Syntax

A command consists of three main parts:
  1. What to do?
  2. How to do?
  3. Other Inputs
Can You Show Me an Example?

Yes My Lord! Sure. Let's take an example. We will see the command line interface of the tail Unix utility. Here are the first few lines from the man page and I quote (please read it thoroughly once):
NAME
tail -- display the last part of a file

SYNOPSIS
tail [-F | -f | -r] [-q] [-b number | -c number | -n number] [file ...]

DESCRIPTION
The tail utility displays the contents of file or, by default, its stan-
dard input, to the standard output.
The part that is found difficult and that I would like to explain here is the SYNOPSIS. Remember the three main parts? The what? The how? And the other inputs? Let us see these three parts for this tail command.

The How part can be a little tricky for the newbies. You may even wander what is -f? -n? number? the "|" sign? Actually, that is what makes the command line interface friendly. This little table should help you understand about the syntax.
Anything inside []          = Optional
... = Can occur more than once
-something (or --something) = The How
option1 | option2 = Either option1 or option2 can appear

The "how" part can consist of two kinds of options. 1) Self descriptive options 2) Options that need more input.
tail -v -n 25 logs.txt
The "How" in the above command tells the tail command to print the last 25 lines of the file logs.txt and print the output in verbose mode. The below figure explains better.

The Design Difference

In a well designed graphical user interface, everything is presented to you. You need not remember or search for anything (like the -n option). GUIs follow the "push" model. Everything is pushed to you and you get to choose. You learn and discover things quickly. Whereas, in a command line interface, you need to tell what you want to do. You need to search and "pull" the options from the manual and tell the command "how" you want to execute it.

How *NOT* to Design a Command Line Interface

Have you ever used a command line that was like:
doSomething 49595 39504 "/usr/local/file" "/home/srikanths/dir/file"
You got the point. The "how" and the "other inputs" parts are totally messed up. You don't know the meaning of the numbers you are passing and even if you have a manual about this command, you need to refer it every time you use and you have to double check to make sure you pass the arguments correctly. A command line nightmare! Now, see the same command's better version:
doSomething\
-port 49595\
-fallbackPort 39504\
-configFile "/usr/local/file"\
-fallbackConfigFile "/home/srikanths/dir/file"
So, when you design a command line interface, make it more user friendly and follow the common pattern that is already there for decades now. There are many modules available either out of the box or as frameworks that can parse the command line arguments for you. Just search which ones are the most popular CLI frameworks for your language and choose one from that. Hope this post helped you!

Bottom Line

"The next time you tell someone RTFM, tell them how to RTFM."

References
Other posts

Tuesday, February 26, 2008

Cool Firefox Tips

Firefox is my favorite browser despite it's appetite for memory. I use a few command line options and extension that is worth sharing here.



Firefox Profiles: Do you use more than one profile in Firefox? It's a very useful feature. I use it when I would like to log into multiple Gmail accounts at the same time (or you can also use it if you don't use your Gmail account for other Google services such as Reader and Orkut). My main use of the "profiles" feature is during development and testing. You can have one Firefox loaded with all the extensions in the world like Firebug, XPath viewer, various themes, etc. and you can have another light-weight Firefox with a very few extensions which you always need.



So how do you create and use another profile?

$ firefox -profilemanager -no-remote
This opens up the following self explanatory profile launcher window:





As you can see, I have one 'default' and one 'dev' profile. To open a particular profile:

$ firefox -p profile_name -no-remote
The "-no-remote" option is used from Firefox2+ when there's already a profile running.



Firefox Extensions: This is what makes Firefox truly a personal browser. You can create your own extensions or you can download and install the existing ones that are available on the internet. Typically, you browse for an extension and you install it using the "Install" button that is found on the website. But when you do it in this way, the installation only happens for that current profile you are using. To install an extension for all the profiles follow the steps below:



1. Instead of directly clicking on the "Install" button, use right-click button and select "Save Link As..." and save it to your local hard drive. Typically this file is a .xpi or a .jar file.



2. Type the following command on your terminal (or Start -> Run on Windows):

$ firefox -install-global-extension "path_to_saved_extension_file"
# for example:
$ firefox -install-global-extension "c:\downloads\firefox\firebug.xpi"
My favorite Firefox extensions are:

Other recommendations:

Happy browsing!

Srikanth



PS: One more feature you may want to turn on. Firefox on Ubuntu selects the complete URL when you double click on any part of the URL. Try double clicking on any single word on this this URL: http://codeblog.srikanths.net, if you could select the complete URL then you are doing good. Otherwise, follow the steps:

  1. Go to Location bar on Firefox (Alt + D)

  2. Type about:config and press ENTER

  3. In the Filter text box, type this: layout.word_select.stop_at_punctuation

  4. Double click to change the value to FALSE to enable URL selection while double clicking.

I learned this trick from this blog post, so all the credit goes to him/her for this tip.

Monday, February 11, 2008

Filter Your 'Tasks View' in Eclipse Using Patterns in TODO

The Feature: Eclipse's Tasks is one of my favorite features in the IDE. You can set reminders right there in your source code and you can see all of them in the Tasks View of the IDE which you can dock it anywhere you want. A sample screenshot of the Tasks View can be seen below.





The Problem: As you can see from the sample screenshot that many people added their own TODOs and FIXMEs. Imagine this for a big project, this will be in hundreds. The point of adding TODOs and FIXMEs right in the source code is that it can be seen more often such that we will be constantly reminded about the unfinished. But when everybody adds, it becomes a problem to keep track of your own Tasks and we may start ignoring them like spam mail. Having control is the key to using this feature, just like e-mail.



The Solution: Fortunately there's a very simple solution to this problem with a little effort. You can create and configure filters, just like you can create filters for your e-mails in Gmail. On the Tasks View you will see the button using which you can create filters. See below:



The rest of the filter configuration work is simple. The Filters dialog pops up and it's very intuitive as what all you can do with it. I typically create filter that contains the string "(srikanths): " in its description. And when I write my TODOs and FIXMEs, I make sure I prefix them with that description. You can come up with your own standard within your team and follow that.



The tasks that I write in the code typically look like this:



// TODO(srikanths): Srikanth should come back to complete this.
// FIXME(srikanths): Srikanth should fix this ASAP or he is dead.






Happy coding!

Srikanth

Friday, April 27, 2007

Eclipse :: Configuring Eclipse Startup

If your eclipse is starting up a little slower or if it's running a little slower or if it gets an OutOfMemoryError, then just go through this checklist:-

  • Is it using the latest and the fastest JRE?

  • Is it having a good minimum and maximum memory?


And then follow this lovely blog:-
http://swem.wm.edu/blogs/waynegraham/index.cfm/2006/9/7/Tweaking-Eclipse

Let me know if the link doesn't work :-)