I have a file editor. It reads from a file, creates an object with the read data and can then write that data into a same (or another) file.

Every time user opens a file a new tab is created and a private instance of the object is assigned to it, containing it in the tab.

The object itself is complex and is composited of other objects. Each custom type has a Write() and Read() methods that extract and save data from and into the file. Something like this:

TopObject a0
    int a
    int b
    string c
    CustomObjectMid d
        int e
        bool f
    List<CustomObjectMid2> g
    CustomObjectMid3 h
        CustomObjectBottom i
            long j
            ulong k
        Dictionary<string, int> l
    byte m

The real object is much bigger and has more depth (levels) to it.

TopObject, CustomObjectMid, CustomObjectMid2, CustomObjectMid3 and CustomObjectBottom all have a Write() and Read() methods that populate all standard types with data from file. They don't contain any other methods. They don't even have constructors (other than the default).

The sole purpose of this object is to hold information. It's essentially a structure. This is actually written as a DLL which purpose it to enable users to make sense of that particular file format (read from it and write to it). So the actual code is not in my program, but is a part of a library. As such, I'd like to keep the library relatively "neutral" - I don't want to implement code that is specific to the editor into the library.

The way this object is implemented is that everything is public and I can access anything in a0.h.i.j manner. I tend to pass relevant parts of the objects to pieces of program that use it, so I don't have to reference to everything from the top (a0).

Other parts of the program display the data to the user and translate the changes user makes into the object. They also perform the necessary checks to keep the integrity of data (prevent passing of chars for ints, etc)

My question is is this good design? I decided not to use getters and setters. It would just bloat the code for no reason (at least I can't see one).

All that being said, for some reason this smells like a bad code to me (not sure why), but I don't really see any other way of implementing it without doing an overkill, given it's role in the program.

EDIT: Here is the full code of the root class. All the other classes have the same structure:

namespace SaveManipulator.GameData
public class CustomFile
    //version = 7
    public Value<uint> saveFileVersion;

    public ECD ecd;

    public Food food;

    public Drink drink;

    public Stack[] inventory;

    public Value<int> selectedSlot;

    public Stack[] bag;

    public HashSet<string> craftedList;

    public List<Vector3Di> spawnPoints;

    public Value<long> SpawnKey;

    //notSaved = true
    public Value<bool> randomBoolean;

    //notSaved = 0
    public Value<short> randomShort;

    public Value<bool> bLoaded;

    public Vector3Di lastPosition;

    public Value<int> id;

    public Vector3Di droppedPosition;

    public Value<int> kills;

    public Value<int> fKills;

    public Value<int> deaths;

    public Value<int> score;

    public Equipment equipment;

    public List<string> unlockedList;

    //notSaved = 1
    public Value<ushort> randomUShort;

    public Vector3Di marker;

    public Equipment favorites;

    public Value<uint> experience;

    public Value<int> level;

    public Value<bool> bCrouched;

    public Cdata cdata;

    public List<string> favoriteList;

    public Skills skills;

    public Value<uint> totalItems;

    public Value<float> distance;

    public Value<float> life;

    public Waypoints waypoints;

    public Value<int> skillPoints;

    public Quests quests;

    public Value<int> deathTime;

    public Value<float> currentL;

    public Value<bool> bDead;

    public CustomFile Clone()
        Stream stream = new FileStream("CustomFile ", FileMode.Create, FileAccess.Write, FileShare.None);

        IFormatter formatter = new BinaryFormatter();
        formatter.Serialize(stream, this);

        stream = new FileStream("CustomFile", FileMode.Open, FileAccess.Read, FileShare.None);
        PlayerDataFile clone = (PlayerDataFile)formatter.Deserialize(stream);


        return clone;

    public void Read(string path)
        BinaryReader reader = new BinaryReader(new FileStream(path, FileMode.Open));

        if (reader.ReadChar() == 'd' && reader.ReadChar() == 't' && reader.ReadChar() == 's' &&
            reader.ReadChar() == '\0')
            saveFileVersion = new Value<uint>((uint)reader.ReadByte());

            ecd = new ECD();
            food = new Food();
            drink = new Drink();

            inventory = Stack.Read(reader);
            selectedSlot = new Value<int>((int)reader.ReadByte());
            bag = Stack.Read(reader);

            int craftedListLength = (int)reader.ReadUInt16();
            craftedList = new HashSet<string>();
            for (int i = 0; i < craftedListLength; i++)

            byte spawnPointsCount = reader.ReadByte();
            spawnPoints = new List<Vector3Di>();
            for (int j = 0; j < (int)spawnPointsCount; j++)
                Vector3Di spawnPoint = new Vector3Di();
                spawnPoint.x = new Value<int>(reader.ReadInt32());
                spawnPoint.y = new Value<int>(reader.ReadInt32());
                spawnPoint.z = new Value<int>(reader.ReadInt32());


            spawnKey = new Value<long>(reader.ReadInt64());

            randomBoolean = new Value<bool>(reader.ReadBoolean());
            randomShort = new Value<short>(reader.ReadInt16());

            bLoaded = new Value<bool>(reader.ReadBoolean());

            lastPosition = new Vector3Di();
            lastPosition.x = new Value<int>(reader.ReadInt32());
            lastPosition.y = new Value<int>(reader.ReadInt32());
            lastPosition.z = new Value<int>(reader.ReadInt32());
            lastPosition.heading = new Value<float>(reader.ReadSingle());

            id = new Value<int>(reader.ReadInt32());

            droppedPosition = new Vector3Di();
            droppedPosition.x = new Value<int>(reader.ReadInt32());
            droppedPosition.y = new Value<int>(reader.ReadInt32());
            droppedPosition.z = new Value<int>(reader.ReadInt32());

            kills = new Value<int>(reader.ReadInt32());
            fKills = new Value<int>(reader.ReadInt32());
            deaths = new Value<int>(reader.ReadInt32());
            score = new Value<int>(reader.ReadInt32());
            equipment = Equipment.Read(reader);

            int count = (int)reader.ReadUInt16();
            unlockedList = new List<string>();
            for (int k = 0; k < count ; k++)
                unlockedList .Add(reader.ReadString());

            randomUShort = new Value<ushort>(reader.ReadUInt16());

            marker = new Vector3Di();
            marker.x = new Value<int>(reader.ReadInt32());
            marker.y = new Value<int>(reader.ReadInt32());
            marker.z = new Value<int>(reader.ReadInt32());

            favorites = Equipment.Read(reader);
            experience = new Value<uint>(reader.ReadUInt32());
            level = new Value<int>(reader.ReadInt32());

            bCrouched = new Value<bool>(reader.ReadBoolean());
            cdata = new Cdata();

            int favoriteListSize = (int)reader.ReadUInt16();
            favoriteList = new List<string>();
            for (int l = 0; l < favoriteListSize; l++)

            int size = (int)reader.ReadUInt32();

            skills = new Skills();

            totalItems = new Value<uint>(reader.ReadUInt32());
            distance = new Value<float>(reader.ReadSingle());
            life = new Value<float>(reader.ReadSingle());

            waypoints = new Waypoints();

            skillPoints = new Value<int>(reader.ReadInt32());

            quests = new Quests();

            deathTime = new Value<int>(reader.ReadInt32());
            currentL = new Value<float>(reader.ReadSingle());
            bDead = new Value<bool>(reader.ReadBoolean());

            //irelevant bytes

            throw new IOException("Save file corrupted!");

    public void Write(string path)
        BinaryWriter writer = new BinaryWriter(new FileStream(path, FileMode.Create));



        Stack.WriteItemStack(writer, inventory);
        Stack.WriteItemStack(writer, bag);

        HashSet<string>.Enumerator enumerator = craftedList.GetEnumerator();
        while (enumerator.MoveNext())

        for (int i = 0; i < spawnPoints.Count; i++)




        List<string>.Enumerator enumerator2 = unlockedList.GetEnumerator();
        while (enumerator2.MoveNext())


        List<string>.Enumerator enumerator3 = favoriteList.GetEnumerator();
        while (enumerator3.MoveNext())






    public CustomFile(string path)

Since the code is mostly copied from the source of the game, I obfuscated it a little bit, that's why some names may look vague. Also Value<Type> is a wrapper class used to pass around value types while keeping their reference without explicitly having to think about it. That way the user can change bits of the file easier. I don't really see how I could partially save the file. Save files are usually from 4kB to (my estimate) 10kB. They aren't that big.

2 Answers 2


Oh dear, what has become of the world, when people are worried about using a simple structure?

Yes, this is perfectly fine. Your code does not smell. Structures are just another name for record types (although using the word structure usually connotes mutability), which are a form of product type. Product types are one of the two fundamental composite data types in programming languages / type systems (the other being sum types). The more you can rely on these simple types, the better. Getters and setters are absolutely overkill.

For what it's worth, objects are just record types where some of the fields are functions (with a sprinkling of self-reference to keep things adequately confusing). Classes are just a type-alias for a certain record type along with a function (called the constructor) that creates records of that type.


I wouldnt say its bad, but it would be very hard to program well.

The key problem comes around the requirement to save/read only parts of the object. Leading to the read/write methods in each object rather than soley on the root object.

You can imagine the problems, for example I see you have a list g under the root object. If I create a new g and add it to the list, does that auto save it? Do i need to call g.save() or root.save() or both?

Can I load a sub object without first loading its parent?

It seems like this library is following an active record pattern. A more modern approach would be to only save/load the top level 'Aggregate root' object.

You would probably write a seperate repository object to Handle the file format and writing to disk etc.

However active record was all the rage a few years ago, and if the requiremenr to load/save parts of the root object is a must, then you would need to write something like this. I know dome people take a very hard line approach to 'code smell' I am a bit more forgiving. I would say yes its a code smell, but if you investigate you might find there is a good reason for it!

In any case since you are consuming the library you dont have a whole lot of choice in the matter.

My gut reaction is to try and wrap it in a repository style of save /load, iterating through the tree and saving all the items. Only passing the root into your own functions etc but without a full understanding its a tough call to assume this would be a sensible use of time.

Are you writing lots of duplicate 'save all g and childern' 'load all d where x' style logic? Have you found a bug where you need to check the file for a parent before you know child.save() will execute correctly? or does it 'just work'?

Edit ****

Sorry I just realised its a library which you wrote yourself. Definitely rewrite as just a struct in a Models dll plus a FileRepository in a second dll. Then people can consume the object without the saving logic and you can add additional DatabaseRepository, InMemoryRespository etc etc later

  • The thing is that it's a savegame file. I'm updating the question with a sample of root class. The class doesn't really make much sense if I don't add the save/load code. The whole object is saved by calling rootInstance.Write(string path) which then deals with all the rest of the saving. Since it's a binary file, I don't think inserting is a thing that would speed up anything. Is it? Commented Sep 26, 2016 at 9:26
  • i would totaly split it into GameState and GameStateRepository. The key thing I notice is your Equipment.Read() method. Say you change the format to xml, or download a save from the internet. You dont want Equipment to know about binary writer
    – Ewan
    Commented Sep 26, 2016 at 12:58
  • Also, game are notorious for breaking changes in save games. Putting the parsing logic in a repository allows you to have GameStateRepoV1 etc
    – Ewan
    Commented Sep 26, 2016 at 13:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.