views:

226

answers:

4

I don't want to replicate code across overloaded constructors as below. How do I refactor it?

/// <summary>
/// Represents a pseudo-random number generator, a device that
/// produces a sequence of numbers that are normally distributed.
/// </summary>
public class NormalDeviate : UniformDeviate
{
    double mean;
    double standardDeviation;
    double storedValue = 0d;

    /// <summary>
    /// Initializes a new instance of the <see cref="NormalDeviate"/> class.
    /// </summary>
    /// <param name="mean">The mean.</param>
    /// <param name="standardDeviation">The standard deviation.</param>
    /// <param name="seed">The seed.</param>
    public NormalDeviate(double mean, double standardDeviation, ulong seed)
        : base(seed)
    {
        this.mean = mean;
        this.standardDeviation = standardDeviation;
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="NormalDeviate"/> class.
    /// </summary>
    /// <param name="mean">The mean.</param>
    /// <param name="standardDeviation">The standard deviation.</param>
    public NormalDeviate(double mean, double standardDeviation)
        : base()
    {
        this.mean = mean;
        this.standardDeviation = standardDeviation;
    }
}
A: 

How about this?

public class NormalDeviate : UniformDeviate
{
    double mean;
    double standardDeviation;
    double storedValue;

    /// <summary>
    /// Initializes a new instance of the <see cref="NormalDeviate"/> class.
    /// </summary>
    /// <param name="mean">The mean.</param>
    /// <param name="standardDeviation">The standard deviation. The distance from the mean where 78.2% of the samples lie.</param>
    /// <param name="seed">The seed.</param>
    public NormalDeviate(double mean, double standardDeviation, ulong seed) : base(seed)
    {
        this.mean = mean;
        this.standardDeviation = standardDeviation;
        this.storedValue = 0d;
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="NormalDeviate"/> class.
    /// </summary>
    /// <param name="mean">The mean.</param>
    /// <param name="standardDeviation">The standard deviation. The distance from the mean where 78.2% of the samples lie.</param>
    public NormalDeviate(double mean, double standardDeviation)
        : this(mean, standardDeviation, 0)
    {

    }
}

EDIT: Fixed code

SolutionYogi
Your example provides two of the same three-parameter constructor and misses the two-parameter constructor.
jasonh
My bad. Fixed the code. Thanks for pointing out my mistake.
SolutionYogi
Your example sends the 0 parameter to the base constructor, which is not exactly the same as calling it with 0 parameters.
Jader Dias
I agree, but wouldn't 'seed' being 'ulong' be zero if you didn't supply it?
SolutionYogi
See my discussion with Steven above for the answer.
Jader Dias
+1  A: 

Look at this link about constructor chaining.

Robert
+1  A: 
public ClassName(double arg1, double arg2, double arg3) : base(arg3) 
{
    _member1 = arg1;
    _member2 = arg2;
}
public ClassName(double arg1, double arg2) : this(arg1,arg2,0) { }
public ClassName(double arg1) : this(arg1,0,0) { }
Steven A. Lowe
It's incorrect. The hard-coded zero will reach the base constructor.
Jader Dias
@[Jader Dias]: it's just an example. use NaN or whatever default is appropriate for the unspecified values
Steven A. Lowe
@[Jader Dias]: in your example, presumably the ulong seed value defaults to zero if not specified ;-)
Steven A. Lowe
@Steven the default is ((ulong)Environment.TickCount), but that's another code I don't want to replicate.
Jader Dias
@[Jader Dias]: in that case, you could use an initialization method with nullable arguments, or check the seed value for zero before using it and use tickcount instead
Steven A. Lowe
+2  A: 

Given that you can't feed the base constructor a 0 seed when no seed is provided, this would be what you want:

/// <summary>
/// Represents a pseudo-random number generator, a device that
/// produces a sequence of numbers that are normally distributed.
/// </summary>
public class NormalDeviate : UniformDeviate
{
    double mean;
    double standardDeviation;
    double storedValue = 0d;

    /// <summary>
    /// Initializes a new instance of the <see cref="NormalDeviate"/> class.
    /// </summary>
    /// <param name="mean">The mean.</param>
    /// <param name="standardDeviation">The standard deviation.</param>
    /// <param name="seed">The seed.</param>
    public NormalDeviate(double mean, double standardDeviation, ulong seed)
        : base(seed)
    {
        CommonInitialization(mean, standardDeviation);
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="NormalDeviate"/> class.
    /// </summary>
    /// <param name="mean">The mean.</param>
    /// <param name="standardDeviation">The standard deviation.</param>
    public NormalDeviate(double mean, double standardDeviation)
        : base()
    {
        CommonInitialization(mean, standardDeviation);
    }

    private void CommonInitialization(double mean, double standardDeviation)
    {
        this.mean = mean;
        this.standardDeviation = standardDeviation;
    }
}
jasonh