Skip to main content
read at end as suggested by VisualMelon
Source Link
dfhwze
  • 13.9k
  • 3
  • 38
  • 101

As you suggested, you have a lot of redundant code. And rightfully so you want to adhere to the DRY principle.

All these input methods have the same pattern internally..

static void Main(string[] args)
{
    GetWeightInput();
    GetHeightInput();
    GetWidthInput();
    GetDepthInput();

    Console.ReadLine();
}

Road to DRY

..ideally you would like to be able to call them like this:

static void Main(string[] args)
{
    var weight = AskInteger("Enter weight of parcel: ");
    var height = AskInteger("Enter height of parcel: ");
    var width = AskInteger("Enter width of parcel: ");
    var depth = AskInteger("Enter depth of parcel: ");

    Console.WriteLine("press any key to terminate the application..");
    Console.ReadLineReadKey(true);
}

Advanced road to DRY

Or if you would provide a complex object Request with 4 properties and a lambda to provide both a message to the user as a setter expression to materialise the request:

static void Main(string[] args)
{
    var request = new Request();
    AskInteger(request, x => x.Weight);
    AskInteger(request, x => x.Height);
    AskInteger(request, x => x.Width);
    AskInteger(request, x => x.Depth);

    Console.WriteLine("press any key to terminate the application..");
    Console.ReadLineReadKey(true);
}

How you would implement AskInteger is a challenge I leave to you.


Misc

  • Don't pollute your code with regions that have no added value. #region User Weight Input Validation
  • Try to avoid meaningless comments _isInvalidInput = true;//reset value to true

As you suggested, you have a lot of redundant code. And rightfully so you want to adhere to the DRY principle.

All these input methods have the same pattern internally..

static void Main(string[] args)
{
    GetWeightInput();
    GetHeightInput();
    GetWidthInput();
    GetDepthInput();

    Console.ReadLine();
}

Road to DRY

..ideally you would like to be able to call them like this:

static void Main(string[] args)
{
    var weight = AskInteger("Enter weight of parcel: ");
    var height = AskInteger("Enter height of parcel: ");
    var width = AskInteger("Enter width of parcel: ");
    var depth = AskInteger("Enter depth of parcel: ");

    Console.WriteLine("press any key to terminate the application..");
    Console.ReadLine();
}

Advanced road to DRY

Or if you would provide a complex object Request with 4 properties and a lambda to provide both a message to the user as a setter expression to materialise the request:

static void Main(string[] args)
{
    var request = new Request();
    AskInteger(request, x => x.Weight);
    AskInteger(request, x => x.Height);
    AskInteger(request, x => x.Width);
    AskInteger(request, x => x.Depth);

    Console.WriteLine("press any key to terminate the application..");
    Console.ReadLine();
}

How you would implement AskInteger is a challenge I leave to you.


Misc

  • Don't pollute your code with regions that have no added value. #region User Weight Input Validation
  • Try to avoid meaningless comments _isInvalidInput = true;//reset value to true

As you suggested, you have a lot of redundant code. And rightfully so you want to adhere to the DRY principle.

All these input methods have the same pattern internally..

static void Main(string[] args)
{
    GetWeightInput();
    GetHeightInput();
    GetWidthInput();
    GetDepthInput();

    Console.ReadLine();
}

Road to DRY

..ideally you would like to be able to call them like this:

static void Main(string[] args)
{
    var weight = AskInteger("Enter weight of parcel: ");
    var height = AskInteger("Enter height of parcel: ");
    var width = AskInteger("Enter width of parcel: ");
    var depth = AskInteger("Enter depth of parcel: ");

    Console.WriteLine("press any key to terminate the application..");
    Console.ReadKey(true);
}

Advanced road to DRY

Or if you would provide a complex object Request with 4 properties and a lambda to provide both a message to the user as a setter expression to materialise the request:

static void Main(string[] args)
{
    var request = new Request();
    AskInteger(request, x => x.Weight);
    AskInteger(request, x => x.Height);
    AskInteger(request, x => x.Width);
    AskInteger(request, x => x.Depth);

    Console.WriteLine("press any key to terminate the application..");
    Console.ReadKey(true);
}

How you would implement AskInteger is a challenge I leave to you.


Misc

  • Don't pollute your code with regions that have no added value. #region User Weight Input Validation
  • Try to avoid meaningless comments _isInvalidInput = true;//reset value to true
Source Link
dfhwze
  • 13.9k
  • 3
  • 38
  • 101

As you suggested, you have a lot of redundant code. And rightfully so you want to adhere to the DRY principle.

All these input methods have the same pattern internally..

static void Main(string[] args)
{
    GetWeightInput();
    GetHeightInput();
    GetWidthInput();
    GetDepthInput();

    Console.ReadLine();
}

Road to DRY

..ideally you would like to be able to call them like this:

static void Main(string[] args)
{
    var weight = AskInteger("Enter weight of parcel: ");
    var height = AskInteger("Enter height of parcel: ");
    var width = AskInteger("Enter width of parcel: ");
    var depth = AskInteger("Enter depth of parcel: ");

    Console.WriteLine("press any key to terminate the application..");
    Console.ReadLine();
}

Advanced road to DRY

Or if you would provide a complex object Request with 4 properties and a lambda to provide both a message to the user as a setter expression to materialise the request:

static void Main(string[] args)
{
    var request = new Request();
    AskInteger(request, x => x.Weight);
    AskInteger(request, x => x.Height);
    AskInteger(request, x => x.Width);
    AskInteger(request, x => x.Depth);

    Console.WriteLine("press any key to terminate the application..");
    Console.ReadLine();
}

How you would implement AskInteger is a challenge I leave to you.


Misc

  • Don't pollute your code with regions that have no added value. #region User Weight Input Validation
  • Try to avoid meaningless comments _isInvalidInput = true;//reset value to true