4
\$\begingroup\$

This program calculates wood length and area of glass required for the window. I am a beginner in C# and I want to know how professional programmers will write program like this.

        double width, height, woodLength, glassArea;
        const double MAX_WIDTH = 5.0;
        const double MIN_WIDTH = 0.5;
        const double MAX_HEIGHT = 3.0;
        const double MIN_Height = 0.75;
        string widthString, heightString;
        Console.WriteLine("please enter width of the window");
       widthString = Console.ReadLine();
        width = Convert.ToDouble(widthString);
        if (width < MIN_WIDTH)
        {
            Console.WriteLine("you enter vaule less than min width \n"+ "using mininium one");
            width = MIN_WIDTH;
        }
        if (width>MAX_WIDTH)
        {
            Console.WriteLine("you entered value grater than MaX width\n" + "using maxuimum one");

            width = MAX_WIDTH;
        }
        Console.WriteLine("please enter your height of the window");
        heightString = Console.ReadLine();
        height = double.Parse(heightString);
        if (height<MIN_Height)
        {
            Console.WriteLine("you enter vaule less than min width \n" + "using mininium one");
            height = MIN_Height;

        }
        if (height > MAX_HEIGHT) 
        {
            Console.WriteLine("you enter value grater than Max height\n" + "using maxuimum one");
            height = MAX_HEIGHT;
        }
        woodLength = 2 * (width+height);
        glassArea =  (width * height);
        Console.WriteLine("the lenth of the wood is "+woodLength+"feet");
        Console.WriteLine("the area of the glass is " + glassArea +"m^2");
\$\endgroup\$
  • \$\begingroup\$ Why are you multiplying the required length by 3.25 and the area by 2? Are those bugs, or am I missing some context? \$\endgroup\$ – forsvarir Dec 10 '16 at 9:48
  • \$\begingroup\$ I multiply by 3.25 to get length in feet and by two i mean two panes of glass \$\endgroup\$ – Abdelrhman Fawzy Dec 10 '16 at 13:18
  • \$\begingroup\$ Looks like you took out the 3.25 but it still says m^2. Need to test is the numbers parse. \$\endgroup\$ – paparazzo Dec 10 '16 at 18:11
5
\$\begingroup\$

Typos / Spelling issue

Attention to detail is important. Spelling errors, in variable names + strings should be avoided. They give a bad impression. You have several. mininium should be minimum, maxuimum should be maximum, Sentences should start with a capital letter etc.

Be clear with the user

You ask the user for the width/height of the window, however you don't tell the user what unit the measurements are in. Your original code you were converting to feet (by multiplying by 3.25). If you're going to do something like this, consider defining a constant with a name that reflects what it is, FeetPerMetre for example (Constants should be in Pascal, not ALL_CAPS naming). You also haven't told the user the min/max values either when you prompt for input, or when you decide to use them.

Contain your functionality

The code you've posted is all in a single block. It doesn't appear to have been wrapped in a function, suggesting that it's just sitting in a static main somewhere. Think about what elements the code contains and break it out into functions that you might be able to reuse. Some possible functions might be:

  • int GetLengthFromUser(string prompt, double minLength, double maxLength)
  • double CalculateWindowFrameLength(double width, double length)
  • double CalculateWindowPaneArea(double width, double length)
  • double ConvertFeetToMetres(double length)

Interpolated Strings

Depending on the C# version you're using, you may be able to use interpolated strings which can things like WriteLine statements look a bit cleaner:

Console.WriteLine($"The lenth of the wood is {woodLength} feet");

double.Parse / Convert.ToDouble

Both methods can fail and throw an exception if the user enters something other than a number (which will inevitably happen eventually). Consider using TryParse and re-prompting the user when it fails.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.