Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- - "Narrow Change Strategy" -> Schnittstellenumbau
- public string GetErrorMessage()
- {
- return this.errorMessage;
- }
- Narrow change: use an existing variable, to assign it something new (ie. the change we want to apply in several places),
- and then to inline that variable, so that our change takes effect everywhere where the variable was used.
- http://dwyler.net/blog/refactoring-introduce-new-class-and-move-behaviour-to-it/
- - "Feature Envy" Circle.toString()
- -
- public override bool Contains(int x, int y)
- {
- int squaredDistanceToCenter = sqrd(x, this.x) + sqrd(y, this.y);
- bool result = squaredDistanceToCenter <= Math.Sqrt(radius);
- if (result)
- {
- numberOfContainingPoints++;
- }
- return result;
- }
- --------------
- mit Point
- public override bool Contains(Point point)
- {
- int squaredDistanceToCenter = sqrd(point.X, this.x) + sqrd(point.Y, this.y);
- bool result = squaredDistanceToCenter <= Math.Sqrt(radius);
- if (result)
- {
- numberOfContainingPoints++;
- }
- return result;
- }
- private int sqrd(int a, int b)
- {
- return (a - b) * (a - b);
- }
- public int CountContainingPoints(int[] xCords, int[] yCords)
- {
- Point[] points = new Point[xCords.Length];
- for (int i = 0; i < points.Length; ++i)
- {
- points[i] = new Point(xCords[i], yCords[i]);
- }
- return CountContainingPoints(points);
- }
- public int CountContainingPoints(Point[] points)
- {
- numberOfContainingPoints = 0;
- for (int i = 0; i < points.Length; ++i)
- {
- Contains(points[i]);
- }
- return numberOfContainingPoints;
- }
- ----------------
- public override bool Contains(Point point)
- {
- bool result = point.SquaredDistanceToCenter(this.x, this.y) <= Math.Sqrt(radius);
- if (result)
- {
- numberOfContainingPoints++;
- }
- return result;
- }
- public int SquaredDistanceToCenter(int x, int y)
- {
- return (this.X - x) * (this.X - x) + (this.Y - y) * (this.Y - y);
- }
- ------------------
- [TestMethod]
- public void LISKOV_BREAK()
- {
- Square square = GetCustomShape( );
- // new Rectangle(widht =3, height = 4);
- square.Width = 2;
- Assert.AreEqual(4, square.CalculateArea());
- --> 8 as Area
- }
- ---------------------
- Simulated Polymorphic Behaviour
- -
- public override string ToXml()
- {
- StringBuilder builder = new StringBuilder();
- builder.Append("<circle");
- builder.Append(" x=\"" + this.X + "\"");
- builder.Append(" y=\"" + this.Y + "\"");
- builder.Append(" radius=\"" + this.GetRadius() + "\"");
- builder.Append(" />\n");
- return builder.ToString();
- }
- public abstract string ToXml();
- //public virtual string ToXml()
- //{
- // StringBuilder builder = new StringBuilder();
- // builder.Append(ToXml());
- // return builder.ToString();
- //}
- -------- Int to Enum --------
- Intro new class SystemStateNew (not enum)
- new property NewState { get; set; }
- Add more code that assigns state
- Goal:
- SystemStateNew stateNew = SystemStateNew.AllServicesOK;
- Assert.Equal(SystemStateNew.AllServicesOK, stateNew);
- Assert.Equal("All Services OK", SystemState.GetDescriptionForState(SystemState.AllServicesOK));
- Assert.Equal("All Services OK", SystemStateNew.GetDescriptionForState(SystemStateNew.AllServicesOK));
- -----------------
- The Core 6 refactorings are:
- * Rename
- * Inline
- * Extract Method
- * Introduce Local Variable
- * Introduce Parameter
- * Introduce Field
- In typical OO languages, there are 4 things we can name:
- 1. local variables,
- 2. methods,
- 3. parameters,
- 4. fields.
- There are also classes, but those are different.
- I think of variables, methods, parameters, and fields as scalars.
- They are atomic, simple things. Classes are compositions; they compose scalars together.
- http://arlobelshee.com/the-core-6-refactorings/
- --> Let me repeat that: if you find yourself typing indentation, newlines, space around parens, or anything else like that, you are wasting your time.
- Your editor may manipulate text better than an IDE. It may start up faster. But it can’t manipulate code as quickly as an IDE.
- ---
- todos
- -
- Focus on 5-8 Resharper Shortcuts only
- -> See Evernote
- Use this as progress during the class
- [ ] is TODO, [X] is done
- TODO
- [X] "Complicated Boolean"
- Circle.Contains
- bool result = (x - this.x) * (x - this.x) +
- (y - this.y) * (y - this.y) <=
- radius * radius;
- int squaredDistanceToCenter = sqrd(x, this.x) + sqrd(y, this.y);
- bool result = squaredDistanceToCenter <= Math.Sqrt(radius);
- [X] ShapeGroup.Add
- --> Long Method
- Extract till you drop
- [X] Ctrl E, H -> View Hierarchy
- SimpleShape Collapse hierarchy -> Alt+Del
- Show visual
- [X] DrawingBoard: Remove inheritance
- [X] Speculative Generality ComplexShape
- Pull Members Up + Delete class
- [X] Remove IShape -> not needed
- * "Convert interface to abstract class"
- * Move all members up (instead of down)
- * Alt+Del (Collapse hierarchy)
- [X] "Refused Bequest" Square+Rectangle
- 1st show "Alternative class different interface" <--------------
- Remove unused "ContainsLine" and "GetHeight"
- Encapsulate field: public int GetX() {
- [X] Color.GetColorFormatted
- --> Flag Parameter
- Extract, Gradual Cutover
- [X] 2. Flag Parameter smell in constructor
- public FileWriter(string newContent, bool useAppendMode)
- Create 2 classes: AppendingFileWriter, OverwritingFileWriter
- Extract method "virtual WriteToFile" -> if (isInAppendMode)
- Move code from FileWriter to the 2 classes
- Use "Replace Constructor with Factory Method"
- Create instance of AppendingFileWriter if isAppending
- Template method: Additional.FileWriter
- [X] Inappropriate Intimacy
- [X] ShapeGroup.shapes[]
- --> Parallel Change
- What is public?
- Encapsulate field -> don't update local usages.
- Write Access first
- Encapsulate "size"
- Read Access second
- https://martinfowler.com/bliki/ParallelChange.html
- [X] Primitive Obsession x,y -> Point
- [X] "Duplicated Code"
- Pull Up color from Rec+Circle
- [X] "Alternative classes with diff interface"
- contains / containsPoint()
- [X] "Inconsistent solution":
- ToString()
- [X] Color.ErrorCode -> Exception
- [X] "Data Clump"
- [X] "Feature Envy" Circle.toString()
- Access circle. multiple times
- [X] Int to Enum
- -> Gradual cutover
- [ ] Refactoring: "Convert to Indexer"
- [X] "Primitive Obsession"
- Circle WriteXml(StringBuilder builder)
- {
- builder.Append("<circle");
- builder.Append(" x=\"" + GetX() + "\"");
- builder.Append(" y=\"" + GetY() + "\"");
- builder.Append(" radius=\"" + GetRadius() + "\"");
- Introduce:
- // using System.Xml.Linq;
- var xmlElement = new XElement("circle",
- new XAttribute("x", circle.GetX()),
- new XAttribute("y", circle.GetY()),
- new XAttribute("radius", circle.GetRadius()));
- return xmlElement.ToString();
- [ ] AbstractShape.ToXML
- --> Switch to Polymorphism
- StringBuilder before and return after
- Extract, Make Static,
- Move Method, Make Non-Static,
- PullUp Method
- 2: Visitor Pattern:
- Use no return value in Visitor --> but property string XmlString.
- 1. Add IShapeVisitor that can visit different shapes
- 2. Add the Accept(IShapeVisitor v) on every shape by starting on "abstract class Shape"
- 3. Implement a ShapeToXmlConverter
- 4. Move code from ToXml to ShapeToXmlConverter (don't inline *all*)
- 5. Fix square Accept & GetHeight to play well
- 6. Use visitor to traverse and get XML
- var converter = new ShapeToXmlConverter();
- return (string)this.Accept(converter);
- ADVANCED: Make Generic VisitorReturns<string>
- [ ] PersonNameStrategy
- Transform Parameters
- Select 3 parameters
- PersonNameStrategy
- ------------
- # URL to Source
- http://bit.ly/QbtYzJ
- #Tips
- Collapse hierarchy = Alt+Del on class
- ## 2: Visitor Pattern:
- 1. Add IShapeVisitor that can visit different shapes
- 2. Add the Accept(IShapeVisitor v) on every shape
- 3. Implement a ShapeToXmlConverter
- 4. Move code from ToXml to ShapeToXmlConverter
- 5. Fix square Accept & GetHeight to play well
- 6. Use visitor to traverse and get XML
- Additional.zip
- ## Flag Parameter smell in constructor
- public FileWriter(string newContent, bool useAppendMode)
- Create 2 classes: AppendingFileWriter, OverwritingFileWriter
- Extract method "virtual WriteToFile" -> if (isInAppendMode)
- Move code from FileWriter to the 2 classes
- Use "Replace Constructor with Factory Method"
- Create instance of AppendingFileWriter if isAppending
- ## x,y --> Point
- Tricky: CountContainingPoints(
- int[] xCords, int[] yCords)
- ## shapes[] "Encapsulate field"
- ## shapes[] --> List<IShape> shapeList
- # Align interfaces: contains / containsPoint
- # Inconsistent solution toString()
- # Find an get rid of dead code
- # Primitive Obsession in "writing XML"
- --> Use "System.Xml.Linq.XElement"
- # Circle.ToString Feature Envy
- # Links
- # Tips Naming
- * The names indicates exactly, what the variable/method/class/package is intended for.
- * Every variable/method/class/package only does, what its name suggests.
- * Assess the quality of names from the callers perspective (on the declaration side, they always seem clear).
- * Let others assess the quality of names (e.g. in a code review).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement