Sunday, January 9, 2011

Refactoring - Manage Your Code and Layers

Refactory - Design Improvement of the Existing Code.

When I say Code Management most people tell me "I know exactly what I wrote... I know exactly where everything lays".

Well.. This sentence usually takes me back to when I was 6 or 7 years old.  My mother used to tell me to clean and organize my room  and that's exactly what I told her...:

"But mom...  I love it when my soldiers are under the carpet, my school books are in the closet, my clothes are on my library and my shoes are where my action figures were.

I'm pretty sure most of you are nodding and agreeing with me.
But this is wrong.

Back to development then...
There are several file types that should be in a unique folder (for each) in every project:

   1. JavaScript
   2. HTML
   3. CSS

JavaScript should not be inside "script" tag.
CSS should not be inside "style" tag nor inside the element it's affecting.
And HTML... Well... HTML should be on his own.

Now I want to talk a little about Classes.
In every language we write we should have different classes to achieve different functionality.

One of the most important Classes out there is the DB Layer - Data Base Layer.
Lets go over the basic terms:
  1. DBLayer - a class containing calls for the Views or Stored Procedures in the database.
  2. Stored Procedure - SP - a function inside the database which designed to insert / update / delete / select from a table or from a number of tables.  Stored Procedures can also return DataSet / DataTable / String / Int / DateTime according to specific parameters.   
  3. Views - A View is a static query designed to be activated without any major changes (a view won't usually accept parameters.  Store Procedure will).
So, Back to the Management of the Code.

The basic level is the GUI - Graphic User Interface - Meaning, what the user see and work with.
The GUI is basically the HTML and CSS (in website development).

After that, we have the Client side code - Scripts - JavaScript.
The JavaScript should usually be the one (or plain HTML elements "OnClick" attributes) to activate the code behind functions.

After a Client Side element calls a Server Side function - the function should preform it functionality.  Meaning, run through the code.
In most of the cases, a large part of the functions involve calling the datalayer.

The Data Layer Class (or as I call it "DBLayer") contains all the functions that should call the database.  Since we want a neat and tidy code, we wont write Select / Insert / Update / Delete / Drop inside the code.
This is not meant only for a clean code, this code moderation is basically for security reasons.

Lets say you have several projects that use the same data base.  On one of these projects you have a friend working with.
Giving him full access permissions to Drop and Truncate Tables is not the best idea.

Eventually, the whole point of this post was to give a short explanation of how to correctly moderate your code.  How it should be designed.

If your code isn't designed that way - That's the exact point of Refactoring..
Changing the design of the existing code without altering it's functionality.

Elad Shalom,
CTO at