in

CodeSmith Community

Your Code. Your Way. Faster!

Coding feedback

Last post 03-06-2007 10:15 AM by mike123. 1 replies.
Page 1 of 1 (2 items)
Sort Posts: Previous Next
  • 03-06-2007 8:16 AM

    • oezoezoe
    • Not Ranked
    • Joined on 03-01-2007
    • Posts 2
    • Points 70

    Coding feedback

    Hi,

    I was looking at the generated code and saw a lot of these kind of coding constructions in this case the weblibrary/data/ProviderDataSource.cst:

    public ProviderDataSourceInsertMethod InsertMethod
      {
       get
       {
        ProviderDataSourceInsertMethod insertMethod = ProviderDataSourceInsertMethod.<%= insertMethods[0] %>;
        Object method = ViewState["InsertMethod"];
        if ( method != null )
        {
         insertMethod = (ProviderDataSourceInsertMethod) method;
        }
        return insertMethod;
       }

    wouldn't the following code be better?

    public ProviderDataSourceInsertMethod InsertMethod
      {
       get
       {
        ProviderDataSourceInsertMethod insertMethod = null;
        Object method = ViewState["InsertMethod"];
        if ( method != null )
        {
         insertMethod = (ProviderDataSourceInsertMethod) method;
        }
        else
        {
         insertMethod = ProviderDataSourceInsertMethod.<%= insertMethods[0] %>
        }
        return insertMethod;
       }

     

    Because now it doesn't have to execute unnessesary code

    Just my 2 cents

    • Post Points: 35
  • 03-06-2007 10:15 AM In reply to

    • mike123
    • Top 10 Contributor
    • Joined on 02-25-2005
    • Toronto, Ontario
    • Posts 723
    • Points 16,775

    Re: Coding feedback

    Avoiding use of the null keyword, actually improves the stability of the code (reducing number of NullReferenceExceptions to deal with)

            public ProviderDataSourceInsertMethod InsertMethod
            {
                get
                {
                    // -- i wouldn't say the line bellow executes the unnessesary code, but rather
                    // -- initializes a local variable to an enum value - named constant so to speak
                    ProviderDataSourceInsertMethod insertMethod = ProviderDataSourceInsertMethod.Insert;
                    Object method = ViewState["InsertMethod"];
                    if ( method != null )
                    {
                        insertMethod = (ProviderDataSourceInsertMethod) method;
                    }
                    return insertMethod;
                }
                set { ViewState["InsertMethod"] = value; }
            }

        /// <summary>
        /// Enumeration of method names available for the ProviderDataSource.InsertMethod property.
        /// </summary>
        public enum ProviderDataSourceInsertMethod
        {
            /// <summary>
            /// Represents the Insert method.
            /// </summary>
            Insert,
            /// <summary>
            /// Represents the Save method.
            /// </summary>
            Save,
            /// <summary>
            /// Represents the DeepSave method.
            /// </summary>
            DeepSave
        }

    Mike Shatny
    -------------------------------------
    Member of the .netTiers team
    http://www.nettiers.com
    -------------------------------------
    • Post Points: 5
Page 1 of 1 (2 items)
Copyright © 2008 CodeSmith Tools, LLC
Powered by Community Server (Commercial Edition), by Telligent Systems