Skip to main content
added 7 characters in body
Source Link
jpmc26
  • 1.2k
  • 8
  • 14

Meaning that I have to put half of my logic concerning the lazy property in the constructor, and having more boilerplate code.

This is a little speculative, but I think you have an XY problem. You're trying to reduce boilerplate, but there are probably better ways to do that than what you've suggested.

If I understand correctly, your problem is that your classes look something like this:

public class MyClass
{
    private Lazy<string> _MyStringValue;
    // ...

    public MyClass()
    {
        this._MyStringValue = new Lazy<string>(() => {
            var builder = new StringBuilder();
            builder.Append("a");
            // 50 more lines of expensive construction
            return builder.ToString();
        });

        // 100 more lines constructing OTHER lazy stuff
    }
}

Gloss over the details of building up the value; it's just an example. The important point is that you have all this logic here deep in your constructor.

I think there are two things you can do to alleviate this problem:

  1. Parameterize

    Why put all this logic in the constructor? You're losing a lot of reusablity by doing that anyway. So make these things parameters and construct them elsewhere:

    public class MyClass
    {
        private Lazy<string> _MyStringValue;
        // ...
    
        public MyClass(Lazy<string> myStringValue)
        {
            this._MyStringValue = myStringValue;
        }
    }
    
  2. You can embed this construction logic in a method, and then pass the method to the Lazy constructor:

    class MyStringValueMaker
    {
        // Could be an instance method if that's more appropriate.
        // This is just for example
        public static string MakeValue()
        {
            var builder = new StringBuilder();
            builder.Append("a");
            // 50 more lines of expensive construction
            return builder.ToString();
        }
    }
    

    And then elsewhere:

    var myClass = new MyClass(new Lazy<string>(MyStringValueMaker.MakeValue));
    

Now suddenly everything is much better organized, more reusable, and simpler to understand.

If that's not what your class originally looked like, well, then I think you'd be better off posting a new question asking for a review on the original class to get ideas about how to improve.

Meaning that I have to put half of my logic concerning the lazy property in the constructor, and having more boilerplate code.

This is a little speculative, but I think you have an XY problem. You're trying to reduce boilerplate, but there are probably better ways to do that than what you've suggested.

If I understand correctly, your problem is that your classes look something like this:

public class MyClass
{
    private Lazy<string> _MyStringValue;
    // ...

    public MyClass()
    {
        this._MyStringValue = new Lazy<string>(() => {
            var builder = new StringBuilder();
            builder.Append("a");
            // 50 more lines of expensive construction
            return builder.ToString();
        });

        // 100 more lines constructing OTHER lazy stuff
    }
}

Gloss over the details of building up the value; it's just an example. The important point is that you have all this logic here deep in your constructor.

I think there are two things you can do to alleviate this problem:

  1. Parameterize

    Why put all this logic in the constructor? You're losing a lot of reusablity by doing that anyway. So make these things parameters and construct them elsewhere:

    public class MyClass
    {
        private Lazy<string> _MyStringValue;
        // ...
    
        public MyClass(Lazy<string> myStringValue)
        {
            this._MyStringValue = myStringValue;
        }
    }
    
  2. You can embed this construction logic in a method, and then pass the method to the Lazy constructor:

    class MyStringValueMaker
    {
        // Could be an instance method if that's more appropriate.
        // This is just for example
        public static MakeValue()
        {
            var builder = new StringBuilder();
            builder.Append("a");
            // 50 more lines of expensive construction
            return builder.ToString();
        }
    }
    

    And then elsewhere:

    var myClass = new MyClass(new Lazy<string>(MyStringValueMaker.MakeValue));
    

Now suddenly everything is much better organized, more reusable, and simpler to understand.

If that's not what your class originally looked like, well, then I think you'd be better off posting a new question asking for a review on the original class to get ideas about how to improve.

Meaning that I have to put half of my logic concerning the lazy property in the constructor, and having more boilerplate code.

This is a little speculative, but I think you have an XY problem. You're trying to reduce boilerplate, but there are probably better ways to do that than what you've suggested.

If I understand correctly, your problem is that your classes look something like this:

public class MyClass
{
    private Lazy<string> _MyStringValue;
    // ...

    public MyClass()
    {
        this._MyStringValue = new Lazy<string>(() => {
            var builder = new StringBuilder();
            builder.Append("a");
            // 50 more lines of expensive construction
            return builder.ToString();
        });

        // 100 more lines constructing OTHER lazy stuff
    }
}

Gloss over the details of building up the value; it's just an example. The important point is that you have all this logic here deep in your constructor.

I think there are two things you can do to alleviate this problem:

  1. Parameterize

    Why put all this logic in the constructor? You're losing a lot of reusablity by doing that anyway. So make these things parameters and construct them elsewhere:

    public class MyClass
    {
        private Lazy<string> _MyStringValue;
        // ...
    
        public MyClass(Lazy<string> myStringValue)
        {
            this._MyStringValue = myStringValue;
        }
    }
    
  2. You can embed this construction logic in a method, and then pass the method to the Lazy constructor:

    class MyStringValueMaker
    {
        // Could be an instance method if that's more appropriate.
        // This is just for example
        public static string MakeValue()
        {
            var builder = new StringBuilder();
            builder.Append("a");
            // 50 more lines of expensive construction
            return builder.ToString();
        }
    }
    

    And then elsewhere:

    var myClass = new MyClass(new Lazy<string>(MyStringValueMaker.MakeValue));
    

Now suddenly everything is much better organized, more reusable, and simpler to understand.

If that's not what your class originally looked like, well, then I think you'd be better off posting a new question asking for a review on the original class to get ideas about how to improve.

Source Link
jpmc26
  • 1.2k
  • 8
  • 14

Meaning that I have to put half of my logic concerning the lazy property in the constructor, and having more boilerplate code.

This is a little speculative, but I think you have an XY problem. You're trying to reduce boilerplate, but there are probably better ways to do that than what you've suggested.

If I understand correctly, your problem is that your classes look something like this:

public class MyClass
{
    private Lazy<string> _MyStringValue;
    // ...

    public MyClass()
    {
        this._MyStringValue = new Lazy<string>(() => {
            var builder = new StringBuilder();
            builder.Append("a");
            // 50 more lines of expensive construction
            return builder.ToString();
        });

        // 100 more lines constructing OTHER lazy stuff
    }
}

Gloss over the details of building up the value; it's just an example. The important point is that you have all this logic here deep in your constructor.

I think there are two things you can do to alleviate this problem:

  1. Parameterize

    Why put all this logic in the constructor? You're losing a lot of reusablity by doing that anyway. So make these things parameters and construct them elsewhere:

    public class MyClass
    {
        private Lazy<string> _MyStringValue;
        // ...
    
        public MyClass(Lazy<string> myStringValue)
        {
            this._MyStringValue = myStringValue;
        }
    }
    
  2. You can embed this construction logic in a method, and then pass the method to the Lazy constructor:

    class MyStringValueMaker
    {
        // Could be an instance method if that's more appropriate.
        // This is just for example
        public static MakeValue()
        {
            var builder = new StringBuilder();
            builder.Append("a");
            // 50 more lines of expensive construction
            return builder.ToString();
        }
    }
    

    And then elsewhere:

    var myClass = new MyClass(new Lazy<string>(MyStringValueMaker.MakeValue));
    

Now suddenly everything is much better organized, more reusable, and simpler to understand.

If that's not what your class originally looked like, well, then I think you'd be better off posting a new question asking for a review on the original class to get ideas about how to improve.