Is using MultiMaps code smell? If so what alternative data structures fit the needs

code smelldata structures

I'm trying to model nWoD characters for a roleplaying game in a character builder program. The crux is I want to support saving too and loading from yaml documents.

One aspect of the character's is their set of skills. Skills are split between exactly three 'types': Mental, Physical, and Social. Each type has a list of skills under if. My Yaml looks like this:

PHYSICAL:
    Athletics: 0
    Brawl: 3
MENTAL:
    Academics: 2
    Computers

My initial thought was to use a Multimap of some sort, and have the skill type as an Enum, and key to my map. Each Skill is an element in the collection that backs the multimap.

However, I've been struggling to get the yaml to work. On explaining this to a colleague outside of work they said this was probably a sign of code smell, and he's never seen it used 'well'.

Are multiMaps really code smell? If so what alternate data structures would suit my goals?

Best Answer

A couple of notes:

  • Never deserialize untrusted YAML. In most YAML parsers this allows arbitrary code execution. In SnakeYAML, you can prevent this, but you need to be very careful, and the risk seems unnecessary.

    Since you just need a key-value store I would recommend JSON instead, which brings me to:

  • Use strings as keys, and convert from strings to enums after loading (You can do this with a switch statement or by reflection.). This avoids the need for calling Java constructors from the YAML parser, and thus eliminates the security vulnerability. The conversion can easily be made where needed, and hopefully will not cause much of a slowdown.

  • Related Topic