Need advice on verbose method naming

I am writing an API for creating geometric shapes, and I am having some difficulties naming my methods.

Take a simple case: creating a circle. Most of us may be familiar with the type method graphics.drawEllipse(x, y, w, h). To draw a circle, you need to know the upper left coordinate, as well as the width and height of the circle.

My API is designed so that the developer easily draws shapes using a variety of information, without doing a lot of math, which is trivial for circles, but more complex for other shapes. For example, you should also be able to draw a circle, taking into account its central coordinates and radius, as well as the upper left and lower right coordinates.

So I have a class Circlewith factory, for example:

Circle.createWithCenterAndRadius(cx, cy, r)
Circle.createWithBoundingBox(x1, y1, x2, y2)
Circle.createWithWidthAndHeight(x, y, w, h)

I feel that there might be a “code smell”, but I'm not sure. On the one hand, these factory methods are necessarily descriptive. On the other hand, I can foresee that these method names get out of hand. For example, what can I call the factory triangle method that creates a triangle based on a point, the length of one side, the angle and the length of the other side? Triangle.createWithPointSideAngleAndSide(x, y, side1, angle, side2)? Is it just evil?

If you intend to use this API, will the method names like this be okay? Do you have any tips on how I can make method names more reasonable?

+5
source share
9 answers

You can change the circle methods to

Circle.FromCenterAndRadius(...)
Circle.FromBoundingBox(...)
Circle.FromWidthAndHeight(...)

, ...

+14

, . , Create .

:

Circle.Create(
   centerX = cx, 
   centerY = cy, 
   radius = r
);

( , , ):

circleBuilder.Center(cx,cy).Radius(r)
circleBuilder.Center(x,y).Width(w).Height(y)
circleBuilder.BoundWith().Left(x1,y1).Right(x2,y2)

, . BoundWith , Left.

+11

, - , . , -.

, , factory - , factory . CircleProperties , CenterX, CenterY, Radius, (bool) UseCenterX, (bool) UseCenterY .., factory, () factory .

, #:

var circleProperties = new CircleProperties()
{
   CenterX = 10,
   CenterY = -5,
   Radius = 8,
   UseCenterX = true,
   UseCenterY = true,
   UseCenterRadius = true
};

var circle = Circle.Create(circleProperties);
+8

, , .

// instead of Circle.createWithCenterAndRadius(cx, cy, r)
Circle.create( new Point(cx,xy), r);

// instead of Circle.createWithBoundingBox(x1, y1, x2, y2)
Circle.create( new Point(x1,y1), new Point(x1,y1) );
// or even...
Circle.create( new Box(p1,p2));

// instead of Circle.createWithWidthAndHeight(x, y, w, h)
Circle.create( new Point(x,y), w, h);

Point, Distance ( )

, , factory .

Circle c = new Circle(new Point(cx,xy), r);
+6

, , , Circle.create, (, "center" "" ), , , , ? , , , .

+1

CreateTriangle .

.

Circle.CreateCircle(cx, cy, r)
Circle.CreateCircle(point1, point2)
Circle.CreateCircle(point, width, height)
+1

, -, , Apple Cocoa.

0

- - iffy.

, . 5 , .

, , , - .

, , XML .

, .

Swing Java. "new Button()", .set, ( ) reset ..

, , - .

0

, . , C/++/Java, , , , , CamelCaseNaming.

:

Circle.createWithCenterAndRadius(cx, cy, r)  
Circle.createWithBoundingBox(x1, y1, x2, y2)
Circle.createWithWidthAndHeight(x, y, w, h)

And now let's get rid of this camel's notation

Circle.create_with_center_and_radius(cx, cy, r)  
Circle.create_with_bounding_box(x1, y1, x2, y2)
Circle.create_with_width_and_height(x, y, w, h)

This may seem terribly incomprehensible, but to be honest: which version is easier to read?

-1
source

All Articles