mercredi 16 octobre 2019

Cleaner code resulting in data duplication and unnecessary operations?

The overall dilemma is that "good" / "clean" method design seems to be forcing me to make a extra copies of things, so I am wondering whether I am doing something wrong and how to improve this.

Say I am working with a bunch of objects, and each has a string property called "Id".

I want to do a bulk query for something about these objects, and all that I need to do the query is the ID.

So I design an interface like:

IList<Data> GetDataForObjects(IList<string> Ids)

But then I have to make a copy of the string ids of my objects to pass to this method, like:

objects.Select(o => o.Id).ToList()

I could instead have my method use the object list:

IList<Data> GetDataForObjects(IList<ObjectWithId> objects)

But then it is requiring information that is not really necessary which seems like a code smell and a hindrance to re-usability.

Any advice for dealing with issues like this?

Would the right call be to just make the method that is most useful for the current code I have (list of objects), and add a sort of duplicate method (list of string ids) or refactor the original method later if necessary?

Would using IEnumerable as the argument and passing the select statement without the ToList improve things?

Aucun commentaire:

Enregistrer un commentaire