Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1021197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:16:28+00:00 2026-05-16T11:16:28+00:00

I have this method called LoadToDictionary. It accepts a dictionary and a filepath. Currently,

  • 0

I have this method called LoadToDictionary. It accepts a dictionary and a filepath. Currently, it would look something like this:
void LoadToDictionary(Dictionary<string, string> dict, string filePath. Then inside, it would have a lot of code to extract the contents of the file in filePath to a dictionary. I want to make it more general, so it could, say, take a dictionary with int’s instead, and the ability to change the parsing code in the method. Should I mark this method and later override it? Is there any other way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T11:16:29+00:00Added an answer on May 16, 2026 at 11:16 am

    Premature generality gives premature optimization a run for the money as the root of evil. Generality is expensive and expensive code should be justified by clear benefits.

    I would therefore drive increased generality into your method on the basis of specific scenarios. I can think of many ways to make your method more general:

    1. Don’t take a dictionary of string to string; take a dictionary of, say, string to arbitrary type.

    2. Don’t take a Dictionary<...>; take an IDictionary<...>.

    3. Don’t take any kind of dictionary. Take an Action<K, V> whose action could be to enter the item in a dictionary.

    4. Don’t take a filename. You’re just going to turn the file name into a stream anyway, so take a Stream to begin with. Have the caller open the stream for you.

    5. Don’t take a Stream. You’re just going to turn the stream into a sequence of items anyway, so take an IEnumerable<...> and have the caller parse the stream for you.

    How general can we make this? Let’s suppose we have a sequence of T which is then transformed into a map from K to V. What do we need? A sequence, a key extractor, a value extractor, and a map writer:

    static void MakeMap<T, K, V>(this IEnumerable<T> sequence, Action<K, V> mapWriter, Func<T, K> keyExtractor, Func<T, V> valueExtractor)
    {
        foreach(var item in sequence)
            mapWriter(keyExtractor(item), valueExtractor(item));
    }
    

    Notice how the code gets really short when it gets really general. That is pretty freakin’ general, and as a result the call site will now look like an unholy mess. Is that really what you want? Are you going to use all that generality effectively? Probably not. What are the scenarios you actually have that are driving a desire to be more general? Use those specific scenarios to motivate your refactoring.

    Is that as far as we can go? Could we get even more general than this? Sure. We could notice for example that it seems bad to have a side effect in there. Shouldn’t the method just be returning a newly constructed dictionary? What should the comparison policy of that dictionary be? What if there is more than one item that has the same key? Should it be replaced, or should we have the dictionary actually be a map from keys to a list of values? We could take these ideas and make a new method that solves all those problems:

    public static ILookup<TKey, TElement> ToLookup<TSource, TKey, TElement>(
    this IEnumerable<TSource> source,
    Func<TSource, TKey> keySelector,
    Func<TSource, TElement> elementSelector,
    IEqualityComparer<TKey> comparer) { ... }
    

    And hey, we’ve just reinvented the ToLookup() extension method. Sometimes when you make a method general enough you discover that it already exists.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have a simple method called Invert : public static void Invert(this bool
I have a class with a method called DataIn(int InputID, string CSVValue) , this
I have a model which has a method called 'has_voted'. It looks like this...
I have this method which gets called from a different class: -(void)artworkInfo:(NSNumber *)pos{ [image
My data access classes often have a method called loadDataToEntity or something similar. This
I have this method that gets called when i press a button and removes
So I have a method in a reservation model called add_equip. This method does
Trying to use GridBagLayout. I have the method called buildLabel. This creates three label.
I have a Jtable on which I called the method table1.setAutoCreateRowSorter(true); . So this
I have a Method name update, this will be called depending on the boolean

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.