Sunday, April 29, 2007

Lazy Evaluation and Caching...

... are the most widely used techniques for improving your software's implementation performance.

Then what are they and how can I use them?

Lazy Evaluation is exactly just like a Lazy Employee; he wont do the job unless some one directly requests it from him. Infact, he'll postpone it forever until the request is made official.

Caching on the other hand is simply storing frequently used data in some quick array or memory for fast retreival, although the same data is stored in a database or in a file, the programmer usually chooses to cache it in its accessible form for fast retreival. This memory could be an Array, a Berkley DB file, a tmp file or any other suitable media.

Google - a well known search engine - caches search results for most frequently used queries. FileNET (a really good CMS server) renders all content objects inside one single HTML page ready for access (instead of querying it from a database everytime). The HTTP protocol itself caches different requested pages on the internet on local internet proxies and implements the HTTP headers to do so. And even does your browser cache some html pages from time to time.

Thats why you usually need to press Ctrl+F5 to force requesting to refresh the page from the server.

As a programming example, lets say you have a small class named UserAccountManager and this class contains a single method:

public static String UserAccountsManager.getUsername(int UserId);

And a single private array which is simply a HashMap mapping a UserID to the Username which is used by getUsername(int UserId).

The method is implemented simply to execute a query on the database:

"SELECT USERNAME FROM USERS WHERE ID=" + UserId

Here is a simple way to see how caching and lazy evaluation can be applied.

The program can implement caching using a private array map that maps the user id to the username string. This array is filled only once from the database when the program first loads.

The tricky part when implementing caching is knowing when to re-generate the cache, or more accurately, how to identify that the original data store has changed or not? If changed, then the cache needs to be regenerated or else your program is using out-of-date data.
Any calls to getUsername will simply access the array instead of accessing the database. This is whats Caching all about.

Now, lazy evaluation simply changes the implementation so that the array AINT FILLED AT ALL until someone actually calls the getUsername() method.

The advantage here is that you dont waste memory or connect to the database until someone actually calls the function. (ps. It might need not be called at all).

After all this explanation, there is a very annoying advice that you have to stick to:

"Never implement optimization techniques such as caching or lazy evaluation until a bottle neck is identified in the system using benchmarking and testing procedures that shows that need. This simply means, avoid Premature Optimization as much as you can."

Friday, April 27, 2007

SQL Statement Shortcuts

What always bothered me when writing SQL SELECT Statements is handling long table names, and having to reference a field by its table's name to avoid ambiguous errors.

Take this for example, three tables: infobase, infobase_labels, and infobase_users. And let's imagine that those three tables each have a field named 'ID'.

So a simple select statement will go like that:

SELECT infobase.ID, infobase.NAME AS INFO_NAME, infobase_labels.NAME AS LABEL_NAME, PHONE FROM infobase_labels, infobase_users, infobase WHERE infobase_labels.ID=infobase.LABEL_ID and infobase_users.ID=infobase.USER_ID AND infobase.ID=5;

Okay, 238 characters of unreadable sql code.

Whats next? Okay, till here you have two options:
1. Live with it.
2. Find a simpler way.

I have been using option 1 for the last 7 years. Until, option two showed up ...

... the usage of name aliasing for table names to make a query simpler, now this I love, simple alias each table with a 2 letter name, and use it instead of the table name:

SELECT _IB.ID, _IB.NAME AS INFO_NAME, _IBL.NAME AS LABEL_NAME, PHONE FROM infobase_labels AS _IBL, infobase_users AS _IBU, infobase AS _IB WHERE _IBL.ID=_IB.LABEL_ID and _IBU.ID=_IB.USER_ID AND _IB.ID=5;

Okay, this makes them 205 characters long.

For me the latter is much more readable, shorted and easier to write.

Tidying the SQL statement a bit:
SELECT
_IB.ID,
_IB.NAME AS INFO_NAME,
_IBL.NAME AS LABEL_NAME,
PHONE
FROM
infobase_labels AS _IBL,
infobase_users AS _IBU,
infobase AS _IB
WHERE
_IBL.ID=_IB.LABEL_ID
AND _IBU.ID=_IB.USER_ID
AND _IB.ID=5;

Okay thats it for today. Quite a long post, but I like it ; )

Sunday, April 22, 2007

Master the Techniques Before Chasing the Goals

Miyagi told Daniel Son while holding the chop sticks:

"You have to catch fly first."

And this applies to any career. Ok, Miyagi was a bit over-reacting here, I mean, if it was not for the 'beginners luck' of Daniel Son, I dont think this movie would have ever ended.

The lesson here is that a career person SHOULD master the techniques of her or his profession. Know it well, do it right, know when to use a technique, know when to drop it, know the pros and cons of different techniques, but most importantly, Master it!

That could be the boring part for some, to master the technique, to know the details, to understand the bits and pieces.

A professional football player knows that mastering a 'pass' weighs much more than scoring a goal. Mediocre players - on the other hand - only care about scoring, this arises from a mentality of greed ... "Need to score, makes me feel good.".

Most low quality environments overlook developing good techniques, and favor jumping directly to goals. Ok, fair enough, the time and budget constraints of 'the start' could force such conditions. But, this symptom easily grows to a disorder when this methodology becomes a continuous habit.

Wisdom of today's post:

"Master the techniques, and make them the solid path to your goals."

Tuesday, April 17, 2007

Toggle The Toggler

We all use them, different places in our code, toggling variables.
And there are many ways to achieve the same results.


My first toggle looked like that. This toggles between 1 and -1.
a *= -1;

It then evolved to:
a = a XOR 1; // or a = a ^ 1 in Java

I also started using the modulus operator:
a = (a + 1) % 2;

Then I liked the modulus operator so much, that I started using it to toggle between three, four or N values:
a = (a + 1) % 4;

Sunday, April 15, 2007

Teach Yourself Programming in Ten Years

Very nice article discussing the programmers' learning curve:
http://norvig.com/21-days.html

This article is so popular it has been translated to many different languages including chinese, arabic, german, french, polish, japanese, korean, russian, spanish and turkish.

ref: Khaled J. Simtix Software.

Friday, April 13, 2007

Book Quote

"Some people play tennis all their lives, but never get better. Those people are not willing to take a fresh look at what they do or to consider changing it. Good players recognize that getting better often means making an investment in new approaches. For a while, they may get worse, as they wrestle with new and unfamiliar techniques, but eventually they surpass their old plateau."

-Fisher, Ury and Patton
In Getting To Yes

Wednesday, April 11, 2007

Getting Organized

I've seen different people do different stuff when it comes to getting organized.

Whether its organizng your images, favorites, invoices, note taking, meetings, budgeting.

Frankly, what works with some turns up to be impractical for others, and vice versa.

1. Some use the browser's favorites toolbar; others see it as non-sense.
2. Some use small notepad files to collect different websites, todo items, readings, passwords.
Others use excel sheets for the same task.
3. Some people even use stange but effective tools. An example is a tool that applies different colors to the different folders on a machine. Even others use tools that automate your environment through pre-configured shortcut keys and mouse clicks.
4. Some use information tools - called outliners - for collecting important pieces of information. Others see it as overly organized.
5. Some use outlook calendar for different reminders, others use their mobile, or even pen and paper.

Best general tips when adopting an organizing procedure is:

1. Make sure the maintenance cost is to the minimum when organizing an item. You dont want to spend an hour trying to figure out where to place your favorite site.
2. Make sure there is some temp storage for your information items that need to be organized. Since sometimes you're too lazy to organize even when you have the infrastructure.
3. Make sure that retrieval of the organized item is to the minimum (example, should be easily located, easily searchable by different fields, dates, text).
4. Make sure that it is available from different places.
5. Take time investigating organization tools at downloads.net, and tucows.com, see what fits your case, use what you find practical, make your life easier.

You never know, a fifteen minute deep thought of how to efficiently organize yourself, or even a small research in finding the right practical tool is your way for a much efficient and easier life.

The main hurdle that lets people postpone organizing themselves is their false feeling of 'I can manage' my reminders, my todos, my schedules, my information, and my budget. Trust me on this one, when it comes to organizing yourself, your brain alone is very limited.

Sunday, April 08, 2007

Big Downloads Cause Big Frustrations

These days, I'm downloading different IDE's, database binaries and (non microsoft) service updates from the net. The minimum download size is about 40 MB and the maximum was the new release of JDeveloper which is currently still being downloaded. Its about 508 MB in size.

This is the third time I try to download this 508 piece with a failed connection frustration.

Until... I found this wonderful free tool. Its a download manager named 'free download manager' that can be downloaded from http://www.freedownloadmanager.org/download.htm.

Enjoy downloading. No more connection disruptions. No more frustrations.

ref: Thanks Quazy (IT support team)

Tuesday, April 03, 2007

Bad Software Methodologies

Ok, this time its from the software industry...

Here are some very well known anti-pattern methodologies in software:

/* 1. Copy and Paste Programming: */
Copying (and modifying) existing code rather than creating generic solutions

/* 2. Golden Hammer */
Assuming that a favorite solution is universally applicable

/* 3. Tester Driven Development */
Software projects where new requirements are specified in bug reports

/* 4. Improbability factor */
Assuming that it is improbable that a known error becomes effective

/* 5. Defactoring */
The process of removing functionality and replacing it with documentation

/* 6. Premature Optimization */
Optimization on the basis of insufficient information

/* 7. Programming by permutation */
Trying to approach a solution by successively modifying the code to see if it works

/* 8. Reinventing the square wheel */
Creating a poor solution when a good one exists

/* 9. Reinventing the wheel */
Failing to adopt an existing, adequate solution

/* 10. Silver Bullet */
Assuming that a favorite technical solution can solve a larger process or problem

Ref: Copied and pasted from wikipedia

Patterns Plus Anti-Patterns

In every engineering science lies the notion of good (design) patterns.

This comes out as a result of repeating good ideas and/or methodologies that work. Once being repeated in different places, a pattern is spotted in which it gets popular between the engineering community and can be safely reused, it even acts as guidelines to beginners.

"Another important concept - but not as popular - is the concept of anti-patterns."

Good Patterns and Antipatterns complete each other. Anti-Patterns are simply the bad patterns that have been spotted being practiced repeatedly and should be avoided.

Its important to note that patterns and anti-patterns do not apply only to software engineering, but rather can be applied to any science. You can even apply it in your own kitchen (strong assumption: you have a kitchen).

I'll discuss here some anti-patterns spotted in the managerial world that have been popular and should be avoided:

1. Fruitless Hoop: The manager who requires endless (often meaningless) data before making a decision.
2. Golden Child: When special responsibility, opportunity, recognition, or reward is given to a team member based on personal relationships or contrary to the person’s actual performance.
3. Leader Not Manager: Being a great leader doesn’t necessarily mean being a great manager.
4. Manager Not Leader: The manager who is proficient at their administrative and managerial duties, but lacks leadership ability
5. Management Meeting Mania: The manager whose only function in the organization is to schedule useless meetings
6. All You Have is a Hammer: One-dimensional management where the same technique is used on all subordinates.

These will do for today. For more you can search google or checkout wikipedia. Keywords Anti-Patterns and Managerial. You can also check amazon.com for good books on the anti-patterns subject.

Sunday, April 01, 2007

Swing/AWT

Fortunately, I've found lots of books and online material that teach you Java swing/awt programming for desktop environments.

Unfortunately, very few (approximately none) discuss structuring your application code.

I've seen "Learning Java" over discuss the 'Adapter' design pattern when it comes to separating UI actions from application functionality. But still, I'm looking for patterns that will make code scale, techniques that will handle complex user interfaces using simple class relations without cross-referencing different variables, classes and elements along the code.

Till I meet such a tutorial or reference, I think I'll be treating Swing the MFC way.
Lucky me , I've done MFC ;)

In Search For A Convenient Java IDE

With all the years of VI programming on linux as my ultimate IDE. Being introduced to some civilized java IDE's exposed me to a cultural shock.

With all the impressing stuff, I'm still trying to find the best out there. Trying to find the best by comparison then by elimination. First of all, they all look great. The Java IDE's sum up to the following:

1. Eclipse
2. NetBeans
3. JBuilder (by Borland)
4. JDeveloper (by Oracle)
5. Intellij IDEA

The best link I found that does a fair comparison is this one:
http://www.mojavelinux.com/wiki/doku.php?id=javaidecomparison

I'm trying them out one by one. Please note that my evaluation will be based on the best IDE for creating desktop applications.

I've just tried JDeveloper today and I think its the best. Eclipse, NetBeans and Intellij IDEA I've tried a bit. But the one that provides the best convenience I believe is JDeveloper, specially when it comes to creating GUI Desktop applications.

The problem with Eclipse is that it doesn't support Swing/AWT ui elements, but rather have to be purchased from thirdparty commercials (most notably MyEclipse).

Netbeans is good. But the layout facilities provided are so cumbersome. To that, NetBeans lacks alot of features, and its intellisense makes me mad from time to time. When I'm in the middle of typing a function name, and go back by pressing delete, intellisense dissapears. This doesn't happen with JDeveloper.

Intellij IDEA. I have to be frank here, I didnt give it a full try. Its commercial for $500 and looks great. Also lots of features, a big community, and lots of plugins, the only problem is that I didnt give it a full try since the GUI layouts it provided were a bit like NetBeans. I'm sure there is an option that makes it like JDeveloper. But I didn't find it fast enough.

The only one left to try is JBuilder from Borland. I dont think I will get there. I'm so happy about JDeveloper I dont think I'll ever go anywhere else ;)