Tuesday, November 18, 2014

Good link for java script, jQuery code optimization

http://developer.nokia.com/community/wiki/JavaScript_Performance_Best_Practices

Thursday, September 11, 2014

How to use JQuery Selectors?

The selectors are very useful and would be required at every step while using jQuery. They get the exact element that you want from your HTML document.
Following table lists down few basic selectors and explains them with examples.
SelectorDescription
NameSelects all elements which match with the given element Name.
#IDSelects a single element which matches with the given ID
.ClassSelects all elements which match with the given Class.
Universal (*)Selects all elements available in a DOM.
Multiple Elements E, F, GSelects the combined results of all the specified selectors E, F or G.
Similar to above syntax and examples, following examples would give you understanding on using different type of other useful selectors:
  • $('*'): This selector selects all elements in the document.
  • $("p > *"): This selector selects all elements that are children of a paragraph element.
  • $("#specialID"): This selector function gets the element with id="specialID".
  • $(".specialClass"): This selector gets all the elements that have the class of specialClass.
  • $("li:not(.myclass)"): Selects all elements matched by
  • that do not have class="myclass".
  • $("a#specialID.specialClass"): This selector matches links with an id of specialID and a class of specialClass.
  • $("p a.specialClass"): This selector matches links with a class of specialClass declared within elements.
  • $("ul li:first"): This selector gets only the first
  • element of the
      .
  • $("#container p"): Selects all elements matched by that are descendants of an element that has an id of container.
  • $("li > ul"): Selects all elements matched by
      that are children of an element matched by
  • $("strong + em"): Selects all elements matched by that immediately follow a sibling element matched by .
  • $("p ~ ul"): Selects all elements matched by
      that follow a sibling element matched by .
  • $("code, em, strong"): Selects all elements matched by or or .
  • $("p strong, .myclass"): Selects all elements matched by that are descendants of an element matched by as well as all elements that have a class of myclass.
  • $(":empty"): Selects all elements that have no children.
  • $("p:empty"): Selects all elements matched by that have no children.
  • $("div[p]"): Selects all elements matched by
    that contain an element matched by .
  • $("p[.myclass]"): Selects all elements matched by that contain an element with a class of myclass.
  • $("a[@rel]"): Selects all elements matched by that have a rel attribute.
  • $("input[@name=myname]"): Selects all elements matched by that have a name value exactly equal to myname.
  • $("input[@name^=myname]"): Selects all elements matched by that have a name value beginning with myname.
  • $("a[@rel$=self]"): Selects all elements matched by that have rel attribute value ending with self
  • $("a[@href*=domain.com]"): Selects all elements matched by that have an href value containing domain.com.
  • $("li:even"): Selects all elements matched by
  • that have an even index value.
  • $("tr:odd"): Selects all elements matched by that have an odd index value.
  • $("li:first"): Selects the first
  • element.
  • $("li:last"): Selects the last
  • element.
  • $("li:visible"): Selects all elements matched by
  • that are visible.
  • $("li:hidden"): Selects all elements matched by
  • that are hidden.
  • $(":radio"): Selects all radio buttons in the form.
  • $(":checked"): Selects all checked boxex in the form.
  • $(":input"): Selects only form elements (input, select, textarea, button).
  • $(":text"): Selects only text elements (input[type=text]).
  • $("li:eq(2)"): Selects the third
  • element
  • $("li:eq(4)"): Selects the fifth
  • element
  • $("li:lt(2)"): Selects all elements matched by
  • element before the third one; in other words, the first two
  • elements.
  • $("p:lt(3)"): selects all elements matched by elements before the fourth one; in other words the first three elements.
  • $("li:gt(1)"): Selects all elements matched by
  • after the second one.
  • $("p:gt(2)"): Selects all elements matched by after the third one.
  • $("div/p"): Selects all elements matched by that are children of an element matched by
    .
  • $("div//code"): Selects all elements matched by that are descendants of an element matched by
    .
  • $("//p//a"): Selects all elements matched by that are descendants of an element matched by
  • $("li:first-child"): Selects all elements matched by
  • that are the first child of their parent.
  • $("li:last-child"): Selects all elements matched by
  • that are the last child of their parent.
  • $(":parent"): Selects all elements that are the parent of another element, including text.
  • $("li:contains(second)"): Selects all elements matched by
  • that contain the text second.
You can use all the above selectors with any HTML/XML element in generic way. For example if selector $("li:first") works for
  • element then $("p:first") would also work for element.
  • Tuesday, January 14, 2014

    Coding standard



    1. Naming Conventions and Style
    douse PascalCasing for class names and method names.
    public class ClientActivity
    {
        public void ClearStatistics()
        {
            //...
        }
        public void CalculateStatistics()
        {
            //...
        }
    }
    Why: consistent with the Microsoft's .NET Framework and easy to read.
    douse camelCasing for method arguments and local variables.
    public class UserLog
    {
        public void Add(LogEvent logEvent)
        {
            int itemCount = logEvent.Items.Count;
            // ...
        }
    }
    Why: consistent with the Microsoft's .NET Framework and easy to read.
    do notuse Hungarian notation or any other type identification in identifiers
    // Correct
    int counter;
    string name;

    // Avoid
    int iCounter;
    string strName;
    Why: consistent with the Microsoft's .NET Framework and Visual Studio IDE makes determining types very easy (via tooltips). In general you want to avoid type indicators in any identifier.
    do notuse Screaming Caps for constants or readonly variables
    // Correct
    public static const string ShippingType = "DropShip";

    // Avoid
    public static const string SHIPPINGTYPE = "DropShip";
    Why: consistent with the Microsoft's .NET Framework. Caps grap too much attention.
    avoidusing Abbreviations. Exceptions: abbreviations commonly used as names,
                     such as Id, Xml, Ftp, Uri
    // Correct
    UserGroup userGroup;
    Assignment employeeAssignment;

    // Avoid
    UserGroup usrGrp;
    Assignment empAssignment;

    // Exceptions
    CustomerId customerId;
    XmlDocument xmlDocument;
    FtpHelper ftpHelper;
    UriPart uriPart;
    Why: consistent with the Microsoft's .NET Framework and prevents inconsistent abbreviations.
    douse PascalCasing for abbreviations 3 characters or more (2 chars are both uppercase)
    HtmlHelper htmlHelper;
    FtpTransfer ftpTranfer;
    UIControl uiControl;
    Why: consistent with the Microsoft's .NET Framework. Caps would grap visually too much attention.
    do notuse Underscores in identifiers. Exception: you can prefix private static variables
                        with an underscore.
    // Correct
    public DateTime clientAppointment;
    public TimeSpan timeLeft;

    // Avoid
    public DateTime client_Appointment;
    public TimeSpan time_Left;

    // Exception
    private DateTime _registrationDate;
    Why: consistent with the Microsoft's .NET Framework and makes code more natural to read (without 'slur'). Also avoids underline stress (inability to see underline).
    douse predefined type names instead of system type names like Int16, Single, UInt64, etc
             
    // Correct
    string firstName;
    int lastIndex;
    bool isSaved;

    // Avoid
    String firstName;
    Int32 lastIndex;
    Boolean isSaved;
    Why: consistent with the Microsoft's .NET Framework and makes code more natural to read.
    douse implicit type var for local variable declarations. Exception: primitive types (int, string,
              double, etc) use predefined names.
    var stream = File.Create(path);
    var customers = new Dictionary<int?, Customer>();

    // Exceptions
    int index = 100;
    string timeSheet;
    bool isCompleted;
    Why: removes clutter, particularly with complex generic types. Type is easily detected with Visual Studio tooltips.
    douse noun or noun phrases to name a class.
    public class Employee
    {
    }
    public class BusinessLocation
    {
    }
    public class DocumentCollection
    {
    }
    Why: consistent with the Microsoft's .NET Framework and easy to remember.
    doprefix interfaces with the letter I.  Interface names are noun (phrases) or adjectives.
    public interface IShape
    {
    }
    public interface IShapeCollection
    {
    }
    public interface IGroupable
    {
    }
    Why: consistent with the Microsoft's .NET Framework.
    doname source files according to their main classes. Exception: file names with partial classes
              reflect their source or purpose, e.g. designer, generated, etc.
    // Located in Task.cs
    public partial class Task
    {
        //...
    }
    // Located in Task.generated.cs
    public partial class Task
    {
        //...
    }
    Why: consistent with the Microsoft practices. Files are alphabetically sorted and partial classes remain adjacent.
    doorganize namespaces with a clearly defined structure
    // Examples
    namespace Company.Product.Module.SubModule
    namespace Product.Module.Component
    namespace Product.Layer.Module.Group
    Why: consistent with the Microsoft's .NET Framework. Maintains good organization of your code base.
    dovertically align curly brackets.
    // Correct
    class Program
    {
        static void Main(string[] args)
        {
        }
    }
    Why: Microsoft has a different standard, but developers have overwhelmingly preferred vertically aligned brackets.
    dodeclare all member variables at the top of a class, with static variables at the very top.
    // Correct
    public class Account
    {
        public static string BankName;
        public static decimal Reserves;

        public string Number {get; set;}
        public DateTime DateOpened {get; set;}
        public DateTime DateClosed {get; set;}
        public decimal Balance {get; set;}

        // Constructor
        public Account()
        {
            // ...
        }
    }
    Why: generally accepted practice that prevents the need to hunt for variable declarations.
    douse singular names for enums. Exception: bit field enums.
    // Correct
    public enum Color
    {
        Red,
        Green,
        Blue,
        Yellow,
        Magenta,
        Cyan
    }

    // Exception
    [Flags]
    public enum Dockings
    {
        None = 0,
        Top = 1,
        Right = 2,
        Bottom = 4,
        Left = 8
    }
    Why: consistent with the Microsoft's .NET Framework and makes the code more natural to read. Plural flags because enum can hold multiple values (using bitwise 'OR').
    do notexplicitly specify a type of an enum or values of enums (except bit fields)
    // Don't
    public enum Direction : long
    {
        North = 1,
        East = 2,
        South = 3,
        West = 4
    }

    // Correct
    public enum Direction
    {
        North,
        East,
        South,
        West
    }
    Why: can create confusion when relying on actual types and values.
    do notsuffix enum names with Enum
    // Don't
    public enum CoinEnum
    {
        Penny,
        Nickel,
        Dime,
        Quarter,
        Dollar
    }

    // Correct
    public enum Coin
    {
        Penny,
        Nickel,
        Dime,
        Quarter,
        Dollar
    }
    Why: consistent with the Microsoft's .NET Framework and consistent with prior rule of no type indicators in identifiers.