patterncsharpModerate
Function for determining triangle type
Viewed 0 times
determiningfunctiontypetrianglefor
Problem
A while back I was asked to write some sample code for a job that I was applying for. They wanted a class that had a function that would accept three lengths and return the appropriate type of triangle (Scalene, Isosceles or Equilateral) based on that. They also wanted unit tests.
After sending this I never heard back, so I'm wondering if anyone would have any suggestions for a better way to implement this.
```
using NUnit.Framework;
namespace Triangle
{
/****
* This test is divided into two halves.
* 1. Implement the GetTriangleType method so that it returns the appropriate
* enum value for different inputs.
* 2. Write a complete series of passing unit tests for the GetTriangleType method
* under various input conditions. In this example we're using NUnit, but
* you can use whatever testing framework you prefer.
*
* When finished, send back your solution (your version of TriangleTester.cs
* will suffice) including any explanatory comments you feel are needed.
*/
public enum TriangleType
{
Scalene = 1, // no two sides are the same length
Isosceles = 2, // two sides are the same length and one differs
Equilateral = 3, // all sides are the same length
Error = 4 // inputs can't produce a triangle
}
public class TriangleTester
{
///
/// Given the side lengths a, b, and c, determine and return
/// what type of triangle the lengths describe, or whether
/// the input is invalid
///
/// length of side a
/// length of side b
/// length of side c
/// The triangle type based on the number of matching sides passed in.
public static TriangleType GetTriangleType(int a, int b, int c)
{
//Placing items in an array for processing
int[] values = new int[3] {a, b, c};
// keeping this as the first check in case someone passes
After sending this I never heard back, so I'm wondering if anyone would have any suggestions for a better way to implement this.
```
using NUnit.Framework;
namespace Triangle
{
/****
* This test is divided into two halves.
* 1. Implement the GetTriangleType method so that it returns the appropriate
* enum value for different inputs.
* 2. Write a complete series of passing unit tests for the GetTriangleType method
* under various input conditions. In this example we're using NUnit, but
* you can use whatever testing framework you prefer.
*
* When finished, send back your solution (your version of TriangleTester.cs
* will suffice) including any explanatory comments you feel are needed.
*/
public enum TriangleType
{
Scalene = 1, // no two sides are the same length
Isosceles = 2, // two sides are the same length and one differs
Equilateral = 3, // all sides are the same length
Error = 4 // inputs can't produce a triangle
}
public class TriangleTester
{
///
/// Given the side lengths a, b, and c, determine and return
/// what type of triangle the lengths describe, or whether
/// the input is invalid
///
/// length of side a
/// length of side b
/// length of side c
/// The triangle type based on the number of matching sides passed in.
public static TriangleType GetTriangleType(int a, int b, int c)
{
//Placing items in an array for processing
int[] values = new int[3] {a, b, c};
// keeping this as the first check in case someone passes
Solution
I don't code C#, so just some generic notes:
-
You can omit the
-
Some comments are just says what's in the code. I'd remove them, they're just noise.
-
The code doesn't check that the the sum of the lengths of any two sides of the triangle have to be greater than the length of the third side. (
-
About the specification: in case of an error you might want to throw an exception with a detailed error message instead of the
-
Just a link for @Jeff's point: Too many assert in one test is a bad smell. If the first
-
You can omit the
else keyword if you return immediately:if (a <= 0 || b <= 0 || c <= 0)
{
return TriangleType.Error;
}
if (values.Distinct().Count() == 1) //There is only one distinct value in the set, therefore all sides are of equal length
{
return TriangleType.Equilateral;
}
...-
Some comments are just says what's in the code. I'd remove them, they're just noise.
-
The code doesn't check that the the sum of the lengths of any two sides of the triangle have to be greater than the length of the third side. (
a + b > c)-
About the specification: in case of an error you might want to throw an exception with a detailed error message instead of the
Error enum which tells nothing about the cause of the error to the clients.-
Just a link for @Jeff's point: Too many assert in one test is a bad smell. If the first
AreEqual throws an exception you won't know anything about the results of the other assert calls which could be important because they could help debugging and defect localization.Code Snippets
if (a <= 0 || b <= 0 || c <= 0)
{
return TriangleType.Error;
}
if (values.Distinct().Count() == 1) //There is only one distinct value in the set, therefore all sides are of equal length
{
return TriangleType.Equilateral;
}
...Context
StackExchange Code Review Q#18257, answer score: 12
Revisions (0)
No revisions yet.