1

I'm working on a widget, for reordering menus.

For this purpose I have added few buttons like MoveUp, MoveDown, Add, Delete which are supposed to change the order of menus in the XML file, as XML file is being read for populating menus.

Now my question is that I have applied file open, write and close operation for every click of these buttons, which instantly changes into xml file. It is relatively easy way for me. Given that, the widget has comboBox too, which read from xml and then populate current order of inner level submenus in the widget. ( same like : MS outlook->tools->customize->RearrangeCommands)

My XML file is a small one around 1.5K. I want to know which option would be better?

i) The one I mentioned above, opening-reading-writing XML files for every mouse click.

ii) Creating a data structure which once reads the file, stores the data and all activity happens with this data and then finally OK buttons write the data into actual XML file.

I personally like option (i) which is very easy to implement overall. And if (ii) one is better option then what data structure should I use? XML file format is like :

<menu>
<action id="1">menu1</action>
<action id="9">menu2</action>
<submenus id="5" name="submenus 1">
        <action id="17">menu14</action>   
    </submenus>
<action id = "10">menu1</action>
<submenus id="7" name="submenus 1">
        <action id="3">menu14</action>   
    </submenus>
<action id="11">menu2</action>
</menu>

Trade-off between frequently reading/writing a small file and once reading, creating a data structure to store data and finally writing only once, which one is better option?

Note : I'm using QT DOM with c++ for all this purpose.

Maverick33
  • 337
  • 3
  • 15
  • 2
    Somehow I feel it the second one. If there is an alternate to frequent file read/write then why not. – Alok Swain Jan 15 '14 at 07:24
  • 1
    The second option might be more user-friendly, as the user can cancel then without modification. As for performance: It probably doesn't make much of a difference to the user, profile it if in doubt – Frank Osterfeld Jan 15 '14 at 08:43

2 Answers2

2

I know this is not c# but I am not too fond with c++, so if someone could fill in my blanks, that would be awesome

In C# you can just load your Xml into an XDocument object and do the manipulations in memory, aka instant.
When done you save them to file.

This has the disadvantage that when you crash, your changes are gone.
To migitate this you can save in a spefified intervall.
Advantage is that you can get rid of your frequent file accesses.

The question IF there is a performance penalty depends on the way you do the IO operations: If you do them async you don't loose much time even if your OS is a bit occupied, sync strongly depends on your OS' performance, in particular the business of the file system.

Also too frequent file operations wear off the storage device. I know we're probably not talking about EEPROM here with ~10k IO-cycles, but still.

Can someone let me know wether there is a namespaces/library for c++ that provides similar functionality?

Mark
  • 419
  • 1
  • 6
  • 21
  • 1
    Seems that you ask for DOM parser -https://en.wikipedia.org/wiki/Document_Object_Model. It is most often MSXML for Windows or Xerces for cross-platform.There is a good link at SO on the subject "What XML parser should I use in C++?"- http://stackoverflow.com/questions/9387610/what-xml-parser-should-i-use-in-c – SChepurin Jan 15 '14 at 08:44
  • Qt has `QDomDocument` for that. – Pavel Strakhov Jan 15 '14 at 11:05
  • 1
    Thanks @Mark , Schepurin and Pavel. I'm using QDomDocument now. Its good way and now I'll need to write in the file only when 'OK' button is pressed – Maverick33 Jan 15 '14 at 11:27
1

I think writing configuration directly into a file is bad practice. Imagine that you want to get the order of menus in some other code. For example, if "File" menu is hidden or moved, you want to display a message box in some moments.

Now you need to open the XML and parse it each time you want to get this information. You need to duplicate code needed for parsing XML and getting menus order from it.

If you had a structure containing your settings, you could just store it somewhere in a variable and use it any time you want. No duplication, more clean code, easier to write unit tests.

Generally, I know from my experience that creating a structure for settings is good. This approach looks harder at the start, but most likely will save you time in the future.

Pavel Strakhov
  • 39,123
  • 5
  • 88
  • 127
  • Yes, this approach surely looks harder :P , but as you have suggested above I'm now using QDomDOcument, and writing in the file only after 'OK' button is pressed. Though I'll have the problem you have mentioned, that parsing XML and getting menus order-creating duplication of codes n all...But right now I'd like to do it in simpler way. Thanks for the suggestion. – Maverick33 Jan 15 '14 at 11:27