A series of hopefully simple questions regarding best practices and coding rules about this code

I posted comments with my inline questions. Code supports rest calls.

    // (1) Is appending Base to the name of base classes useful?
    public abstract class RestCallBase : IRestCall
    {
        // (2) Is there a good way to decide on the ordering/grouping of members?
        // I have seen code that uses #region for this, but I feel like it not
        // pervasive.
        public string Url { get; set; }
        public string Method { get; set; }
        public string PostData { get; set; }
        public string ResponseText { get; set; }

        // (3) How do you feel about using the same name for type and identifier
        // in cases like this?  I go back and forth because on one hand it feels
        // cleaner than using underscore (_MyPrivateProperty) or
        // camel cased (myPrivateProperty).
        private HttpWebRequest HttpWebRequest { get; set; }

        // (4) Is it clear that the target of the lone verb comprising the method
        // name is the noun which is the name of the class?  To me, it is 
        // redundant to say 'PrepareForRestCall'.
        protected abstract void Prepare();

        public IRestCall Go()
        {
            this.Prepare();

            HttpWebRequest = (HttpWebRequest)WebRequest.Create(Url);

            // (5) Here a region is used in place of a comment, but I have not
            // seen any other code that uses regions this way.  My thinking is
            // that it brings the code one step closer to becoming a private
            // method but can stay like this until it actually needs to be called
            // from multiple points in the logic.
            #region Add post data to request if present.
            if (!string.IsNullOrEmpty(PostData))
            {
                // (6) I changed this from 'sw' to 'writer' after code review.
                // Would you have as well?
                using(StreamWriter writer = new StreamWriter(HttpWebRequest.GetRequestStream()))
                    writer.Write(PostData); // (7) Would you use curly braces for a single statement?  I opt to save two lines so I can see more code on the screen.
            }
            #endregion

            using (HttpWebResponse response = HttpWebRequest.GetResponse() as HttpWebResponse)
            {
                StreamReader reader = new StreamReader(response.GetResponseStream());
                ResponseText = reader.ReadToEnd();
            }

            return this;
        }
    }
+3
source share
7 answers

Despite the fact that no one liked all the rules related to StyleCop, our team decided to accept it, so at least simple things would be consistent across our entire code base.

  • Yes.
  • Stylecop
  • I do this all the time (like the .NET Framework)
  • I agree.
  • I personally will not use such regions.
  • Definitely
  • Yes. StyleCop will provide this.
+2
source

(1) Does the Basename of the base classes add?

. . RestCallBase instance = new SomeConcreteRestCall(); instance - RestCall. .

(2) / ? , #region , , .

, . #region ( , #region), .

(3) ?

. . .

, , , , (_MyPrivateProperty) (myPrivateProperty).

( , ). this; .

(4) , , , - , ? PrepareForRestCall.

, , , Prepare, - , PrepareForRestCall , Prepare, , , .

(5) , , .

. , , .

(6) sw writer . ?

.

(7) ? , .

, . , .

+2
  • Abstract. Base, ;

  • / ;

  • , /. , , _myPrivateProperty;

  • ;

  • , . . , , . ;

  • , foreach. if , , .

+1

:

  • , . , API (. ). RestCallBase instance = ... RestCall.

  • . , .

  • , . BCL.

  • . , . , , "" ? , API, , "" .

  • . , , . . , .

  • streamWriter, , .

  • , , . StyleCop , BTW - .

+1

# 5 - - AddPostDataToRequestIfPresent.

+1

6:

(6) sw . ?

. sw - , ( ) ( ).

, ( ), (, postContent). writer streamWriter - - , , , .

+1

(5) , , .

, . , .

:

  • If the comment explains what you are doing, just read the comment and keep the area closed. Thus, helping to understand the program with minimal effort.

  • You clearly see which area this comment refers to. Sometimes this is not so clear if you use a regular comment.

It seems to me that its method, in addition to its code, is in the most logical place where it was used.

Possible cons:

  • Entrance and exit are less clear.
+1
source

Source: https://habr.com/ru/post/1770143/


All Articles