0

This is not my code I just have to understand it. The original programmer cannot be reached. dobj is just an object type. My main question is: Why does he deserialize again when dobj was never changed? Please ignore all his goto's for right now they are everywhere in this program.

        ////////////////////////
        //Deserialize Original//
        ////////////////////////
        dobj = Generics.IO.BinarySerializer.Open(g_PathToTMP);

        if (dobj == null)
        {
            ///////
            //LOG//
            ///////

            goto Label_Done;
        }

        dccmcaltered = dobj as ASM001.MatSettings;

        if (dccmcaltered == null)
            goto Label_Done;
        //

        //////////////////////////////////////////
        //Apply Changes To Deserialized Original//
        //////////////////////////////////////////
        dccmcaltered.ObjectLocation = wpuiobj.ObjectLocation;
        dccmcaltered.ObjectOffset = wpuiobj.ObjectOffset;
        dccmcaltered.UserDefinedLocation = wpuiobj.UserDefinedLocation;
        dccmcaltered.Locked = wpuiobj.Locked;
        dccmcaltered.RinseLocation = wpuiobj.RinseLocation;
        dccmcaltered.RinseDepth = wpuiobj.RinseDepth;
        dccmcaltered.DrainLocation = wpuiobj.DrainLocation;
        dccmcaltered.DrainDepth = wpuiobj.DrainDepth;
        //

        ////////////////////////
        //Deserialize Original//Why did we need to Deserialize again
        ////////////////////////
        dobj = Generics.IO.BinarySerializer.Open(g_PathToTMP);

        if (dobj == null)
        {
            ///////
            //LOG//
            ///////

            goto Label_Done;
        }

        dccmcoriginal = dobj as ASM001.MatSettings;
        if (dccmcoriginal == null)
            goto Label_Done;
        //

        bResult = Generics.IO.SerializerPlus.IsBinaryEqual(dccmcoriginal, dccmcaltered);

        Label_Done:
        ;

        bCurrent = bResult;

        ///////////
        //Cleanup//
        ///////////
        FileInfo fInfo = new FileInfo(g_PathToTMP);

        if (fInfo.Exists)
            fInfo.Delete();
        //

        System.Diagnostics.Debug.WriteLineIf(!bCurrent && g_bVerbose, "[Main] Mat is not Current [ASM = 1]!");
        System.Diagnostics.Debug.WriteLineIf(bCurrent && g_bVerbose, "[Main] Mat is Current! [ASM = 0]");

Edit I added the rest of the method

Josh Davis
  • 90
  • 8
  • 1
    OMG why are you/whoever using `goto`.... – Federico Berasategui Oct 13 '14 at 18:31
  • He was fired right? Is the code exactly the same as the original workflow after he deserialized a 2nd time? Could be as simple as a copy paste issue. Just looks like an oversight and no real reason. Any answer will be pure assumption. – TyCobb Oct 13 '14 at 18:33
  • 1
    Basically, it's because he's a bad programmer. That's unnecessary code right there, and the `goto` just reinforces my assumption. – WGS Oct 13 '14 at 18:33
  • This should go straight to [codecrap.com](http://codecrap.com/) – Federico Berasategui Oct 13 '14 at 18:37
  • Your edit clearly shows he is checking the original vs the altered object. That's why the two deserialize calls. – TyCobb Oct 13 '14 at 18:38
  • `dobj` is just a placeholder variable. This code deserializes from a file, applies changes, then compares the changed object against the original object, possibly to implement a form of optimistic locking (although, since the file is unconditionally deleted at the end, I don't see how that would work). – Jeroen Mostert Oct 13 '14 at 18:39
  • @Jeronen bCurrent is passed by reference. – Josh Davis Oct 13 '14 at 18:41
  • @JoshDavis Ah, mystery solved then -- something is done with the result after all. – Jeroen Mostert Oct 13 '14 at 19:17

3 Answers3

4

Why does he deserialize again when dobj was never changed?

The object referenced by dobj was changed. It is the same object no matter whether you refer to it via dobj or dccmcaltered:

dccmcaltered = dobj as ASM001.MatSettings;

This just gets a different typed reference to the same object;

dccmcaltered.ObjectLocation = wpuiobj.ObjectLocation;
dccmcaltered.ObjectOffset = wpuiobj.ObjectOffset;
dccmcaltered.UserDefinedLocation = wpuiobj.UserDefinedLocation;
dccmcaltered.Locked = wpuiobj.Locked;

And now: the values are changed.

Note that dccmcaltered retains a reference to this original object, so those changes remain accessible even after dobj is assigned a different object.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • Gotcha that's is were my lack in prgramming knowledge is. I didn't realize that changing dccmcaltered changed dobj. Thanks – Josh Davis Oct 13 '14 at 18:43
  • 1
    @JoshDavis it is important to keep in mind the difference between *references* and *objects*. You can have 3000 references that all point to the same object. If you change that one object via *any* of those references: the change will be visible from the rest. There is, after all, *only one object*. – Marc Gravell Oct 13 '14 at 18:44
  • Again my lack of knowledge. I thought he created an object dccmcaltered then sets its members with the dobj as ASM001. I was thinking it was a copy of the dobj not a reference – Josh Davis Oct 13 '14 at 18:49
2

Somewhere later on he wants to compare the unaltered and the updated version. First he deserializes to dccmcaltered , and sets some properties. Then he deserializes to dccmcoriginal without setting these properties.

Glad I don't have to maintain this... Good luck!

Yep, the comparison is (aargh): bResult = Generics.IO.SerializerPlus.IsBinaryEqual(dccmcoriginal, dccmcaltered);

So you could just delete all about dccmcoriginal, and verify if the properties need to change before actually changing them.

0

Because he is changing firstly deserialized object dobj with lines like dccmcaltered.ObjectLocation = wpuiobj.ObjectLocation; so he loses "original" dccmcaltered.ObjectLocation values and "restores" them (into another object) with second deserialization... strange guy... cheers

Mikant
  • 299
  • 3
  • 18