Code Elegance

Archive for April, 2007

SQL the new Assembly

Is SQL the new assembly language?

Think about it.

The language that we are using for writing application is an hi-level language. Take C#. It runs in a managed environment, it abstract a lot of things, we don’t have to worry about low level interaction between OS and out app, the Garbage Collector keep memory clean.

Then when you pass to SQL to extract data from database you loose all this abstraction and you need to know every little issues (every byte!) to build a well designed database and to write a good query.

We (developer) need a new level of abstraction for the database interactions and I think that an ORM is a sort of that abstraction. But why the DBAs hate so much the ORM?

2 comments

Write all your test

I’m a big fan of TDD and I try to infect everyone with Unit Testing and TDD.

TDD is an hard practice, you have to be clever if you want that your effort is worthwhile. Why?
Because if you write tests that do not cover large parts (or some small critical parts) of your application you can throw your tests away: they are usefull!

Think about it. A unit test suite is useful when you do some refactoring, if somenthing is breaking a test shows it and show where it is the error. But if you don’t cover all your code, what does the green bar means?
That it’s all right or that the test can’t find the bug?

So write all the needed test, be clever! :-)

No comments

Exception Bubbling

Often in class during lesson people find difficult to understand how to manage the exception in a real word application.
Think about a 3 layer application (UI, Biz, DAL), the question is always: “How do I manage the DAL exception? Should I intercept the exception in the DAL and return an error code to the caller? Should I let the exception raise in the business layer? Should I catch the exception and rethrow it to the caller?

I try varius way but today the best one is:
1) Catch the exception in the DAL and rethrow a new custom exception (MyDalExcetion)
2) In the business layer catch the MyDalExcetion and throw a MyBizException to the UI Layer which can handle it and eventually show an error message to the user

It’s not always true that an exception has to be rethrow, should exists cases in which a layer can solve the problem by itself.

No comments

Code Elegance

The developer’s job has something to do with Art.
The solution to a problem is not unique and a lazy developer can write an algorithm that he used a lot of times to resolve the same problem.
But every problem is not like the old one and the smart developer find a better solution even if he knows that the usual one work fine.
Why because the new solution is more elegant ;-)

1 comment