Protecting the instance variables...



  • The huge body of C# I'm trying to beat into shape has many examples of this:

        public class Foo
        {
            private Bar _bar;
    
            public void DoSomething()
            {
                Bar bar = _bar;
                // 50 to 100 lines using "bar"
            }
    

    This is C#; in this function using "bar" is no different from using "_bar" except that under the covers I imagine there's something going on along the line of saving the same address in two places (the stack and heap) instead of one (the heap).

    Maybe the author was embarrassed about so many methods operating on instance variables?

    The picky-ass problem I have is that this practice obscures the fact that this method changes the Foo instance's state. And also it's stupid and pointless.


  • Discourse touched me in a no-no place

    @rstinejr said:

    This is C#; in this function using "bar" is no different from using "_bar"
    Is it the intention of DoSomething() to modify _bar, or did the original author think he was working on a copy?



  • @rstinejr said:

    The huge body of C# I'm trying to beat into shape has many examples of this:

        public class Foo
        {
            private Bar _bar;
    
            public void DoSomething()
            {
                Bar bar = _bar;
                // 50 to 100 lines using "bar"
            }
    

    This is C#; in this function using "bar" is no different from using "_bar" except that under the covers I imagine there's something going on along the line of saving the same address in two places (the stack and heap) instead of one (the heap).

    Maybe the author was embarrassed about so many methods operating on instance variables?

    The picky-ass problem I have is that this practice obscures the fact that this method changes the Foo instance's state. And also it's stupid and pointless.

    Does the unobfuscated code look more like:

        private Bar _myInstCopyOfBar;
    
        void DoSomething()
        {
            Bar bar = _myInstCopyOfBar;
            //use bar
        }
    

    Because, I can understand not wanting to type out a long name 500 times.

    Alternately, if your class is multithreaded, _bar might be switched out mid-procedure. Saving a copy of the reference would prevent that.



  • @PJH said:

    @rstinejr said:
    This is C#; in this function using "bar" is no different from using "_bar"
    Is it the intention of DoSomething() to modify _bar, or did the original author think he was working on a copy?

    We have not seen the declaration of "Bar"...if it is a struct, then he IS working on a copy...

    Even if it is a class, the code ensures that the SAME INSTANCE is being used accross the board. Without the local variable, a reassignment of the member variable to another instance in the middle of calculations would create havoc. If one is following the "immutable object" design patterns [which have some major benefits for scalability - if is just planning for the future] then assignment to a different instance is going to be what happens rather than the contents of a given instance changing.



  • @TheCPUWizard said:

    We have not seen the declaration of "Bar"...if it is a struct, then he IS working on a copy...

    Not in C#.



  • It's not that bad, but I hate it simple because it makes the code confusing. To me, it implies that you're treating bar differently than _bar, and I would waste time focusing on why/where it's happending.

    Hell, I recall pulling out my hair because I spent nearly an hour debugging a 1000 line method (30% of which was unused variables, etc.) and somewhere in the pile of shit, he assigned a class variable to a local variable. Then a few lines down, he was changing the local variables properties (which I obviously didn't associate with the class variable) and was wondering why the fuck the class variable was changing. In one instance, i found this indirection two levels deep: assigning the class variable to a local variable, and another local variable to the local variable he had just created, and was modifying all fucking three in the method!!! Of course, the method was a slender 1300 lines...

    If we're talking about coding standards in C#, for private methods, etc., you should always use the private field directly, and not the public property simply because getting/setting the property can have side effects.



  • @C-Octothorpe said:

    It's not that bad, but I hate it simply because it makes the code confusing.

    Bingo!


  • Discourse touched me in a no-no place

    @rstinejr said:

    @TheCPUWizard said:

    We have not seen the declaration of "Bar"...if it is a struct, then he IS working on a copy...

    Not in C#.

    Not according to this page - Value and Reference Types section. (Though it's not beyond the bounds of possibility that that page is wrong.)



  • Build and run the code below; output is

    _bar._val is 0
    _bar._val is 999
    

    That is, after the assignment Bar locBar = _bar, changes to locBar show up in _bar.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    

    namespace ConsoleApplication1
    {
    class Bar
    {
    public int _val = 0;
    public void ModVal()
    {
    _val = 999;
    }
    }
    class Foo
    {
    Bar _bar = new Bar();
    public void DoStuff()
    {
    Bar locBar = _bar;
    Console.WriteLine(string.Format("_bar._val is {0}", _bar._val));

            locBar.ModVal();
    
            Console.WriteLine(string.Format("_bar._val is {0}", _bar._val));
        }
    
        static void Main(string[] args)
        {
            new Foo().DoStuff();
        }
    }
    

    }



  • @PJH said:

    @rstinejr said:
    @TheCPUWizard said:

    We have not seen the declaration of "Bar"...if it is a struct, then he IS working on a copy...

    Not in C#.

    Not according to this page - Value and Reference Types section. (Though it's not beyond the bounds of possibility that that page is wrong.)

    Excerpt from the page PJH referenced...

     If we copy the objects to new variables:

    Point p2 = p1;
    Form f2 = f1;

    p2, being a struct, becomes an independent copy of p1, with its own separate fields.  But in the case of f2, all we’ve copied is a reference, with the result that both f1 and f2 point to the same object.



  • @rstinejr said:


    Build and run the code below; output is

    _bar._val is 0
    _bar._val is 999
    

    That is, after the assignment Bar locBar = _bar, changes to locBar show up in _bar.

     Now change "class Bar" to "struct Bar", and you will see exactly what I posted.
     



  • rstinejr, you clearly do not know what you are talking about. Structs are copied when they are assigned.

    <rant>I am so sick of people bitching about minor stylistic problem; usually because they do not understand the actual architechture/technical problems that are occuring in an app</rant>

    Seriously,
     If this is your definition of a WTF, I would love to take on your job. 



  • @PJH said:

    @rstinejr said:
    @TheCPUWizard said:

    We have not seen the declaration of "Bar"...if it is a struct, then he IS working on a copy...

    Not in C#.

    Not according to this page - Value and Reference Types section. (Though it's not beyond the bounds of possibility that that page is wrong.)

    The page is about references and copies of scalars, not classes. In C#, when you assign class instances, you are using a reference rather than executing a copy constructor.



  • @this_code_sucks said:

    rstinejr, you clearly do not know what you are talking about. Structs are copied when they are assigned.

    <rant>I am so sick of people bitching about minor stylistic problem; usually because they do not understand the actual architechture/technical problems that are occuring in an app</rant>

    Seriously,
     If this is your definition of a WTF, I would love to take on your job. 

    See the code above, asshole. Get a friend to compile and run it for you, because you are probably too stupid to copy-and-paste.



  • @PJH said:

    p2, being a struct, becomes an independent copy of p1, with its own separate fields.

    Sorry, my mistake, I had my own C++ blinders on. I've said elsewhere that I'm rusty in C#; I'm used to a struct being a class with weird default permissions.

    For the record, I never said that the problem was with structs.

    this_code_sucks, you are still an utter asshole.



  • @rstinejr said:

    @PJH said:
    @rstinejr said:
    @TheCPUWizard said:

    We have not seen the declaration of "Bar"...if it is a struct, then he IS working on a copy...

    Not in C#.

    Not according to this page - Value and Reference Types section. (Though it's not beyond the bounds of possibility that that page is wrong.)

    The page is about references and copies of scalars, not classes. In C#, when you assign class instances, you are using a reference rather than executing a copy constructor.

    You are the one with a comprhension problem, we are NOT talking about CLASS insances, we are talking abot STRUCT instances. Now in C++ these are the same, except fo rdefault visibility. In C# they are completely different. Make the edit I told you (change bar from a class to a struct)...


  • Discourse touched me in a no-no place

    @rstinejr said:

    @PJH said:
    @rstinejr said:
    @TheCPUWizard said:

    We have not seen the declaration of "Bar"...if it is a struct, then he IS working on a copy...

    Not in C#.

    Not according to this page - Value and Reference Types section. (Though it's not beyond the bounds of possibility that that page is wrong.)

    The page is about references and copies of scalars, not classes. In C#, when you assign class instances, you are using a reference rather than executing a copy constructor.

    If you'd care to reread the quote to which you replied "Not in C#", you'll see that we are talking about structs, not classes.


  • You never defined Bar in your orginial code, so it would be difficult for my friend to compile it.

    If I'm an asshole, I'm not sure what that makes you for publicly bitching about one of your coworker's code which only reassigns an instance field inside a method. A "mistake" which not only causes you a minimum of trouble, but could also be easily fixed in less time than it took you to post this.


    I'd also rather be an asshole than so incompetant that I don't know assigning a struct creates a copy in C#. I mean that is novice level stuff right there.



  • @PJH said:

    If you'd care to reread the quote to which you replied "Not in C#", you'll see that we are talking about structs, not classes.

    Right you are. My bad. Not sure why we started talking about structs, but whatever.



  • @rstinejr said:

    @PJH said:
    If you'd care to reread the quote to which you replied "Not in C#", you'll see that we are talking about structs, not classes.

    Right you are. My bad. Not sure why we started talking about structs, but whatever.



    Because Bar being a struct would provide a valid rational for your coworkers "WTF"


  • @rstinejr said:

    @PJH said:
    If you'd care to reread the quote to which you replied "Not in C#", you'll see that we are talking about structs, not classes.

    Right you are. My bad. Not sure why we started talking about structs, but whatever.

    We (Actually I) started because the original poster did not show how Bar was defined (so it could be a class or a struct). Without knowing this, the impact of the assignment to a local can not be determined; thus declaring it a WTF is meaningless. I have posted reasons why it *might* be a "good design" for both conditions (with the case for struct being the stronger one).



  • @rstinejr said:


    Build and run the code below; output is

    _bar._val is 0
    _bar._val is 999
    

    That is, after the assignment Bar locBar = _bar, changes to locBar show up in _bar.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    

    namespace ConsoleApplication1
    {
    class Bar
    {
    public int _val = 0;
    public void ModVal()
    {
    _val = 999;
    }
    }
    class Foo
    {
    Bar _bar = new Bar();
    public void DoStuff()
    {
    Bar locBar = _bar;
    Console.WriteLine(string.Format("_bar._val is {0}", _bar._val));

            locBar.ModVal();
    
            Console.WriteLine(string.Format("_bar._val is {0}", _bar._val));
        }
    
        static void Main(string[ args)
        {
            new Foo().DoStuff();
        }
    }
    

    }



    Even if Bar is a class as opposed to a struct.
    While bar.DoSomething() would be equivlent to _bar.DoSomething()
    bar = new Bar() is not the same as _bar = new Bar()

  • Discourse touched me in a no-no place

    @TheCPUWizard said:

    @rstinejr said:
    @PJH said:
    If you'd care to reread the quote to which you replied "Not in C#", you'll see that we are talking about structs, not classes.

    Right you are. My bad. Not sure why we started talking about structs, but whatever.

    We (Actually I) started because the original poster did not show how Bar was defined (so it could be a class or a struct).

    rstinejr is the OP. I think it's probably safe to say from the rest of their posts to this thread (and from a statement in the OP) that it is a class, and not a struct.


  • Ok, which one of you guys is bolus, and which is horseflop?

    ok I have literally no way to fix this post in windows phone 7. Literally impossible.



    [Fixed your link. -ShadowMod]



  • @blakeyrat said:

    <a href="http://m.youtube.com/#/watch?v=UGxgFaMu8jM" mce_href="http://m.youtube.com/#/watch?v=UGxgFaMu8jM">Ok, which one of you guys is bolus, and which is horseflop?</a>

    ok I have literally no way to fix this post in windows phone 7. Literally impossible.



    I was hoping for one of your blakeyrants (I am a first time poster, long time reader, and I love them), and all I got was a youtube search result :-/



  • In Windows Phone 7, you can't turn off TinyMCE (it brings up some error about a popup blocker?), you can't use the Link button in the TinyMCE toolbar, and you also can't use the HTML button. So... it's impossible to post a link to this forum from Windows Phone 7. Awesome.

    Anyway, I thought that back-and-forth sounded a lot like the voices Croooow and Servo used for Bolus and Horseflop. That was the point I was trying to make. WORK THE LUMPS!



  • Posting from a phone is TRWTF.



  • @El_Heffe said:

    Posting from a phone is TRWTF.
     

    Yeah. Just use your phone to RDP to your home machine, and post from there.


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.