Skip to main content
sp
Source Link
Peter Taylor
  • 24.5k
  • 1
  • 49
  • 94

Input handling

The derivative and the initial values have separate methods to prompt and to take input; the target and step size don't.

None of the methods have error handling if the value given is invalid. You should expect the users to mess things up.

I would create a combined fix for the two along the lines of

T PromptForInput<T>(string prompt, Func<string, T> parse)
{
    Console.WriteLine(prompt);
    while (true)
    {
        try
        {
            return parse(Console.ReadLine());
        }
        catch
        {
            Console.WriteLine("Invalid input");
        }
    }
}

Then the input phase becomes

    string derivativeinput = PromptForInput("Dy/Dx=", s => s);
    int[] xyvals = PromptForInput("Initial value: X,Y", parsePair);
    int stepto = PromptForInput("Estimate what value of x?", int.Parse);
    int stepby = PromptForInput("Step by=?", int.Parse);

    Parser.Load();
    LoadInitVals(xyvals[0], xyvals[1]);

with an auxiliary method parsePair which can check that it gets two values and parse them directly.

(Alternatively, you could use one of the many Point / Point2D structs in the standard library which has a Parse method).

Fractional step count

The lack of comments and input validation means that I'm not sure what the intended behaviour is if I supply the following input:

x
0,0
3
2

Should that be an error because 3 - 0 is not a multiple of 2? If not, what should it do? At present I think it outputs the value extrapolated for x = 2, without any warning that it's not the value for x = 3, and I think that's a bug.

Types

I'm surprised by the heavy use of int. I would have expected double.

L10n

Note that if you switch to using double then you need to be wary when parsing the X,Y input because some cultures use , as the decimal point.

Library

The YAMP changelog lists "Changed static Parser to instance model" two versions ago, so you seem to have an outdated version of the library. For that change alongalone I would think it's worth updating.

Input handling

The derivative and the initial values have separate methods to prompt and to take input; the target and step size don't.

None of the methods have error handling if the value given is invalid. You should expect the users to mess things up.

I would create a combined fix for the two along the lines of

T PromptForInput<T>(string prompt, Func<string, T> parse)
{
    Console.WriteLine(prompt);
    while (true)
    {
        try
        {
            return parse(Console.ReadLine());
        }
        catch
        {
            Console.WriteLine("Invalid input");
        }
    }
}

Then the input phase becomes

    string derivativeinput = PromptForInput("Dy/Dx=", s => s);
    int[] xyvals = PromptForInput("Initial value: X,Y", parsePair);
    int stepto = PromptForInput("Estimate what value of x?", int.Parse);
    int stepby = PromptForInput("Step by=?", int.Parse);

    Parser.Load();
    LoadInitVals(xyvals[0], xyvals[1]);

with an auxiliary method parsePair which can check that it gets two values and parse them directly.

(Alternatively, you could use one of the many Point / Point2D structs in the standard library which has a Parse method).

Fractional step count

The lack of comments and input validation means that I'm not sure what the intended behaviour is if I supply the following input:

x
0,0
3
2

Should that be an error because 3 - 0 is not a multiple of 2? If not, what should it do? At present I think it outputs the value extrapolated for x = 2, without any warning that it's not the value for x = 3, and I think that's a bug.

Types

I'm surprised by the heavy use of int. I would have expected double.

L10n

Note that if you switch to using double then you need to be wary when parsing the X,Y input because some cultures use , as the decimal point.

Library

The YAMP changelog lists "Changed static Parser to instance model" two versions ago, so you seem to have an outdated version of the library. For that change along I would think it's worth updating.

Input handling

The derivative and the initial values have separate methods to prompt and to take input; the target and step size don't.

None of the methods have error handling if the value given is invalid. You should expect the users to mess things up.

I would create a combined fix for the two along the lines of

T PromptForInput<T>(string prompt, Func<string, T> parse)
{
    Console.WriteLine(prompt);
    while (true)
    {
        try
        {
            return parse(Console.ReadLine());
        }
        catch
        {
            Console.WriteLine("Invalid input");
        }
    }
}

Then the input phase becomes

    string derivativeinput = PromptForInput("Dy/Dx=", s => s);
    int[] xyvals = PromptForInput("Initial value: X,Y", parsePair);
    int stepto = PromptForInput("Estimate what value of x?", int.Parse);
    int stepby = PromptForInput("Step by=?", int.Parse);

    Parser.Load();
    LoadInitVals(xyvals[0], xyvals[1]);

with an auxiliary method parsePair which can check that it gets two values and parse them directly.

(Alternatively, you could use one of the many Point / Point2D structs in the standard library which has a Parse method).

Fractional step count

The lack of comments and input validation means that I'm not sure what the intended behaviour is if I supply the following input:

x
0,0
3
2

Should that be an error because 3 - 0 is not a multiple of 2? If not, what should it do? At present I think it outputs the value extrapolated for x = 2, without any warning that it's not the value for x = 3, and I think that's a bug.

Types

I'm surprised by the heavy use of int. I would have expected double.

L10n

Note that if you switch to using double then you need to be wary when parsing the X,Y input because some cultures use , as the decimal point.

Library

The YAMP changelog lists "Changed static Parser to instance model" two versions ago, so you seem to have an outdated version of the library. For that change alone I would think it's worth updating.

Source Link
Peter Taylor
  • 24.5k
  • 1
  • 49
  • 94

Input handling

The derivative and the initial values have separate methods to prompt and to take input; the target and step size don't.

None of the methods have error handling if the value given is invalid. You should expect the users to mess things up.

I would create a combined fix for the two along the lines of

T PromptForInput<T>(string prompt, Func<string, T> parse)
{
    Console.WriteLine(prompt);
    while (true)
    {
        try
        {
            return parse(Console.ReadLine());
        }
        catch
        {
            Console.WriteLine("Invalid input");
        }
    }
}

Then the input phase becomes

    string derivativeinput = PromptForInput("Dy/Dx=", s => s);
    int[] xyvals = PromptForInput("Initial value: X,Y", parsePair);
    int stepto = PromptForInput("Estimate what value of x?", int.Parse);
    int stepby = PromptForInput("Step by=?", int.Parse);

    Parser.Load();
    LoadInitVals(xyvals[0], xyvals[1]);

with an auxiliary method parsePair which can check that it gets two values and parse them directly.

(Alternatively, you could use one of the many Point / Point2D structs in the standard library which has a Parse method).

Fractional step count

The lack of comments and input validation means that I'm not sure what the intended behaviour is if I supply the following input:

x
0,0
3
2

Should that be an error because 3 - 0 is not a multiple of 2? If not, what should it do? At present I think it outputs the value extrapolated for x = 2, without any warning that it's not the value for x = 3, and I think that's a bug.

Types

I'm surprised by the heavy use of int. I would have expected double.

L10n

Note that if you switch to using double then you need to be wary when parsing the X,Y input because some cultures use , as the decimal point.

Library

The YAMP changelog lists "Changed static Parser to instance model" two versions ago, so you seem to have an outdated version of the library. For that change along I would think it's worth updating.