Friday, June 4, 2010

Suicide coding

While working on my projects, something got into my mind... we all know that you cannot invest too much time in commenting your code or implementing classes that will manipulate database tables but is that really such a big deal? I mean imagine that today you start a new project which has no future but in 2-3 weeks/months it goes sky high or it gets a lot of attention -- I call this Suicide coding.
Why? well think of it this way: how many lines of code can one write in 2-3 weeks for example? I suspect somewhere between 5.000 to 10.000 or even more, is that suicide? Yes sir!
Now let's imagine that today you start on a project which is required to be completed in the next 5-7 days, of course you will focus on getting it's functionality working, some error checking here and there and before you know it, POWA! the project can be shipped as soon as it compiles.
Now a few days/weeks passes and your boss comes to you:
Boss: Hey, do you remember project X -- you worked on it like few weeks ago...
You: Ya, so...?
Boss: Well, it got some attention and we need a new release, here's the list of features that needs to be implemented in the next 7 days.
You: Holly crap! that's like 20+ features to be implemented and a lot of stuff needs to be changed.
Now that's a typical way of getting pissed in the pocket, do you guess what will happen next?
1. Read the entire list of stuff you need to do and smoke a pack of cig's until you reach half of the list.
2. You start searching for queries in between shietload of lines of code...
3. OMG, not only that you have to add new tables and fields(to existing tables) to database but you also need to modify existing tables/fields...
4. Ok that day came to a end, finally you can go home, but WAIT! you cannot sleep tonight because you now have 6 days left to finish your (part of the) project, damn...
I'm very positive that the above situation happened to a lot of you guys out there and still happens, but still you stick to suicide coding...
How can you avoid this kind of situation? well it's not that hard, just comment the freaking code as much as possible, while you think of how to make something work you can also comment existing uncommented code -- that's a very good way to maximize your productivity.
Here's something even better for database stuff, define a base class that has some generic methods for setting the SQL string, grab fields, etc. and derive new classes from it that will manipulate tables:
  TBaseQuery = class
    FConnection: TDatabaseConnection;
    FQuery: TQuery;
    function IsEmpty: Boolean;
    function FieldByName(const AFieldName: string): TField;
    procedure Execute;
    procedure Open;   
    procedure SQL(const s: string); overload;
    procedure SQL(const s: string; const args: array of const); overload; 
    constructor Create(OnConnection: TDatabaseConnection);
    destructor Destroy; override;
  TQueryEmployee = class(TBaseQuery)
    function EmployeeAdd(const FirstName, LastName: string): Boolean;
    function EmployeeCanDelete(IDEmployee: Int64): Boolean;
    function EmployeeDelete(IDEmployee: Int64): Boolean;
    function EmployeeCount: Int64;
    function EmployeeUpdate(const FirstName, LastName: string; IDEmployee: Int64): Boolean;    


constructor TBaseQuery.Create(OnConnection: TDatabaseConnection);
  FConnection := OnConnection;
  FQuery := TQuery.Create(NIL);
  FQuery.Connection := FConnection;

destructor TBaseQuery.Destroy;

The above code will NOT compile, it is just to illustrate a good(in my opinion) way to prevent suicide coding.

I would recommend a unit for each TBaseQuery derived class and for each SQL string define a constant, i.e. for TQueryEmployee you can have a unit called „uQueryEmployee.pas¯, and each SQL constant that will be defined in the unit start the name with „EMPLOYEE_¯.

Anyone wanna share his technique of avoiding suicide coding?

No comments:

Post a Comment

Blogroll(General programming and Delphi feeds)