Wednesday, March 7, 2012

Best Practices - JavaScript Programming Part 1


Best Practices -
JavaScript Programming 
Part 1


JavaScript is one of the most popular programming languages today. It is widely used by web developers and client side programmers all over the world. But here’s the thing, while technology companies are strict about their server side code to be structured and readable, the client side code does not get the same attention. Often, the web programming is being delegated to 3rd parties or to server developers who are inexperienced with client development. This can lead to a poorly written client which is OK if it’s a throwaway, but what if the code is there to stay and change over time?
This post is for developers who are familiar with JavaScript but want to get a little bit better at it.  All of the tips given hereby are from my own personal experience, they are not scientific facts and should not be accepted as such.



General Styling

A good place to start is the general appearance of the code. If you’re going to take anything from this post, let it be this: MAKE YOUR CODE READABLE!!! There’s no greater hell than trying to understand a sloppily written JavaScript code, especially if it was written by someone else.
Readability can be fairly achieved by following a few basic guidelines:


Use meaningful variable names, even if they get long. For example:

            var divBuilder is better than var db.


Limit the length of your methods, methods should be no longer than 8 – 10 lines.


Newspaper Paradigm:

Let’s observe the following code:

$(document).ready(function ()
{
    ContactPage.RegisterForEvents();
    ContactPage.FillContactInfo();
    ContactPage.FillAdditionalInfo();
});

You’ve never seen this code before but I bet you can guess what it does. This is due to the newspaper like layout of the method, we’re presented with headlines instead of overwhelming details that are kept out of sight at this level. This way we can directly look up the piece of code that interests us without going through tons of irrelevant data.



Seperation

In the earlier days it was customary to embed scripts inside the Html content. As the years went by, with the improvement of browsers JavaScript engines the use of JavaScript increased dramatically and the scripts volume grew accordingly. Nowadays, separation of the JS code to external JS files will produce a much cleaner and readable Html.



NameSpacing

In JavaScript much like in C, we can add variables to the global scope. For instance we can write the line x = 3 anywhere in the code, then after the evaluation of this line by the interpreter we can successfully reference x from anywhere. When working side by side with external JS code, this can cause naming collisions.
A good solution for this issue is to use namespacing. 


Let’s look at the following example:

var MathExtensions = {
    PI : 3.14159265,
    Add: function (a, b) {
        return a + b;
    },
    ...
}

In this example we use the JavaScript object literal syntax to create a namespace. The variable PI lives inside the MathExtensions context. To reference it, we’ll have to write :
MathExtensions.PI.

 Same goes for the method Add, we’ll have to use:

MathExtensions.Add(3,5).

For those who come from the statically typed languages world, this may resemble a static class. 
*Note: One may also notice that the value of MathExtensions variable is in fact a valid JSON (Java Script Object Notation).  



Constants

It is almost always a good idea to use constant variables to avoid magic numbers and strings inside the code, especially strings that represent Html classes or IDs.
It was more than once that I encountered lines of code that look like this:

var element = document.getElementById("ActionButton");

or JQuery equivalent:

var element = $("#ActionButton");

If one day we decide to change the ID (or class) of an element (which may happen in web design), we’re in for a risky adventure of tracking down all the references to this element’s ID in the code and change them.

A better way would be to store all these magic strings in variables. A namespace for classes or IDs is advisable. For instance, for the example above we’ll use something like this:


var Classes = {
    ActionButtonID: "ActionButton",
    InputTextBoxClass: "InputTextBox"
    ...
};

The reference will change to:

var element = $("#" + Classes.ActionButtonID);




DRY

The last example emphasizes the practice of a very important principle in programming called the DRY principle. DRY stands for ‘Don’t Repeat Yourself’, but what it really means is ‘One Place To Change’. In the example, when we want to change the class name for an element we’ll change only the value in the Classes namespace.
Do not hold multiple pieces of code that do the same work. If you come across a situation in which you need the same JS code in two different web pages, do NOT duplicate it. Instead, you can hold a shared scripts JS file and reference it from both pages.



More to come in part 2








Monday, March 5, 2012

A Practical Guide - From manual to automated QA

Below is a short talk about PicScout's QA practices, which was delivered by Uri Lavi during Agile Practitioners 2012 conference.



Feel free to share with us any additional insights about your own experience in QA automation.

Monday, February 13, 2012

A Week of IL Tech Talks at PicScout

Sharing real life experience for the benefit of Israeli hi-tech community is one of the greatest things we have achieved during the last years.

Ori Lahav’s IL Tech Talks initiative definitely helped in promoting this in our community.
That’s why we happily hosted a full week of those tech talks at PicScout.

We learned a lot: From understanding client side performance, best development practices (here and here), scaling (here and here) to lean practices and management skills.

Amazingly, it appears that “good” things are somehow “reproduced” among the companies. The talks revealed that successful companies are build from the same “forces” in terms of practices and vision. These are exactly the same things we have done and continue to pursue at PicScout.

Unit testing, TDD and Continuous Delivery seems to free business and technology to move safely and together towards the goal. It was quite obvious from Wix’s and Outbrain’s experiences.

For us, being one of the scalable companies in Israel (in terms of billions image recognitions a month), it was very interesting to see others views on scalability.
Specifically (and personally), I would mention here the lecture by Eran Sandler.
Building a truly scalable architecture is hard. Sometimes, it isn’t even necessary till the right conditions are met. Eran’s lecture, introduced small and measured steps to gain scalability without re-writing the code.

Lean techniques (presented skillfully by Elad Sofer) made us to re-enforce our approach of continuous improvement. Building effective communication, seeking and eliminating waste and design for simplicity (yet “useful” products) are skills we all need to embrace and to grow.

Here are some (but not all) of our feedbacks:

Ilya: Once again I have got a validation on how Continuous Delivery and TDD help to create better software - talking about “Building a web infrastructure for 10M users”
Sharon: New ideas on management that will be tested soon ;) - talking about “Team Up”
Merav: Sometimes Agile principles perceived as the new management panacea; Understanding that those principles are here to serve you in a long term is what important - talking about “Lean Software Development”
Arik: I definitely learnt a few new best practices to make our web sites more effective - talking about “Web Performance 101”

We would like to thank all the presenters, who contributed from their own time to present and to share with us their real life experiences!








Monday, January 23, 2012

Database Talk - Unique Identifiers vs. Numeric identities

Wiki
Just to make sure we're all on the same page –
Unique Identifier (which from now on will be referred to as ‘GUID', a.k.a. 'UUID'), is an algorithm-based 32-byte hex string (128 bit integer) which is supposed to generate completely unique values.

I’d like to compare the chances of two identical generated GUIDs to the chance earth will be destroyed by a meteor in the next five minutes.
Now, if that happens – you won’t be here to prove me wrong! J

Here’s one: 5B5D6F63-1FF0-4574-9B77-BD77D716FD66.
It is probably unique and there’s no other like it in the world.


Why should I?
With today’s increasing demand for fully-distributed systems, GUID usage is increasing accordingly.
It allows managing two or more disconnected databases which later on need to merge, knowing that each row on desired shared tables is unique.

Here’s a simple example –

Consider a very large company with many branches across the world.

Each branch has local users, and there’s a sync process which merges the entire users list.
The ‘traditional’ way is to maintain a range of numbers for each branch, for example:
-       use a prefix on the username to maintain uniqueness
-       Assign a numeric range for each branch (easier today with SQL 2012's sequences support)
-       Make a combined key of two columns, for example - UserId+BranchId columns

This is a classic sample for using GUID instead – make the UserId column GUID and problem is solved.


Sounds like fun! Any reason why I shouldn’t use it?
Well, yes.
Before you get all excited and change your “int’s” and “bigint’s” to GUIDs, consider the following:
-       GUID column takes 16 bytes, which is double than Bigint (long).
o    This means that index on the column will also take more space
-       Index Fragmentation is an issue and it is most likely to happen.
o    (See more about it on the Tips section below)
-       Not all databases support GUIDs.
o    So, if you migrate to mysql, you'll have to change the datatype to CHAR(32), so it takes even more space as a result


Some Best-Practices Tips:

[Tip #1]

The first, and probably the most important tip, is to avoid using CLUSTERED INDEX on GUID's.

This is because the GUID values are random – a physical sort will cause the table to be re-sorted very frequently and will also result with an excessive I/O consumption.
So do one of the following:
Either create another identity on the table, which also serves as the PRIMARY KEY (also a generic best practice of avoiding 'heap' tables)
OR -
When possible - use the NEWSEQUENTIALID function.
This will create unique GUIDs on a table but will also make sure the new generated GUID is greater than the last. (It is still globally unique as long as the machine has its own MAC address).
Let’s review a quick sample of the differences between the NEWID() function and the NEWSEQUENTIALID default value.
We'll create two tables; each uses one of the techniques:

      -- Creating the tables:    
      IF OBJECT_ID('UsersNewId') IS NULL
      BEGIN
            CREATE TABLE UsersNewId
            (
                  UserId UNIQUEIDENTIFIER PRIMARY KEY NONCLUSTERED DEFAULT NEWID(),
                  UserName VarChar(255)
            )
      END
     
      IF OBJECT_ID('UsersNewSeqId') IS NULL
      BEGIN
            CREATE TABLE UsersNewSeqId
            (
                  UserId UNIQUEIDENTIFIER PRIMARY KEY NONCLUSTERED DEFAULT NEWSEQUENTIALID(),
                  UserName VarChar(255)
            )
      END;
     
      -- Now, let's fill the tables with some data
      -- Note: I'm using SQL2008+ syntax here; On SQL2005 you need to separate the insert statements
      INSERT INTO UsersNewId(UserName) VALUES ('User1'),('User2'),('User3'),('User4'),('User5')
      INSERT INTO UsersNewSeqId(UserName) VALUES ('User1'),('User2'),('User3'),('User4'),('User5')
     
      -- Now, let's see the tables
      SELECT * FROM UsersNewId
      SELECT * FROM UsersNewSeqId

The results:

Note the GUID on the 2nd table looks the same on first sight, but actually it is not.
These are increasing values that still maintain uniqueness.
This is obviously much better for index fragmentation of any time!

[Tip #2]
Related to the above tip – as a DBA, try to keep the GUID generation on your side!  
Otherwise, if the GUID’s are generated by a different layer in your application (DAL or so), the GUIDs cannot be sequential.  
Besides, try to think as if the GUID’s are actually a new version of your INT identities; you wouldn’t let anyone control the auto-increase now would you?
[Tip #3]
In some cases you may want to consider surrogate keys, which will be used as the foreign key between the tables.
To use the sample at the beginning of this article – each user will have its own GUID, but with an additional “UserInternalId” column (int) which will be the table's primary key.

[Tip #4]
Don’t use GUID if you don’t have to.
When a table needs an ID which does not have to be unique across the enterprise, keep using the regular numeric id.
It takes less space and easier to manage.


Final Words:
GUIDs are great and simple solution to maintain uniqueness across the enterprise.
However, don’t overuse it when not required as it might hurt your databases performances.
This is a very wide subject – hope this in-a-nutshell post helps or at least gives you a good head start.

Thursday, December 29, 2011

ILTechTalks Week

During the second week of January, PicScout will host a week of IL Tech Talks.
We will have 2 talks a day starting from 16:30.

Please follow IL Tech Talks meet-up for more details.

Here's the agenda:


S - 8/1
M - 9/1
T - 10/1
W - 11/1
Th - 12/1
Team Up
(18:00)

The number of places is limited, so hurry up if you are interested.

Sunday, December 11, 2011

Disposing - doing it the right way

.NET’s Garbage-Collection (GC) is a well-documented and discussed feature of the Common-Language-Runtime (CLR). Plenty of posts and tutorials can be found online.
This topic is almost always followed by of another popular aspect - ”Finalize\Dispose Pattern”.

However, there are dark corners that usually aren’t mentioned widely enough, although they play important role in performance. While disposing seems as a simple plug-and-play, overusing it can affect the performance of CPU-bounded applications.

This post will try to shed some light on disposing dangers and show some best practices.


GC and disposing in a nutshell
.NET CLR holds managed heap for each application-domain where reference-types (objects) are stored, while their pointing variables are located on the stack. Those variables are often referred to as “roots”. Every once in a while (according to specific rules), CLR executes a thread which scans the heap, identifies unreachable (“dead-root”) objects and clears them from memory.
Microsoft published a GC optimization technique, the “Finalize\Dispose Pattern”, which is recommended to read before moving on with this post.
Objects can clear both managed and unmanaged members before their root vanishes, so GC won’t need to dedicate precious resources for finalizing later.
This technique is even enforced by Code-Analysis tools (such as FxCop), but bad news is that it causes programmers to often miss important key-points.

Here are the basic rules to watch out:



DO NOT implement Finalizer automatically
A major mistake is to implement needless Finalizer, and it happens quite a lot.
Class must implement such if and only if it creates unmanaged resources which do not reside on heap.
Implementing needless Finalize method means it will take extra time to allocate the object as a reference must be inserted into the finalization queue.
Moreover, each and every object allocated for this type will require two GC collections and additional finalization step, since there is still a finalization-root reference!

Those aren’t things to underestimate – imagine scenarios where hundreds of objects are allocated & deallocated per second!

Other two common mistakes, are that 1) Dispose method is called by GC to clear memory, and 2) Calling Dispose manually prevents objects even from going into GC.
That’s totally wrong!
Dispose can be called only by programmer’s code – either directly or via “using” block.
GC on the other hand clears the memory by physically detecting and removing dead-objects and later executing Finalizers if needed.
So, objects must go into one GC at least.
Implementing Dispose is only recommended when type has Finalizer or at least one of its members is IDisposable. Such preliminary disposing (and finalization suspension) will prevent the object and its members from consuming second GC plus finalization.
Do not make your class IDisposable unnecessarily since it will enforce classes which hold it to become also IDisposable, and so on...

Additional best practice which is usually forgotten is to ensure disposing doesn’t throw exceptions. We use nice extension method for that sake:
public static class IDisposableExtensions
{ 
  public static void SafeDispose(this IDisposable disposedObj) 
  {  
    try  
    {   
       if (disposedObj != null)
       { 
           disposedObj.Dispose();
       }  
    }  
    catch {} // log some stats here… 
   }
}

Last common mistake (but not least), is that calling Dispose method marks objects as null, i.e. removes their root reference.
Such thing never happens!
Removal of root happens only when the variable pointing to the object expires (end of method for ex.), and only then GC is aware of it and marks it for cleaning. So, if one wishes to optimize GC for disposed objects, we recommend using the following nice trick – setting reference variables to “null” right after their disposal. That way, in case GC collection is about to run, such objects will be collected right away and won’t wait for their variable to be eliminated by CLR.
Integrating the two techniques described above, our virtual disposing methods usually look like this (a simple snippet can be used):
private bool m_IsDisposed = false;
protected virtual void Dispose(bool disposing)
{ 
   if (!m_IsDisposed) 
   {  
      if (disposing)  
      {   // clean managed resources...      
         m_ClassMember.SafeDispose();      
         m_ClassMember = null;  
      }   
      // clean unmanaged resources here...  
      m_IsDisposed = true; 
   }
}