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 247317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:14:57+00:00 2026-05-11T21:14:57+00:00

Yes, I know, yet another question about mutable objects. See this for general background

  • 0

Yes, I know, yet another question about mutable objects. See this for general background and this for the closest analogue to my question. (though it has some C++ specific overtones that don’t apply here)

Let’s assume that the following pseudo code represents the best interface design. That is, it’s the clearest expression of the business semantics (as they stand today) into OO type. Naturally, the UglyData and the things we’re tasked to do with it are subject to incremental change.

public class FriendlyWrapper
{
    public FriendlyWrapper(UglyDatum u)
    {
        Foo = u.asdf[0].f[0].o.o;
        Bar = u.barbarbar.ToDooDad();
        Baz = u.uglyNameForBaz;
        // etc
    }

    public Widget Foo { get; private set; }
    public DooDad Bar { get; private set; }
    public DooDad Baz { get; private set; }
    // etc
    public WhizBang Expensive1 { get; private set; }
    public WhizBang Expensive2 { get; private set; }

    public void Calculate()
    {
        Expensive1 = Calc(Foo, Bar);
        Expensive2 = Calc(Foo, Baz);
    }

    private WhizBang Calc(Widget a, DooDad b) { /* stuff */ }

    public override void ToString()
    {
        return string.Format("{0}{1}{2}{3}{4}", Foo, Bar, Baz, Expensive1 ?? "", Expensive2 ?? "");                             
    }
}

// Consumer 1 is happy to work with just the basic wrapped properties
public string Summarize()
{
    var myStuff = from u in data
                  where IsWhatIWant(u)
                  select new FriendlyWrapper(u);

    var sb = new StringBuilder();
    foreach (var s in myStuff)
    {
        sb.AppendLine(s.ToString());
    }
    return sb.ToString();
}

// Consumer 2's job is to take the performance hit up front.  His callers might do things 
// with expensive properties (eg bind one to a UI element) that should not take noticeable time. 
public IEnumerable<FriendlyWrapper> FetchAllData(Predicate<UglyDatum> pred)
{
    var myStuff = from u in data
                  where pred(u)
                  select new FriendlyWrapper(u);

    foreach (var s in myStuff)
    {
        s.Calculate();  // as written, this doesn't do what you intend...
    }

    return myStuff;
}

What’s the best route here? Options I can see:

  1. Mutable object with an explicit Calculate() method, as above
  2. Mutable object where expensive calculations are done in the getters (and probably cached)
  3. Split into two objects where one inherits (or perhaps composes?) from the other
  4. Some sort of static + locking mechanism, as in the C++ question linked above

I’m leaning toward #2 myself. But every route has potential pitfalls.

If you choose #1 or #2, then how would you implement Consumer2’s loop over mutables in a clear, correct manner?

If you choose #1 or #3, how would you handle future situations where you only want to calculate some properties but not others? Willing to create N helper methods / derived classes?

If you choose #4, I think you’re crazy, but feel free to explain

  • 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-11T21:14:57+00:00Added an answer on May 11, 2026 at 9:14 pm

    In your case, since you’re using LINQ, you’re only going to constructing these objects in cases where you want the calculation.

    If that is your standard usage pattern, I would just put the expensive calculation directly in the constructor. Using lazy initialization is always slower unless you plan to have some cases where you do not calculate. Doing the calculation in the getters will not save anything (at least in this specific case).

    As for mutability – mutable objects with reference syntax and identity (ie: classes in C#) are really okay – it’s more a problem when you’re dealing with mutable value types (ie: structs). There are many, many mutable classes in the .NET BCL – and they don’t cause issues. The problem is typically more of one when you start dealing with value types. Mutable value types lead to very unexpected behavior.

    In general, I’d turn this question upside down – How and where are you going to use this object? How can you make this object the most performant (if it’s been determined to be problematic) without affecting usability? Your 1), 3) and 4) options would all make usability suffer, so I’d avoid them. In this case, doing 2) won’t help. I’d just put it in the constructor, so your object’s always in a valid state (which is very good for usability and maintainability).

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

Sidebar

Ask A Question

Stats

  • Questions 221k
  • Answers 221k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer My suggestion is to create a Singleton DataAccess class, instantiate… May 13, 2026 at 12:15 am
  • Editorial Team
    Editorial Team added an answer It's tricky, because the way two digit years work with… May 13, 2026 at 12:15 am
  • Editorial Team
    Editorial Team added an answer If you want to stay with activerecord you can use… May 13, 2026 at 12:15 am

Related Questions

Before I begin: I have spent a long time on many forums (including Stack
Iam trying to create an iterative server based on datagram sockets (UDP). It calls
I am trying to derive the type of an object at runtime. Specifically I
Currently, I have a JList listen to list selection listener. private void jList1ValueChanged(javax.swing.event.ListSelectionEvent evt)

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.