Monday, March 27, 2006

Classes, classes and classes

I'm talking about Java - its a "classy" language. Notice the double quotes - so I define the word classy - its all about classes and classes. Everything has to be a class. There is no concept of globals, enums, structs and all those other fancy stuff that C++ supported to have backward comaptibility with C code. Imagine if you had to define some enum for color. In C++ I would just do the following:

enum colors { RED, BLUE, GREEN, WHITE, BLACK };

Somewhere in my code I could use RED, BLUE, GREEN for equlaity checks or case statement checks.

How do I do the same in Java? I have to create a class and make all these as static constants

public class Constants
{
public static final RED = 1;
public static final BLUE = 2;
...
...
}

Use it in the code as Constants.RED, Constants.BLUE, ...

Ain't it a charm? It depends on how you look at it. With enums life becomes easy to program as you dont have to go through all the roller coaster ride of defining a class structure and making it static. Hence the compiler can be a bit relaxed in these situations BUT there are bigger problems with enums which I will explain in another blog some other day.

One thing about Java - it's a more disciplined language and it forces the programmer to maintain the discipline as there is no alternative way of doing it. No wonder new comers to the OOP world find it easier to learn Java than C++ and old-timers in C++ sit and try to come up with wierd topics for their blogs.

Log in and Password issues in WinCVS

All this while I was using WinCVS 1.3 Beta cycle and it had a Log in... and Log out... feature in the menu that would allow you to login and logout of the cvs. The annoying thing is that everytime you try to login, you have to enter a password. at least I don't know of a way in which one could save the password for picking it later.

After getting spitted on too many times today I decided to go for WinCVS 2.0 and they do have a feature of storing the password and asking WinCVS to connect with those settings. Dont get too happy - the password is not encrypted, it is plain text and it is passed as an argument to the cvs login as -d. So if someone were to come to my machine, they can get the password which I use to login to my CVS. I guess it is ok for certain cases, but for most of the cases it is not, as you need to make sure your workstation is locked when you are not there OR when you are trying to catch a nap in the afternoons when your boss is not looking and someone tries to steal your password.

If someone knows how to ***** the password in WinCVS please do let me know.

WinCVS kept spitting at me

This morning WinCVS nearly made me pull every hair out my head. I kept getting this stupid message everytime I tried to do something on a checked out sandbox in my home directory:

cvs log: Empty password used - try 'cvs login' with a real password

cvs [log aborted]: authorization failed: server [blah] rejected access to [blah blah] for user [blah]

***** CVS exited normally with code 1 *****

The funny thing is though other modules were ok. It was only one particular module. I upgraded to WinCVS 2.0 and it kind of looked ok but boom - there it was again. Googled for it and found many people had similar issues on NT and Win Server boxes. Anyway one guys suggested that it might be because of an already checked out sandbox that you are trying to do something and something has got screwed up.

To make the long story short - I renamed the diretory and checked out the module again and Voila - it worked. Funny thing this WinCVS - we will never know why it ever happened. Anyway I have to get back to fixing some bugs and committing them. Catch you all later.

Saturday, March 25, 2006

3d FTP-pro - truly a multithreaded FTP client

Much to the respect of free software, I started to hunt down some freeware FTP upload softwares. I had tons of pictures to upload to my gallery and I wanted something that was better than Windows Explorer FTP. Its slow and clunky. I don't like it that much. After a lot of searching, I did manage to get a few softwares that claimed to be "Multithreaded." I used them and found that they were not multithreaded, or at least in my understanding of the word they were not multithreaded.

This whole thing intrigued me in trying to sort out the meaning of Multithreading and Multiple threads. Whats the difference? Well, Multiple threads is much similar to parallel processing where the job is chopped into various chunks and performed by different parts of the system and later combine to give one result thereby achieving a faster performance factor. In our case all those SWs that claimed were Multithreaded, were indeed multiply threaded - meaning a single file upload could happen in several chunks, wherein several chunks of the file could get tranferred simultaneously by the use of time slicing. Its good BUT still it is transferring a single file though and not multiple files.

So what is Multithreaded? According to me, a mutlithreaded FTP app is one that can have multiple threads to transfer multiple files at the same time. Each thread maintains its own TCP connection and does it own tranfer meachanisms. So if I initiate a 10 file transfer, with true multithreadedness I can upload 4 (for example) at a time (sharing bandwidth) and finish my job much quicker than what it would have taken to transfer one file at a time.

One may ask - how do you know parallel processing using multiple threads is not faster than multithreaded applications? The answer is simple - I do not know. I can only go as far as what I see on the screen and what works for me. I used all the SWs that claimed to be multithreaded, but not one of them matched the trial version of 3D FTP Pro's true multithreadedness both in terms of speed and ease of use. Check out the SW @ http://www.3dftp.com

It is definitely faster than all the other SWs I have used. As always the rule is - what is really good, is not free (not that all paid SWs are good), I paid about 30 bucks and got the SW and I'm enjoying uploading my pictures (each picture is about 4 MB) to my gallery on my Comcast account. A simple chart shown on the 3dftp's website shows that the SW makes other SWs eat dust.

Friday, March 24, 2006

Trying my hands at morphing

Just installed Adobe Photoshop Elements 4.0 last week and it rocks. I'm a newbie to the SW but I could figure my way around the stuff. Thanks to almost 5 years of basic Photoshop experience. My mom, dad, Priya and myself were all seated around the computer watching some photographs. Thats when it occured to me - why not try to morph some pictures. Guess what picture I picked up - the picture taken during the my parent's arrival @ SFO.

Loaded up the picture in Photoshop Elements and chose the Lasso Tool. Using the Lasso tool I selected my face in the picture and pasted it to another layer. Then after choosing the original layer and using the same Lasso tool selected my dad's face and pasted it to another layer. So now I have 3 layers. Moved my face onto my dad's body and my dad's picture onto my body. Obviously the skin tones were different so I had to do some spot fixing. For this I first saved the psd file as a jpeg and then opened the jpeg file. Then I selected the Dodge tool and dodged the skin tone differences trying to bring both the skintones as much close as possible. Finally saved the jpeg and gave my dad a hair-jump. Check it out:



Data Member Initialization in Java and C++

If there is one thing I love about Java - then it is the data member initialization. You can initialize the data member right when you declare it. Unlike in C++, you have to initialize it in the constructor.

For example in C++ I would do something like this:

class A
{
private:
int i;

public:
A() { i = 0; }
...
...
~A() {}
};

In Java, I would do the same as follows:

public class A
{
private int i = 0;
A() { ... }
}

This is a very simple example and you wouldn't know the difference of the two, but think about a complex project when you have to scroll up and down to look at member variables declarations and their respective defintions in the constructors. Life would have been so much more simpler if languages stuck to one semantic.

Oh lord - why can't there be a standard for braces?

I hate the way Java folks use the braces in their code. It is so difficult to find matching braces when the opening brace happens to be in the same line as the method declaration or a block scope.

Here is what I'm talking about:

public void foo() {
...
...
...
if (...){
...
...
for (...) {
...
...
} // end of for (...)
...
...
} // end of if (...)
} // end of foo()

Arghhhh!! I hate this. I spent almost few hours trying to find out why a certain brace was matching any open braces. The stupid open brace was sitting snuggly with a for loop in the same line.

I don't care how Java folks program but here is how I'm going to stick to - even in Java programs

public void foo()
{
...
...
...
if (...)
{
...
...
for (...)
{
...
...
} // end of for (...)
...
...
} // end of if (...)
} // end of foo()

Even the context sensitive editors have it built into them for Java files - to give braces as most Java programmers do. Luckily you can change it and I have it changed - in NetBeans IDE. I feel this was mainly adopted as a change that was something different than what C++ folks used to do and Java was first adopted by its fanatics who did not like C++'s way of doing certain elegant things. They just wanted to be different, but for me - its annoying!

Wednesday, March 22, 2006

Java: No block scope for access specifiers

Its already been 3 months since I have started coding in Java "fulltime" and being a C++ guy for almost 5 years, I sometimes wonder why the creators of Java did not think of having block scope for access specifiers. I have to precede every data member/method with the access specifier - private/public/protected. If I dont give anything it defaults to "friendly".

In C++ I can do:

public:
...
...
...
protected:
...
...
...
private:
...
...
...

In Java I have to do the following:
private ....
private ....
private ....
public ....
public ....
public ....
protected ....
protected ....
protected ....

Why, why, why? This keep daunting me - but then, this so called feature (or incapability) sometimes has its own advanatage:

Code becomes more pristine to read. I can look at a member variable or method and clearly tell its access restrictions without having to scroll up and down. Imagine I have 50 member variables that are private and I'm somewhere in the middle trying to read some data member and I have to scroll up to see what the specified access was. I have no clue otherwise (lets assume people do not follow coding standards for private/protected/public access specifiers). Think about the situations when people have multiple scopes opened and have declared member variables/methods scattered around the class. It becomes very difficult to read the code.

Many times I have seen people write code in C++ as:

private:
...
...
...
almost 25 lines
...
...
protected:
...
...
...
private:
...
...
...
protected:
...
...
...
The vicious cycle of declarations continues...

It becomes so difficult to read such code. Hence, its not so bad afterall in Java to completely avoid this obfuscated way of declaring member variables/methods. But still I wonder - was this a motivating factor for the creators to create it this way or was it some other factor?

It doesn't hurt to think of another possibility - since Java code is byte interpreted (the class files are all bytecodes), it might be a little too much pressure on the VM to store all the locations where the block scope access specifiers are for a particular data member/method. But then again - the compiler could use some intelligence and put in the correct access specifier for a member variable/method while parsing for the information and still maintain the same byte interface for the class code. This would definitely put more work on the compiler before it can get you a class file. Forget about people hitting you on the head for Java code to be slow, you will be whacked for suggesting to make the compiler also slow.

I guess at the end of the day it helps to have a cleaner looking code than a mushy spagetti kind of looking code as in C++. Goodbye block scope type specifiers.

Tuesday, March 21, 2006

Sorting an array based on the index

I was speaking to Harsha this evening and he asked me an interesting question: How to sort an array...? This question sounds simple enough but the question is not yet complete - the output of the sort should not be a sorted array of the original elements, but should be another array that has the sorted indices refering back to the original array.

For example take a 5 element array:

A[0] = 55
A[1] = 1
A[2] = 75
A[3] = 11
A[4] = 27

If I were to sort this array in ascending order then it would have resulted in

A[0] = 1
A[1] = 11
A[2] = 27
A[3] = 55
A[4] = 75

Now to sort the indices, if you have another array named as B[] then B[] should contain the sorted indices of A[] as follows (ascending order):

B[0] = 1
B[1] = 3
B[2] = 4
B[3] = 0
B[4] = 2

The solution is pretty straight forward - Instead of sorting based on A[i] just sort based on A[B[i]]. Use your favorite sorting algorithm and the runtime will be as bad as whatever sorting algorithm you use. For this simple case lets use Bubble sort:

for (i=0; i<5; i++) B[i] = i;

for (i=0; i<5; i++)
for (j=i; j<5; i++)
if (A[B[i]] > A[B[j]])
swap B[i] and B[j]

Monday, March 20, 2006

Wishlist: MDI for PuTTY

I use PuTTY all the time. It is the best client for SSH. Its easy to use and easy to setup. Amidst all the nice features it has there is one drawback - when I have multiple putty sessions open, there is no one single MDI interface to control them. I have to reply on the Windows grouping of the task bar items in XP and sometimes it is not very convenient. It would really be nice to have one MDI interface for putty and have various tabs for the sessions much like today's text editors with tabs designated for different files open.

While I was looking out for my wish to be fulfilled I found something interesting that might interest you folks - Terminal Window ShortCuts (TWSC). Although this is not a MDI interface for putty sessions, you can still access your putty sessions or create new ones by using a keyboard shortcut. This way you don't have to keep traversing to the task bar and click on the grouped item and choose which session you want to switch to. I liked this feature and the best of this feature - It sorts the list of sessions based on which sessions you access and this sorting is dynamic. Try it out and believe me, it will make your life a little easier than what the WinXP task bar item grouping can provide for maintaining putty sessions.

Tuesday, March 14, 2006

IE 7 Beta 2: Tabbed browsing - Whew!

At last IE gets the tabbed browsing capability. I like it and now I have all the more reason not to move to Firefox. I like firefox but for me (this is only my personal experience), some sites take longer time to load and some sites do not load so well in firefox. I like the interface of IE and it new tab button right next to the last tab on the right. In Firefox it is on the left and sometimes you may end up traversing an entore horizontal screen just to create a new tab. There are other added seurity features in IE7 but for me the most important one was the tabbed browsing which I'm enjoying right now.

Thursday, March 09, 2006

Canon Powershot S80 - neat brother of the S70

After loosing my powershot S70, I had to buy another camera and I chose to buy the S80 that is the 8 MP brother of the S70. It is a neat camera with a sleek design and very light. the buttons are placed pretty intutively and the screen is 2.5 inch wide. Wow - Its pretty fast in starting up and closing. Its worth every penny - Do I really need to say this about Canon? Btw, the zoom on this model in 3.6x optical. I'm happy and contended.