Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. The Lounge
  3. This vs. that

This vs. that

Scheduled Pinned Locked Moved The Lounge
csharpphpvisual-studiocomagentic-ai
33 Posts 21 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Sander RosselS Sander Rossel

    I have a question about your preferred programming style. Whenever I write code in C# I make extensive use of the 'this' keyword and I use class names when referencing statics. So:

    this.SomeProperty = someValue;
    this.SomeMethod();
    ThisClass.SomeStaticMethod();

    Instead of:

    SomeProperty = someValue;
    SomeMethod();
    SomeStaticMethod();

    My reasoning behind this is that I can tell something is an instance method or a static method. And to be completely honest it's also something I started doing in VB because VB has Modules and Modules don't make you specify the Module's name. So Me.SomeMethod() can be an instance method, a Shared/static method on the current class or a method in some Module! Now there's a new guy at work and he really hates this style of programming because he thinks it's redundant. I'm not asking for right or wrong (unless I'm the one who's right ;p), but I want to know personal preferences.

    My blog[^]

    public class SanderRossel : Lazy<Person>
    {
    public void DoWork()
    {
    throw new NotSupportedException();
    }
    }

    V Offline
    V Offline
    V 0
    wrote on last edited by
    #15

    Unless I have a construct like:

    public class X{
    private int number;

    public X(int number){
    this.number = number;
    }
    }

    I rarely uses this.

    V.
    (MQOTD rules and previous solutions)

    1 Reply Last reply
    0
    • Sander RosselS Sander Rossel

      I have a question about your preferred programming style. Whenever I write code in C# I make extensive use of the 'this' keyword and I use class names when referencing statics. So:

      this.SomeProperty = someValue;
      this.SomeMethod();
      ThisClass.SomeStaticMethod();

      Instead of:

      SomeProperty = someValue;
      SomeMethod();
      SomeStaticMethod();

      My reasoning behind this is that I can tell something is an instance method or a static method. And to be completely honest it's also something I started doing in VB because VB has Modules and Modules don't make you specify the Module's name. So Me.SomeMethod() can be an instance method, a Shared/static method on the current class or a method in some Module! Now there's a new guy at work and he really hates this style of programming because he thinks it's redundant. I'm not asking for right or wrong (unless I'm the one who's right ;p), but I want to know personal preferences.

      My blog[^]

      public class SanderRossel : Lazy<Person>
      {
      public void DoWork()
      {
      throw new NotSupportedException();
      }
      }

      R Offline
      R Offline
      R Erasmus
      wrote on last edited by
      #16

      I would say to use the property assignment 'this' in the following scenario:

      void foo(int someValue)
      {
      this.someValue = someValue;
      this.SomeMethod(AnotherClass.SomeMethod());
      }

      But not in the following scenario:

      void foo()
      {
      someValue = 5;
      SomeMethod(5);
      }

      With ThisClass.SomeStaticMethod() I'd always use is as you did unless of course you are using the static method of the class you are working in, then I'd just use SomeStaticMethod.

      "Program testing can be used to show the presence of bugs, but never to show their absence." << please vote!! >>

      1 Reply Last reply
      0
      • R R Giskard Reventlov

        this.Redundant

        K Offline
        K Offline
        Keith Barrow
        wrote on last edited by
        #17

        Errrm, this.

        Alberto Brandolini:

        The amount of energy necessary to refute bullshit is an order of magnitude bigger than to produce it.

        1 Reply Last reply
        0
        • R R Giskard Reventlov

          this.Redundant

          A Offline
          A Offline
          Agent__007
          wrote on last edited by
          #18

          Only assignment, call, increment, decrement, and new object expressions can be used as a statement. :)

          Your time will come, if you let it be right.

          1 Reply Last reply
          0
          • Sander RosselS Sander Rossel

            I have a question about your preferred programming style. Whenever I write code in C# I make extensive use of the 'this' keyword and I use class names when referencing statics. So:

            this.SomeProperty = someValue;
            this.SomeMethod();
            ThisClass.SomeStaticMethod();

            Instead of:

            SomeProperty = someValue;
            SomeMethod();
            SomeStaticMethod();

            My reasoning behind this is that I can tell something is an instance method or a static method. And to be completely honest it's also something I started doing in VB because VB has Modules and Modules don't make you specify the Module's name. So Me.SomeMethod() can be an instance method, a Shared/static method on the current class or a method in some Module! Now there's a new guy at work and he really hates this style of programming because he thinks it's redundant. I'm not asking for right or wrong (unless I'm the one who's right ;p), but I want to know personal preferences.

            My blog[^]

            public class SanderRossel : Lazy<Person>
            {
            public void DoWork()
            {
            throw new NotSupportedException();
            }
            }

            N Offline
            N Offline
            Nagy Vilmos
            wrote on last edited by
            #19

            this is good and I use it normally. The fact that it seems to annoy some pooples just encourages me to use it more. :-D

            S 1 Reply Last reply
            0
            • Sander RosselS Sander Rossel

              I have a question about your preferred programming style. Whenever I write code in C# I make extensive use of the 'this' keyword and I use class names when referencing statics. So:

              this.SomeProperty = someValue;
              this.SomeMethod();
              ThisClass.SomeStaticMethod();

              Instead of:

              SomeProperty = someValue;
              SomeMethod();
              SomeStaticMethod();

              My reasoning behind this is that I can tell something is an instance method or a static method. And to be completely honest it's also something I started doing in VB because VB has Modules and Modules don't make you specify the Module's name. So Me.SomeMethod() can be an instance method, a Shared/static method on the current class or a method in some Module! Now there's a new guy at work and he really hates this style of programming because he thinks it's redundant. I'm not asking for right or wrong (unless I'm the one who's right ;p), but I want to know personal preferences.

              My blog[^]

              public class SanderRossel : Lazy<Person>
              {
              public void DoWork()
              {
              throw new NotSupportedException();
              }
              }

              D Offline
              D Offline
              den2k88
              wrote on last edited by
              #20

              "this." triggers any auto-completion or Intellisense in the world. Yes I'm lazy - I wouldn't train machines to do men's work if I wasn't ;)

              F 1 Reply Last reply
              0
              • L Lost User

                My preference is to never use this unless there is a reason to do so (like conflicting names). My methods are small enough that it is rarely a problem to see what is local and what is a property and anyway, properties begin with a Capital and locals don't. For static methods I use the classname. option, as there is no other obvious differentiator. That said, I don't work with many classes with static methods, so not something I have formed a habit out of. When I see this.SomeProperty = this.SomeMethod(this.SomeOtherProperty) I cringe, and think "this? What else?)

                PooperPig - Coming Soon

                S Offline
                S Offline
                Slacker007
                wrote on last edited by
                #21

                _Maxxx_ wrote:

                (like conflicting names)

                :thumbsup::thumbsup::thumbsup:

                1 Reply Last reply
                0
                • N Nagy Vilmos

                  this is good and I use it normally. The fact that it seems to annoy some pooples just encourages me to use it more. :-D

                  S Offline
                  S Offline
                  Slacker007
                  wrote on last edited by
                  #22

                  such a trouble maker and non-conformist. :-D

                  1 Reply Last reply
                  0
                  • D den2k88

                    "this." triggers any auto-completion or Intellisense in the world. Yes I'm lazy - I wouldn't train machines to do men's work if I wasn't ;)

                    F Offline
                    F Offline
                    Freak30
                    wrote on last edited by
                    #23

                    If you just type one or two letters and then press Ctrl + Space, you would also get Intellisense and have to type even less. ;)

                    The good thing about pessimism is, that you are always either right or pleasently surprised.

                    Sander RosselS 1 Reply Last reply
                    0
                    • Sander RosselS Sander Rossel

                      I have a question about your preferred programming style. Whenever I write code in C# I make extensive use of the 'this' keyword and I use class names when referencing statics. So:

                      this.SomeProperty = someValue;
                      this.SomeMethod();
                      ThisClass.SomeStaticMethod();

                      Instead of:

                      SomeProperty = someValue;
                      SomeMethod();
                      SomeStaticMethod();

                      My reasoning behind this is that I can tell something is an instance method or a static method. And to be completely honest it's also something I started doing in VB because VB has Modules and Modules don't make you specify the Module's name. So Me.SomeMethod() can be an instance method, a Shared/static method on the current class or a method in some Module! Now there's a new guy at work and he really hates this style of programming because he thinks it's redundant. I'm not asking for right or wrong (unless I'm the one who's right ;p), but I want to know personal preferences.

                      My blog[^]

                      public class SanderRossel : Lazy<Person>
                      {
                      public void DoWork()
                      {
                      throw new NotSupportedException();
                      }
                      }

                      B Offline
                      B Offline
                      BillWoodruff
                      wrote on last edited by
                      #24

                      It has become a habit for me to use 'this,' to type in access modifiers even when not required, and to never create duplicate names, but, over the last two years, I have transitioned to using 'var freely which I know causes some people to break out in a poison sweat. I don't think these are particularly "bad" habits, but they are not any saving-grace of poor-quality design, or coding, either. At best, a syntactic sugar that may contribute to maintainability and future re-use ? But, I work alone, not in a team-setting. If (gods forbid) I was managing a commercial software dev team, oh yeah, I'd plump for standards.

                      «OOP to me means only messaging, local retention and protection and hiding of state-process, and extreme late-binding of all things. »  Alan Kay's clarification on what he meant by the term "Object" in "Object-Oriented Programming."

                      Z Sander RosselS 2 Replies Last reply
                      0
                      • B BillWoodruff

                        It has become a habit for me to use 'this,' to type in access modifiers even when not required, and to never create duplicate names, but, over the last two years, I have transitioned to using 'var freely which I know causes some people to break out in a poison sweat. I don't think these are particularly "bad" habits, but they are not any saving-grace of poor-quality design, or coding, either. At best, a syntactic sugar that may contribute to maintainability and future re-use ? But, I work alone, not in a team-setting. If (gods forbid) I was managing a commercial software dev team, oh yeah, I'd plump for standards.

                        «OOP to me means only messaging, local retention and protection and hiding of state-process, and extreme late-binding of all things. »  Alan Kay's clarification on what he meant by the term "Object" in "Object-Oriented Programming."

                        Z Offline
                        Z Offline
                        ZurdoDev
                        wrote on last edited by
                        #25

                        Interesting that a lot of people do use this. I did not expect that. :-\ I don't like it. It just clutters up with extra code that is unnecessary. :^)

                        There are only 10 types of people in the world, those who understand binary and those who don't.

                        1 Reply Last reply
                        0
                        • B BillWoodruff

                          It has become a habit for me to use 'this,' to type in access modifiers even when not required, and to never create duplicate names, but, over the last two years, I have transitioned to using 'var freely which I know causes some people to break out in a poison sweat. I don't think these are particularly "bad" habits, but they are not any saving-grace of poor-quality design, or coding, either. At best, a syntactic sugar that may contribute to maintainability and future re-use ? But, I work alone, not in a team-setting. If (gods forbid) I was managing a commercial software dev team, oh yeah, I'd plump for standards.

                          «OOP to me means only messaging, local retention and protection and hiding of state-process, and extreme late-binding of all things. »  Alan Kay's clarification on what he meant by the term "Object" in "Object-Oriented Programming."

                          Sander RosselS Offline
                          Sander RosselS Offline
                          Sander Rossel
                          wrote on last edited by
                          #26

                          BillWoodruff wrote:

                          At best, a syntactic sugar

                          Almost agreed. The var keyword can actually be quite dangerous as I have witnessed a few times myself. I work with Entity Framework a lot, which uses IQueryable to create queries that run on the database and return a result as IEnumerable that can be used in your application. Interface-wise there is little difference between IQueryable and IEnumerable, but the function of the two differs greatly! A few times I changed my IQueryable to IEnumerable and nothing broke, because the IQueryable was declared using var. The type of my variable just 'silently' changed to IEnumerable and all of a sudden my app wouldn't perform very well. Thing is, in order to create an IEnumerable the query was executed on the database. This was supposed to happen a few lines later, but because I put a ToList() function somewhere in between this was done to early. All my filter functions were done on the in-memory collection instead of the database. Of course you'd also have this problem if you declared your IQueryable as IEnumerable (an IQueryable IS an IEnumerable), but at least you'd explicitly choose to define it as such. I haven't seen this problem with other .NET interfaces, probably because none of them are so common and so costly as IQueryable, but I could imagine it happening when working with Streams or some such base class which has a few subclasses that do different things.

                          My blog[^]

                          public class SanderRossel : Lazy<Person>
                          {
                          public void DoWork()
                          {
                          throw new NotSupportedException();
                          }
                          }

                          1 Reply Last reply
                          0
                          • F Freak30

                            If you just type one or two letters and then press Ctrl + Space, you would also get Intellisense and have to type even less. ;)

                            The good thing about pessimism is, that you are always either right or pleasently surprised.

                            Sander RosselS Offline
                            Sander RosselS Offline
                            Sander Rossel
                            wrote on last edited by
                            #27

                            I don't think you have to type anything at all...

                            My blog[^]

                            public class SanderRossel : Lazy<Person>
                            {
                            public void DoWork()
                            {
                            throw new NotSupportedException();
                            }
                            }

                            1 Reply Last reply
                            0
                            • N Nareesh1

                              By that same reasoning, stop using any "using" and reference only via fully qualified "System.Windows.Forms.ListCtrl.Item item" :rolleyes: who needs any shortening of a line, just buy BLACK FRIDAY 50" MONITOR THATS IT?

                              Sander RosselS Offline
                              Sander RosselS Offline
                              Sander Rossel
                              wrote on last edited by
                              #28

                              Yep, I know. But I prefer not to include any Namespaces, only class names (the class name should be clear enough anyway). I understand that in the new version of C# you can create a using for a class so you can use its static members without specifying the class. It's already my least favourite feature!

                              My blog[^]

                              public class SanderRossel : Lazy<Person>
                              {
                              public void DoWork()
                              {
                              throw new NotSupportedException();
                              }
                              }

                              1 Reply Last reply
                              0
                              • Sander RosselS Sander Rossel

                                I have a question about your preferred programming style. Whenever I write code in C# I make extensive use of the 'this' keyword and I use class names when referencing statics. So:

                                this.SomeProperty = someValue;
                                this.SomeMethod();
                                ThisClass.SomeStaticMethod();

                                Instead of:

                                SomeProperty = someValue;
                                SomeMethod();
                                SomeStaticMethod();

                                My reasoning behind this is that I can tell something is an instance method or a static method. And to be completely honest it's also something I started doing in VB because VB has Modules and Modules don't make you specify the Module's name. So Me.SomeMethod() can be an instance method, a Shared/static method on the current class or a method in some Module! Now there's a new guy at work and he really hates this style of programming because he thinks it's redundant. I'm not asking for right or wrong (unless I'm the one who's right ;p), but I want to know personal preferences.

                                My blog[^]

                                public class SanderRossel : Lazy<Person>
                                {
                                public void DoWork()
                                {
                                throw new NotSupportedException();
                                }
                                }

                                L Offline
                                L Offline
                                Lost User
                                wrote on last edited by
                                #29

                                I have noticed in Android (and Java generally) they often prefix with m like mSomeMemberVariable instead of using this. maybe it depends on the IDE if it has a good IntelliSense.

                                1 Reply Last reply
                                0
                                • P PIEBALDconsult

                                  Got a link?

                                  N Offline
                                  N Offline
                                  Nish Nishant
                                  wrote on last edited by
                                  #30

                                  I did some searching but could not find that any more. Looks like they don't have a single document any more. This was from several years back. Once you installed the StyleCop plugin (was not part of VS then), it would issue warnings when you accessed a member without prefixing this.

                                  Regards, Nish


                                  Blog: voidnish.wordpress.com

                                  P 1 Reply Last reply
                                  0
                                  • P PIEBALDconsult

                                    Got a link?

                                    N Offline
                                    N Offline
                                    Nish Nishant
                                    wrote on last edited by
                                    #31

                                    Managed to dig out the old StyleCop page : http://stylecop.soyuz5.com/SA1101.html[^] Could not locate the original full document, sorry.

                                    Regards, Nish


                                    Blog: voidnish.wordpress.com

                                    1 Reply Last reply
                                    0
                                    • N Nish Nishant

                                      I did some searching but could not find that any more. Looks like they don't have a single document any more. This was from several years back. Once you installed the StyleCop plugin (was not part of VS then), it would issue warnings when you accessed a member without prefixing this.

                                      Regards, Nish


                                      Blog: voidnish.wordpress.com

                                      P Offline
                                      P Offline
                                      PIEBALDconsult
                                      wrote on last edited by
                                      #32

                                      Nish Sivakumar wrote:

                                      StyleCop

                                      X|

                                      N 1 Reply Last reply
                                      0
                                      • P PIEBALDconsult

                                        Nish Sivakumar wrote:

                                        StyleCop

                                        X|

                                        N Offline
                                        N Offline
                                        Nish Nishant
                                        wrote on last edited by
                                        #33

                                        Heh :-) Not everyone's cup of tea, I know.

                                        Regards, Nish


                                        Blog: voidnish.wordpress.com

                                        1 Reply Last reply
                                        0
                                        Reply
                                        • Reply as topic
                                        Log in to reply
                                        • Oldest to Newest
                                        • Newest to Oldest
                                        • Most Votes


                                        • Login

                                        • Don't have an account? Register

                                        • Login or register to search.
                                        • First post
                                          Last post
                                        0
                                        • Categories
                                        • Recent
                                        • Tags
                                        • Popular
                                        • World
                                        • Users
                                        • Groups