Created
March 1, 2023 04:59
-
-
Save Luckey-Elijah/9530f1b0ff5e1c1616cd11acb51d98dc to your computer and use it in GitHub Desktop.
reference: https://youtu.be/tD5NrevFtbU
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:math'; | |
import 'package:benchmark_harness/benchmark_harness.dart'; | |
void main(List<String> args) { | |
final random = Random(); | |
ShapeType randomShapeType() => ShapeType.values[random.nextInt(4)]; | |
double randomSize() => random.nextDouble(); | |
const size = 1000; | |
final List<ShapeUnion> shapeUnions = <ShapeUnion>[]; | |
final List<Shape> shapeClasses = <Shape>[]; | |
for (var i = 0; i < size; i++) { | |
final type = randomShapeType(); | |
final height = randomSize(); | |
late final width = randomSize(); | |
shapeUnions.add(ShapeUnion(type, width, height)); | |
shapeClasses.add(Shape.fromType(type, width, height)); | |
} | |
CleanCodeBenchMark(shapeClasses) | |
..exercise() | |
..report(); | |
NonCleanCodeBenchMark(shapeUnions) | |
..exercise() | |
..report(); | |
} | |
class NonCleanCodeBenchMark extends BenchmarkBase { | |
NonCleanCodeBenchMark(this.shapes) : super('Dirty Code'); | |
final List<ShapeUnion> shapes; | |
@override | |
void run() => totalAreaNonClean(shapes); | |
} | |
class CleanCodeBenchMark extends BenchmarkBase { | |
CleanCodeBenchMark(this.shapes) : super('Clean Code'); | |
final List<Shape> shapes; | |
@override | |
void run() => totalAreaClean(shapes); | |
} | |
// NON CLEAN CODE | |
enum ShapeType { square, rectangle, triangle, circle } | |
class ShapeUnion { | |
final double width, height; | |
final ShapeType type; | |
ShapeUnion(this.type, this.width, this.height); | |
} | |
double totalAreaNonClean(Iterable<ShapeUnion> shapes) { | |
return shapes.fold( | |
0.0, | |
(value, shape) => value + getAreaUnion(shape), | |
); | |
} | |
const coefficient = { | |
ShapeType.square: 1.0, | |
ShapeType.rectangle: 1.0, | |
ShapeType.triangle: .5, | |
ShapeType.circle: pi, | |
}; | |
double getAreaUnion(ShapeUnion shape) => | |
coefficient[shape.type]! * shape.width * shape.height; | |
// CLEAN CODE | |
double totalAreaClean(Iterable<Shape> shapes) { | |
return shapes.fold( | |
0.0, | |
(value, element) => value + element.area(), | |
); | |
} | |
abstract class Shape { | |
static Shape fromType(ShapeType type, double width, double height) { | |
switch (type) { | |
case ShapeType.square: | |
return Square(width); | |
case ShapeType.rectangle: | |
return Rectangle(width, height); | |
case ShapeType.triangle: | |
return Triangle(width, height); | |
case ShapeType.circle: | |
return Circle(width); | |
} | |
} | |
double area(); | |
} | |
class Square extends Shape { | |
Square(this.side); | |
final double side; | |
@override | |
double area() => side * side; | |
} | |
class Rectangle extends Shape { | |
Rectangle(this.width, this.height); | |
final double width, height; | |
@override | |
double area() => width * height; | |
} | |
class Triangle extends Shape { | |
Triangle(this.base, this.height); | |
final double base, height; | |
@override | |
double area() => base * height * 0.5; | |
} | |
class Circle extends Shape { | |
Circle(this.radius); | |
final double radius; | |
@override | |
double area() => radius * radius * pi; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment