Question

I have this method in my NDVector class which finds the angle between two vectors along two axes (for 3D vectors this means you can find the angle between them just in terms of their X and Y components, X and Z components, or Y and Z components):

- (float) angleBetweenThisAnd: (NDVector *) vector2 byAxis: (NSUInteger) axis1 and: (NSUInteger) axis2;

I don't think I'm utilizing the descriptive naming you can do with methods which take multiple parameters in Objective-C. I find myself doing this a lot, really. I think of the method, say it as a sentence which doesn't state the relationship between its subject and its verb in plain English (i.e. [someVector angleBetweenThisAnd: otherVec ...] instead of [someVector findsAngleBetweenItselfAnd: otherVec ...]) and then write it as the method name, but it seems so redundant to say "and" in the name of a method. I mean of course it's and!

In Java, naming methods was a lot simpler, but in Objective-C, I'm confused by the close relationship between plain English and code in method names. Most importantly, is there a common way to avoid using "and" in the name of a method?

Était-ce utile?

La solution

Look at some of the NSDate comparison methods for inspiration.

For example, following a pattern like timeIntervalSinceDate: or descriptionWithCalendarFormat:timeZone:locale:, how about [NDVector angleWhenIntersectingWithVector:axis1:axis2]?

This could look like:

- (float) angleWhenIntersectingWithVector:(NDVector *)vector2
                                    axis1:(NSUInteger)axis1
                                    axis2:(NSUInteger)axis2;

Autres conseils

It's possible to write method parameters that have no name. This is uncommon, but is occasionally the cleanest way to style the code.

- (float) angleWithVector:(NDVector *)vector2 axes:(NSUInteger)axis1 :(NSUInteger)axis2;

[vector angleWithVector:otherVector axes:b :K]

@selector(angleWithVector:axes::)

If you look at your method you made, you're really not being descriptive with the method itself. You are correct, using "and" is never good, using "and" as a parameter description is horrible. Think of it this way, if you don't have the input parameters named, how can you (or someone else) tell what it is (i.e., inline in code)?

For example: - (float) angleBetweenThisAnd: byAxis: and:

in use: [currentThing angleBetweenThisAnd:myVec byAxis:b and:K];

What is "this" in the first input parameter? What should I pass to "and"??? What is an "and" value supposed to be? Maybe you're the only one using your code, but time will come you haven't looked at it and forget how it works.

Think about putting a descriptive word in the method name. By having the method names with descriptive text, anyone else can read it, especially when you call it in code, since the values won't have their types or default names. I would rewrite your method as such:

-(float)angleBetweenVector2:(NDVector *)vector2 withAxis1:(NSUInteger)axis1 withAxis2:(NSUInteger)axis2;

Another approach addressing the title question is to ask

"How does the signature work when there are no argument names"

So your original version.

angleBetweenThisAnd:byAxis:and: doesn't scan so well.

Whereas Aaron's version does…

angleWhenIntersectingWithVector:axis1:axis2:

The method signature now gives a clear indication of what it does without embedding info in the arg names.

After your comment: If you have the freedom you could use an enumeration rather than a struct:

typedef enum { XAxis = 1,
               YAxis = 2,
               ZAxis = 4,
               XYPlane = 3,
               XZPlane = 5,
               YZPlane = 6
             } AxisOrPlane;

- (float) angleAtIntersectionWith:(NDVector *)vector inPlane:(AxisOrPlane)plane

The enumeration values have been picked so XYPlane is equivalent to XAxis | YAxis etc., but you don't need to do that.

A struct will of course work as well, for example:

typedef struct { NSUInteger axis1; NSUInteger axis2; } Plane;

- (CGFloat) angleAtIntersectionWith:(NSArray *)vector inPlane:(Plane)plane

and when calling the method you can use a structure literal:

[aVector angleAtIntersectionWith:anotherVector inPlane:(Plane){XAxis,ZAxis}];

where XAxis and YAxis are defined appropriately.

HTH

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top