Sunday, April 26, 2009

OOP Gotchas...

I was reading a book, "Learning Python", from Mark Lutz, and although a different topic from usual projects at work, I found one particular comment pretty interesting, and serve all languages in general, IMHO:

I once worked in a C++ shop with thousands of classes (some machine-generated), and up to 15 levels of inheritance. Deciphering method calls in a such complex system was often a monumental task: multiple classes had to be consulted for even the most basic operations. In fact, the logic of the system was so deeply wrapped that understanding a piece of code in some cases required days of wading through related files.

The most general rule of thumb of Python programming applies here, too: don't make things complicated unless they truly must be. Wrapping your code in multiples layers of classes to the point of incomprehensibility is always a bad idea. Abstraction is the basis of polymorphism and encapsulation, and it can be a very effective tool when used well. However, you'll simplify debugging and aid maintainability if you make you class interfaces intuitive, avoid making your code overly abstract, and keep your class hierarchies short and flat unless there is a good reason to do otherwise.
Nice words to remind us of the KISS principle. http://lh4.ggpht.com/_HO2u4R9azX8/Sdiirg5JhAI/AAAAAAAAAik/CuhUaBPD3T4/LOL+Animated.gif

Thursday, April 23, 2009

MySql... what future expects

After the bombastic news of database giant Oracle purchasing Sun, the open source community was shaken up to the obscure future of the MySQL. Reading the opinion of its creator, Michael Widenius, things do not look like promising to the community. Check out his blog Monty Says: To be (free) or not to be (free)

Saturday, April 18, 2009

Databases: fixing the legacy dirts

We were brainstorming a little bit on how to solve some of the most common issues every company faces, specially those with rapid growth jumping from small to medium business. The thing is, most of the tables and structure are, in terms of modeling, really dirty, with full of duplication, wrong indexing, crappy namings, etc.

One optimum solution would be to simply re-model the system. Wow, that is easy to say. I wish it was that easy to implement. Thinking on a simple billing table that is used for basically everything on a company, changing it would mean changing all reports, all interfaces, all behind-the-scene processes, applications, etc. Ok, too much work, in deed.

What if we could create a new model and at the same time use the legacy just pointing to this new model? Like a view or something... We could implement a new table or tables to serve the purpose of this specific legacy table, and leave a view with the same signature (names, columns, etc) that would work transparently for the rest of the legacy system.

If you are just talking about a single table, that's not actually so hard thing to do, but re-modeling and putting order to the mess usually means more than that. The problem we need to solve then is: how to create this view that would behave just like the work as if the table was there, but pointing to a better modeled environment?

Searching a bit, we found an interesting approach to that solution, which is to trick the insert/update triggers of the view:




-- Create a sample table A
CREATE TABLE tableNameA
(
tableNameId int PRIMARY KEY,
valueA varchar(10)
)

-- Create a sample table B
CREATE TABLE tableNameB
(
tableNameId int PRIMARY KEY,
valueB varchar(10)
)
GO

-- Create a view which looks to both tables A and B (join)
CREATE VIEW tableName
AS
SELECT
coalesce(tableNameA.tableNameId, tableNameB.tableNameId) AS tableNameId,
tableNameA.valueA,
tableNameB.valueB
FROM tableNameA
FULL OUTER JOIN tableNameB
ON tableNameA.tableNameId = tableNameB.tableNameId
GO

-- Create the trigger that will enable the trick to insert in both tables separately
CREATE TRIGGER tableName_insteadOFInsert
ON tableName
INSTEAD OF INSERT
AS
BEGIN
SET NOCOUNT ON

INSERT INTO tableNameA(tableNameId,valueA)
SELECT tableNameId, valueA
FROM inserted
WHERE valueA IS NOT null

INSERT INTO tableNameB(tableNameId,valueB)
SELECT tableNameId, valueB
FROM inserted
WHERE valueB IS NOT null
END
GO

-- Create the trigger that will enable the trick to update both tables separately
CREATE TRIGGER tableName_insteadOFUpdate
ON tableName
INSTEAD OF UPDATE
AS
BEGIN
SET NOCOUNT ON

UPDATE tableNameA
SET valueA = ( SELECT valueA FROM inserted )
WHERE tableNameId = ( SELECT tableNameId FROM inserted )

UPDATE tableNameB
SET valueB = ( SELECT valueB FROM inserted )
WHERE tableNameId = ( SELECT tableNameId FROM inserted )
END
GO

-- Done!

-- Testing...

-- Insert some data into the tables A and B thru the view
INSERT INTO tableName(tableNameId, valueA, valueB) VALUES (1, NULL, '10')
INSERT INTO tableName(tableNameId, valueA, valueB) VALUES (2, '20', '20')
INSERT INTO tableName(tableNameId, valueA, valueB) VALUES (3, '30', NULL)
GO

-- Displaying the results on screen
SELECT * FROM tableName
GO

-- Update some data of the tables A and B thru the view
UPDATE tableName SET valueA=100, valueB = null WHERE tableNameId = 2
GO

-- Displaying the results on screen
SELECT * FROM tableName
GO

-- Just clean up this mess!
DROP TABLE tableNameA
DROP TABLE tableNameB
DROP TABLE tableName
GO

-- Cool, isn't it? (EOF)




If you try to insert directly into the view without adding the InsteadOf triggers, this is the error SQL-Server will return to you
Server: Msg 4406, Level 16, State 1, Line 1
Update or insert of view or function 'tableName' failed because it contains a derived or constant field.

And here is the result of this example:

(result of the inserts)

tableNameId valueA valueB
----------- ---------- ----------
1 NULL 10
2 20 20
3 30 NULL

(result of the update on id=2)

tableNameId valueA valueB
----------- ---------- ----------
1 NULL 10
2 100 NULL
3 30 NULL

Source:
[MSDN]: INSTEAD OF INSERT Triggers

[MSDN]: INSTEAD OF UPDATE Triggers

Wednesday, April 15, 2009

What can we learn about usability…

To relax. hehehe

Tuesday, April 14, 2009

Computer Science without computer? It's possible???

This is a proposal of the site Computer Science Unplugged with a series of computer knowledge to make without computer such as binary numbers, algorithms and data compression through games and puzzles that use cards, string, crayons and lots of running around.

It is easy and very fun.

Monday, April 6, 2009

L10n?

I first saw this terminology in SplendidCRM code, and did not quite understand why they had it with that name, and just learned how to use it. Today, I was watching a Django video, when the guy presented the following item in the slide:

  • i18n / L10n
I got curious about it and googled it, to find that L10n means "Localization" ("L" followed by 10 chars then "n") and i18n stands for "Internationalization" ("i" followed by 18 chars then "n").

Funny way to write long words...

Source: http://en.wikipedia.org/wiki/Internationalization_and_localization

Friday, April 3, 2009

Online meeting - Desktop Sharing App

I needed to do desktop sharing with one of my friend, and was looking into several desktop sharing / online meeting apps which are FREE. And I found a very good application that is quite simple and easy to setup, and this is my choice. It called Mikogo (http://www.mikogo.com).

It can by pass firewall and you don't need any static IP and it is very easy to setup a meeting.
You can have online meeting with multiple users. It allows the meeting attendees to switch to become presenter, or even the take control of the presenting computer. Oh, only one computer can be a presenter at a time. Another feature that I like, that you can choose which application that can be shared. So, you can keep all the running apps in background while you are in the meeting and not worrying that the other may peek at your mess desktop :)

The lacking is, it does not have chat, voice or video yet. Even so, I don't see that as a big problem. I just combine it with Gmail Video chat and the problem solved. I am afraid if it has the video chat, the desktop sharing will get slower, since currently I am very happy with the response time.

Take a look if you have time.