Advertisement
Guest User

Untitled

a guest
Oct 16th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.70 KB | None | 0 0
  1. - "Narrow Change Strategy" -> Schnittstellenumbau
  2. public string GetErrorMessage()
  3. {
  4. return this.errorMessage;
  5. }
  6.  
  7.  
  8.  
  9. Narrow change: use an existing variable, to assign it something new (ie. the change we want to apply in several places),
  10. and then to inline that variable, so that our change takes effect everywhere where the variable was used.
  11. http://dwyler.net/blog/refactoring-introduce-new-class-and-move-behaviour-to-it/
  12.  
  13.  
  14. - "Feature Envy" Circle.toString()
  15.  
  16. -
  17. public override bool Contains(int x, int y)
  18. {
  19. int squaredDistanceToCenter = sqrd(x, this.x) + sqrd(y, this.y);
  20.  
  21. bool result = squaredDistanceToCenter <= Math.Sqrt(radius);
  22.  
  23. if (result)
  24. {
  25. numberOfContainingPoints++;
  26. }
  27. return result;
  28. }
  29.  
  30.  
  31. --------------
  32. mit Point
  33.  
  34.  
  35. public override bool Contains(Point point)
  36. {
  37. int squaredDistanceToCenter = sqrd(point.X, this.x) + sqrd(point.Y, this.y);
  38.  
  39. bool result = squaredDistanceToCenter <= Math.Sqrt(radius);
  40.  
  41. if (result)
  42. {
  43. numberOfContainingPoints++;
  44. }
  45. return result;
  46. }
  47.  
  48. private int sqrd(int a, int b)
  49. {
  50. return (a - b) * (a - b);
  51. }
  52.  
  53.  
  54. public int CountContainingPoints(int[] xCords, int[] yCords)
  55. {
  56. Point[] points = new Point[xCords.Length];
  57.  
  58. for (int i = 0; i < points.Length; ++i)
  59. {
  60. points[i] = new Point(xCords[i], yCords[i]);
  61.  
  62. }
  63.  
  64. return CountContainingPoints(points);
  65. }
  66.  
  67. public int CountContainingPoints(Point[] points)
  68. {
  69. numberOfContainingPoints = 0;
  70. for (int i = 0; i < points.Length; ++i)
  71. {
  72. Contains(points[i]);
  73. }
  74. return numberOfContainingPoints;
  75. }
  76.  
  77.  
  78. ----------------
  79. public override bool Contains(Point point)
  80. {
  81. bool result = point.SquaredDistanceToCenter(this.x, this.y) <= Math.Sqrt(radius);
  82.  
  83. if (result)
  84. {
  85. numberOfContainingPoints++;
  86. }
  87. return result;
  88. }
  89.  
  90. public int SquaredDistanceToCenter(int x, int y)
  91. {
  92. return (this.X - x) * (this.X - x) + (this.Y - y) * (this.Y - y);
  93. }
  94.  
  95.  
  96.  
  97.  
  98. ------------------
  99.  
  100.  
  101. [TestMethod]
  102. public void LISKOV_BREAK()
  103. {
  104. Square square = GetCustomShape( );
  105. // new Rectangle(widht =3, height = 4);
  106.  
  107. square.Width = 2;
  108.  
  109.  
  110. Assert.AreEqual(4, square.CalculateArea());
  111. --> 8 as Area
  112.  
  113. }
  114.  
  115.  
  116.  
  117.  
  118.  
  119. ---------------------
  120. Simulated Polymorphic Behaviour
  121. -
  122. public override string ToXml()
  123. {
  124. StringBuilder builder = new StringBuilder();
  125. builder.Append("<circle");
  126. builder.Append(" x=\"" + this.X + "\"");
  127. builder.Append(" y=\"" + this.Y + "\"");
  128. builder.Append(" radius=\"" + this.GetRadius() + "\"");
  129. builder.Append(" />\n");
  130.  
  131. return builder.ToString();
  132. }
  133.  
  134.  
  135. public abstract string ToXml();
  136.  
  137. //public virtual string ToXml()
  138. //{
  139. // StringBuilder builder = new StringBuilder();
  140.  
  141. // builder.Append(ToXml());
  142.  
  143. // return builder.ToString();
  144. //}
  145.  
  146.  
  147.  
  148. -------- Int to Enum --------
  149. Intro new class SystemStateNew (not enum)
  150. new property NewState { get; set; }
  151.  
  152. Add more code that assigns state
  153.  
  154. Goal:
  155. SystemStateNew stateNew = SystemStateNew.AllServicesOK;
  156.  
  157.  
  158. Assert.Equal(SystemStateNew.AllServicesOK, stateNew);
  159.  
  160.  
  161.  
  162. Assert.Equal("All Services OK", SystemState.GetDescriptionForState(SystemState.AllServicesOK));
  163.  
  164.  
  165.  
  166. Assert.Equal("All Services OK", SystemStateNew.GetDescriptionForState(SystemStateNew.AllServicesOK));
  167.  
  168.  
  169.  
  170.  
  171. -----------------
  172.  
  173. The Core 6 refactorings are:
  174.  
  175. * Rename
  176. * Inline
  177.  
  178. * Extract Method
  179.  
  180. * Introduce Local Variable
  181.  
  182. * Introduce Parameter
  183.  
  184. * Introduce Field
  185.  
  186.  
  187.  
  188.  
  189. In typical OO languages, there are 4 things we can name:
  190.  
  191. 1. local variables,
  192.  
  193. 2. methods,
  194.  
  195. 3. parameters,
  196.  
  197. 4. fields.
  198.  
  199.  
  200. There are also classes, but those are different.
  201. I think of variables, methods, parameters, and fields as scalars.
  202. They are atomic, simple things. Classes are compositions; they compose scalars together.
  203.  
  204.  
  205.  
  206. http://arlobelshee.com/the-core-6-refactorings/
  207. --> Let me repeat that: if you find yourself typing indentation, newlines, space around parens, or anything else like that, you are wasting your time.
  208.  
  209.  
  210.  
  211. 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.
  212.  
  213.  
  214.  
  215.  
  216. ---
  217. todos
  218.  
  219. -
  220. Focus on 5-8 Resharper Shortcuts only
  221. -> See Evernote
  222.  
  223. Use this as progress during the class
  224. [ ] is TODO, [X] is done
  225.  
  226. TODO
  227. [X] "Complicated Boolean"
  228. Circle.Contains
  229. bool result = (x - this.x) * (x - this.x) +
  230. (y - this.y) * (y - this.y) <=
  231. radius * radius;
  232. int squaredDistanceToCenter = sqrd(x, this.x) + sqrd(y, this.y);
  233. bool result = squaredDistanceToCenter <= Math.Sqrt(radius);
  234.  
  235.  
  236. [X] ShapeGroup.Add
  237. --> Long Method
  238. Extract till you drop
  239.  
  240.  
  241. [X] Ctrl E, H -> View Hierarchy
  242. SimpleShape Collapse hierarchy -> Alt+Del
  243. Show visual
  244.  
  245. [X] DrawingBoard: Remove inheritance
  246.  
  247. [X] Speculative Generality ComplexShape
  248. Pull Members Up + Delete class
  249.  
  250.  
  251. [X] Remove IShape -> not needed
  252. * "Convert interface to abstract class"
  253. * Move all members up (instead of down)
  254. * Alt+Del (Collapse hierarchy)
  255.  
  256.  
  257. [X] "Refused Bequest" Square+Rectangle
  258. 1st show "Alternative class different interface" <--------------
  259. Remove unused "ContainsLine" and "GetHeight"
  260. Encapsulate field: public int GetX() {
  261.  
  262.  
  263. [X] Color.GetColorFormatted
  264. --> Flag Parameter
  265. Extract, Gradual Cutover
  266. [X] 2. Flag Parameter smell in constructor
  267. public FileWriter(string newContent, bool useAppendMode)
  268. Create 2 classes: AppendingFileWriter, OverwritingFileWriter
  269. Extract method "virtual WriteToFile" -> if (isInAppendMode)
  270. Move code from FileWriter to the 2 classes
  271. Use "Replace Constructor with Factory Method"
  272. Create instance of AppendingFileWriter if isAppending
  273.  
  274. Template method: Additional.FileWriter
  275.  
  276.  
  277. [X] Inappropriate Intimacy
  278.  
  279. [X] ShapeGroup.shapes[]
  280. --> Parallel Change
  281. What is public?
  282. Encapsulate field -> don't update local usages.
  283. Write Access first
  284. Encapsulate "size"
  285. Read Access second
  286. https://martinfowler.com/bliki/ParallelChange.html
  287.  
  288.  
  289. [X] Primitive Obsession x,y -> Point
  290.  
  291.  
  292. [X] "Duplicated Code"
  293. Pull Up color from Rec+Circle
  294.  
  295. [X] "Alternative classes with diff interface"
  296. contains / containsPoint()
  297.  
  298. [X] "Inconsistent solution":
  299. ToString()
  300.  
  301.  
  302. [X] Color.ErrorCode -> Exception
  303. [X] "Data Clump"
  304. [X] "Feature Envy" Circle.toString()
  305. Access circle. multiple times
  306.  
  307.  
  308. [X] Int to Enum
  309. -> Gradual cutover
  310.  
  311.  
  312. [ ] Refactoring: "Convert to Indexer"
  313.  
  314.  
  315.  
  316. [X] "Primitive Obsession"
  317. Circle WriteXml(StringBuilder builder)
  318. {
  319. builder.Append("<circle");
  320. builder.Append(" x=\"" + GetX() + "\"");
  321. builder.Append(" y=\"" + GetY() + "\"");
  322. builder.Append(" radius=\"" + GetRadius() + "\"");
  323.  
  324. Introduce:
  325. // using System.Xml.Linq;
  326. var xmlElement = new XElement("circle",
  327. new XAttribute("x", circle.GetX()),
  328. new XAttribute("y", circle.GetY()),
  329. new XAttribute("radius", circle.GetRadius()));
  330. return xmlElement.ToString();
  331.  
  332. [ ] AbstractShape.ToXML
  333. --> Switch to Polymorphism
  334. StringBuilder before and return after
  335. Extract, Make Static,
  336. Move Method, Make Non-Static,
  337. PullUp Method
  338. 2: Visitor Pattern:
  339. Use no return value in Visitor --> but property string XmlString.
  340.  
  341. 1. Add IShapeVisitor that can visit different shapes
  342. 2. Add the Accept(IShapeVisitor v) on every shape by starting on "abstract class Shape"
  343. 3. Implement a ShapeToXmlConverter
  344. 4. Move code from ToXml to ShapeToXmlConverter (don't inline *all*)
  345. 5. Fix square Accept & GetHeight to play well
  346. 6. Use visitor to traverse and get XML
  347. var converter = new ShapeToXmlConverter();
  348. return (string)this.Accept(converter);
  349.  
  350. ADVANCED: Make Generic VisitorReturns<string>
  351.  
  352. [ ] PersonNameStrategy
  353. Transform Parameters
  354. Select 3 parameters
  355. PersonNameStrategy
  356.  
  357.  
  358. ------------
  359. # URL to Source
  360. http://bit.ly/QbtYzJ
  361.  
  362.  
  363. #Tips
  364. Collapse hierarchy = Alt+Del on class
  365.  
  366. ## 2: Visitor Pattern:
  367. 1. Add IShapeVisitor that can visit different shapes
  368. 2. Add the Accept(IShapeVisitor v) on every shape
  369. 3. Implement a ShapeToXmlConverter
  370. 4. Move code from ToXml to ShapeToXmlConverter
  371. 5. Fix square Accept & GetHeight to play well
  372. 6. Use visitor to traverse and get XML
  373.  
  374. Additional.zip
  375.  
  376. ## Flag Parameter smell in constructor
  377. public FileWriter(string newContent, bool useAppendMode)
  378.  
  379. Create 2 classes: AppendingFileWriter, OverwritingFileWriter
  380. Extract method "virtual WriteToFile" -> if (isInAppendMode)
  381. Move code from FileWriter to the 2 classes
  382. Use "Replace Constructor with Factory Method"
  383. Create instance of AppendingFileWriter if isAppending
  384.  
  385.  
  386. ## x,y --> Point
  387. Tricky: CountContainingPoints(
  388. int[] xCords, int[] yCords)
  389.  
  390. ## shapes[] "Encapsulate field"
  391. ## shapes[] --> List<IShape> shapeList
  392.  
  393. # Align interfaces: contains / containsPoint
  394.  
  395. # Inconsistent solution toString()
  396.  
  397. # Find an get rid of dead code
  398.  
  399. # Primitive Obsession in "writing XML"
  400. --> Use "System.Xml.Linq.XElement"
  401.  
  402. # Circle.ToString Feature Envy
  403.  
  404.  
  405.  
  406. # Links
  407.  
  408.  
  409.  
  410. # Tips Naming
  411. * The names indicates exactly, what the variable/method/class/package is intended for.
  412. * Every variable/method/class/package only does, what its name suggests.
  413. * Assess the quality of names from the callers perspective (on the declaration side, they always seem clear).
  414. * Let others assess the quality of names (e.g. in a code review).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement