/ShapeRefactoring

A long refactoring exercise

Primary LanguageC#MIT LicenseMIT

Shape Refactoring Exercise

A Refactoring Chew Toy

Table Of Contents

Note on Refactoring

If you have to change a test to make a change work, you are not refactoring. You are instead rearchitecting. Now this code can benefit from a change to architecture but don't call it refactoring.

Introduction

This is a different kind of refactoring exercise. Most code exercises have a specific answer and are designed to be solved within a couple of hours. This one is intended to have a large number of solutions and take more then 10 hours without completion.

The idea is to be an exercise that you can continuously go back to and see new things. This is not intended for someone who is new to "test based refactoring" but for those who are familiar with the process and want to stay sharp or want to dive deeper.

Requirements

The requirements are for each exercise is in the language specific readme. Please look there to know more.

Definitions

Refactoring: Throughout this exercise the word "refactoring" means changing structure without changing external behavior.

Setup

Global Setup

You need gittey installed globally.

npm i gittey -g

Local Setup

In the directory for the exercise run:

gittey setup

Recommendations

It is suggested that you:

  1. Clone this repository
  2. Delete the .git directory
  3. Delete all the languages you are not planning to work in
  4. Set up the directory for your language according to the readme
    1. Run: gittey setup
  5. run a git init in that directory
  6. Run the tests
    1. gittey test
  7. start working

Run the tests

Thanks to Gittey running the tests looks the same. However please look at the language specific read me for how to run the tests.

Purpose of Code

The code takes an array of points and returns an objects that represents the shape and some of its properties.

Types of Shapes

  • Empty
  • Point
  • Line Segment
  • Triangle
  • Rectangle
  • Other

Basic Shapes

Empty

The empty shape is a non-shape. It is formed from an empty array of points and has no properties except being an empty shape.

Point

The point shape is formed from an array containing a single point and has an X and Y.

Line Segment

The line segment is formed from an array containing 2 points. It has both points as properties as well as length and slope.

Triangle

The triangle is formed from an array with 4 points, the first and last are the same point forming a closed shape. The first 3 points are unique. It will have three line segments, formed from the three points: side A, side B, and side C with side A being formed from the first two points and consecutively from there.

It will have three angles, angle A, angle B, and angle C. Angle A, is opposite side A, angle B is opposite side B, and angle C is opposite side C. Each angle will be formed of two line segments that form the angle and have the degrees of the angle.

A triangle will also contain the area of the triangle.

Rectangle

The rectangle is formed by an array of 5 points, the first and last are the same point forming a closed shape. The first 4 points are unique. A rectangle will contain 4 line segments, side A, side B, side C, and side D. Side A is formed from the first two points, and each side if formed consecutively from there. To be a rectangle the angle between any adjoining sides will be 90 degrees.

A rectangle will contain the area of the rectangle.

Other

The other shape is formed by an array of points that do not make any other shape. It contains the points that formed it.

Changes that need to be made

Now to make the refactoring more focused, we have two expected changes that need to happen. These are optional for the purpose of the exercise but help us have a goal in mind.

A Bug

Expected Behavior: If three points are on the same line they should be classified as a line segment between the two furthest points.

Actual Behavior: The segment is classified as 'Other' and contains all points.

New Feature

Parallelogram If given 5 points that form a parallelogram, but not a rectangle the shape returned should be a parallelogram. The fifth point should be the same as the first forming a closed shape. Each of the other 4 points are unique.

A parallelogram has 4 line segments, side A, side B, side C and side D with side A being the first line segment and all other line segments being consecutive. It has for angles angle A, angle B, angle C and angle D, with angle A being the first angle, and all other angles being consecutive.

A parallelogram should have a height, a width, a perimeter, and a volume.

Contributors ✨

Thanks goes to these wonderful people (emoji key):


EDF Renewables

💵

Chris Stead

🔧

Thomas Desmond

🤔

This project follows the all-contributors specification. Contributions of any kind welcome!

How to contribute

The guide to how to contribute is here: How To Contribute.

A Note on Tooling

When developing this exercise I used two tools I want to call out. The first is Gittey.

Gittey is a free command automation that has understanding of git. It is used to help automate workflows. I have also used it to ensure a common feel between exercises of different languages.

Book Lisp is a mark down document generator that was used to build the document you are currently reading. Book Lisp allows me to break the document into smaller manageable chunks and then compile them into bigger documents.

Both of these tools were created and maintained by Chris Stead. I give him a due thanks for the work he puts into making developer lives easier.