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
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. Style Guide for Obfuscation?

Style Guide for Obfuscation?

Scheduled Pinned Locked Moved The Weird and The Wonderful
toolshelptutorialquestion
7 Posts 5 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.
  • B Offline
    B Offline
    Bernhard Hiller
    wrote on last edited by
    #1

    A colleague told me about a great bugfixing experience: a local variable had the same name as a member variable. "But the compiler will show you a warning." - "No, it does not." Hard to believe, hence I tried it with a very simple class. My colleague was right: there is no compiler warning when a local variable overwrites a member variable. But there are some tools which could help us here, like StyleCop. I installed StyleCop, and eventually got it accepting my code:

    // <copyright file="DontDoThat.cs" company="blah">blah</copyright>
    namespace Nonsense
    {
    /// <summary>
    /// Blah blah
    /// </summary>
    public class DontDoThat
    {
    /// <summary>
    /// Blah blah
    /// </summary>
    private int someValue = 0;

        /// <summary>
        /// Blah blah
        /// </summary>
        public void DoSomething()
        {
            if (this.someValue == 0)
            {
                this.DoSomethingElse();
            }
        }
    
        /// <summary>
        /// Blah blah
        /// </summary>
        private void DoSomethingElse()
        {
            int someValue = 42;
            int somethingElse = someValue \* someValue;
        }
    }
    

    }

    Well, that's clean code, isn't it? StyleCop suggested great things. Still a local variable can overwrite a member variable, but now you see the difference more clearly: a member variable has a "this." prefix. Hungarian notation is so great. X|

    Richard DeemingR realJSOPR R N 4 Replies Last reply
    0
    • B Bernhard Hiller

      A colleague told me about a great bugfixing experience: a local variable had the same name as a member variable. "But the compiler will show you a warning." - "No, it does not." Hard to believe, hence I tried it with a very simple class. My colleague was right: there is no compiler warning when a local variable overwrites a member variable. But there are some tools which could help us here, like StyleCop. I installed StyleCop, and eventually got it accepting my code:

      // <copyright file="DontDoThat.cs" company="blah">blah</copyright>
      namespace Nonsense
      {
      /// <summary>
      /// Blah blah
      /// </summary>
      public class DontDoThat
      {
      /// <summary>
      /// Blah blah
      /// </summary>
      private int someValue = 0;

          /// <summary>
          /// Blah blah
          /// </summary>
          public void DoSomething()
          {
              if (this.someValue == 0)
              {
                  this.DoSomethingElse();
              }
          }
      
          /// <summary>
          /// Blah blah
          /// </summary>
          private void DoSomethingElse()
          {
              int someValue = 42;
              int somethingElse = someValue \* someValue;
          }
      }
      

      }

      Well, that's clean code, isn't it? StyleCop suggested great things. Still a local variable can overwrite a member variable, but now you see the difference more clearly: a member variable has a "this." prefix. Hungarian notation is so great. X|

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      If Visual Studio did everything for you, there'd be no reason to buy a tool like ReSharper! :rolleyes:


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      B 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        If Visual Studio did everything for you, there'd be no reason to buy a tool like ReSharper! :rolleyes:


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        B Offline
        B Offline
        Bernhard Hiller
        wrote on last edited by
        #3

        Good point. But are sure that ReSharper does find that? Really, did you check it?

        Richard DeemingR 1 Reply Last reply
        0
        • B Bernhard Hiller

          Good point. But are sure that ReSharper does find that? Really, did you check it?

          Richard DeemingR Offline
          Richard DeemingR Offline
          Richard Deeming
          wrote on last edited by
          #4

          Yes: Local variable hides member - ReSharper - Confluence[^]


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

          1 Reply Last reply
          0
          • B Bernhard Hiller

            A colleague told me about a great bugfixing experience: a local variable had the same name as a member variable. "But the compiler will show you a warning." - "No, it does not." Hard to believe, hence I tried it with a very simple class. My colleague was right: there is no compiler warning when a local variable overwrites a member variable. But there are some tools which could help us here, like StyleCop. I installed StyleCop, and eventually got it accepting my code:

            // <copyright file="DontDoThat.cs" company="blah">blah</copyright>
            namespace Nonsense
            {
            /// <summary>
            /// Blah blah
            /// </summary>
            public class DontDoThat
            {
            /// <summary>
            /// Blah blah
            /// </summary>
            private int someValue = 0;

                /// <summary>
                /// Blah blah
                /// </summary>
                public void DoSomething()
                {
                    if (this.someValue == 0)
                    {
                        this.DoSomethingElse();
                    }
                }
            
                /// <summary>
                /// Blah blah
                /// </summary>
                private void DoSomethingElse()
                {
                    int someValue = 42;
                    int somethingElse = someValue \* someValue;
                }
            }
            

            }

            Well, that's clean code, isn't it? StyleCop suggested great things. Still a local variable can overwrite a member variable, but now you see the difference more clearly: a member variable has a "this." prefix. Hungarian notation is so great. X|

            realJSOPR Offline
            realJSOPR Offline
            realJSOP
            wrote on last edited by
            #5

            It doesn't give you a warning because it's perfectly valid code. If you want to use the class var, you can qualify it with this.varname. Don't make the mistake of using the IDE as a crutch.

            ".45 ACP - because shooting twice is just silly" - JSOP, 2010
            -----
            You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
            -----
            When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

            1 Reply Last reply
            0
            • B Bernhard Hiller

              A colleague told me about a great bugfixing experience: a local variable had the same name as a member variable. "But the compiler will show you a warning." - "No, it does not." Hard to believe, hence I tried it with a very simple class. My colleague was right: there is no compiler warning when a local variable overwrites a member variable. But there are some tools which could help us here, like StyleCop. I installed StyleCop, and eventually got it accepting my code:

              // <copyright file="DontDoThat.cs" company="blah">blah</copyright>
              namespace Nonsense
              {
              /// <summary>
              /// Blah blah
              /// </summary>
              public class DontDoThat
              {
              /// <summary>
              /// Blah blah
              /// </summary>
              private int someValue = 0;

                  /// <summary>
                  /// Blah blah
                  /// </summary>
                  public void DoSomething()
                  {
                      if (this.someValue == 0)
                      {
                          this.DoSomethingElse();
                      }
                  }
              
                  /// <summary>
                  /// Blah blah
                  /// </summary>
                  private void DoSomethingElse()
                  {
                      int someValue = 42;
                      int somethingElse = someValue \* someValue;
                  }
              }
              

              }

              Well, that's clean code, isn't it? StyleCop suggested great things. Still a local variable can overwrite a member variable, but now you see the difference more clearly: a member variable has a "this." prefix. Hungarian notation is so great. X|

              R Offline
              R Offline
              RugbyLeague
              wrote on last edited by
              #6

              Why would the compiler complain, name scope is part of the language

              1 Reply Last reply
              0
              • B Bernhard Hiller

                A colleague told me about a great bugfixing experience: a local variable had the same name as a member variable. "But the compiler will show you a warning." - "No, it does not." Hard to believe, hence I tried it with a very simple class. My colleague was right: there is no compiler warning when a local variable overwrites a member variable. But there are some tools which could help us here, like StyleCop. I installed StyleCop, and eventually got it accepting my code:

                // <copyright file="DontDoThat.cs" company="blah">blah</copyright>
                namespace Nonsense
                {
                /// <summary>
                /// Blah blah
                /// </summary>
                public class DontDoThat
                {
                /// <summary>
                /// Blah blah
                /// </summary>
                private int someValue = 0;

                    /// <summary>
                    /// Blah blah
                    /// </summary>
                    public void DoSomething()
                    {
                        if (this.someValue == 0)
                        {
                            this.DoSomethingElse();
                        }
                    }
                
                    /// <summary>
                    /// Blah blah
                    /// </summary>
                    private void DoSomethingElse()
                    {
                        int someValue = 42;
                        int somethingElse = someValue \* someValue;
                    }
                }
                

                }

                Well, that's clean code, isn't it? StyleCop suggested great things. Still a local variable can overwrite a member variable, but now you see the difference more clearly: a member variable has a "this." prefix. Hungarian notation is so great. X|

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

                Strictly speaking, there is no need for a compiler warning here really. It's well defined C# language behavior. Also, nothing's overwriting anything here. You declare a local and use the local. It has the same name as a member, but that's not the compiler's problem. That said, code analysis would catch this. See CA1500: Variable names should not match field names[^]

                Regards, Nish


                Website: www.voidnish.com 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