Which is better: write functions or write methods? What costs more performance?

I am currently making some decisions for my first objective-c API. Nothing special, just help yourself a bit to make things faster in the future.

After reading a few hours about different templates, such as creating categories, singletons, etc., I got something that I like because it is easy for me to support me. I am creating a set of useful functions that can be useful everywhere.

So what I did:

1) I created two new files (.h, .m) and gave the โ€œclassโ€ a name: SLUtilsMath, SLUtilsGraphics, SLUtilsSound, etc. I think of it as a "namespace", so all of these things will always be called SLUtils ******. I added them to the SL group containing the SLUtils subgroup.

2) Then I just put my function signatures in the .h file and the function implementations in the .m file. And guess what: It works! I am pleased with this and it is easy to use. The only unpleasant thing is that I have to include the appropriate header every time I need it. But this is normal, as it is normal. I could include it in the header header prefix file.

But then I went to the toilet, and a ghost appeared there, saying: โ€œHey, isnโ€™t it better to create real methods, not functions? Should you not create cool methods so that you call a method and not a function? Isn't that a lot cooler and doesn't have the best performance? " Well, for readability, I prefer features. On the other hand, they do not have such "named parameters" as methods, afaik.

So what would you prefer in this case?

, . .

, . . , :

MYNAMESPACECoolMath.h

#import <Foundation/Foundation.h>


@interface MYNAMESPACECoolMath : NSObject {
}

+ (float)randomizeValue:(float)value byPercent:(float)percent;

+ (float)calculateHorizontalGravity:(CGPoint)p1 andPoint:(CGPoint)p2;

// and some more
@end

MYNAMESPACECoolMath.h :

CGFloat myValue = [MYNAMESPACECoolMath randomizeValue: 10.0f byPercent: 5.0f];

, , , . java, .

, , . CGRectMake (10.0f, 42.5f, 44.2f, 99.11f) , , , , . "named", , .

, , , /, . , . . . . .

+3
5

, , .

, , , , , .

, , , Obj-C - "OO-only", Java #. () , . , . . . . , , , , ( ), /.

+6

- "" . , "", , , , . , , .

, , , , ( ).

Josh, ObjC , - , - Smalltalk. ; Cocoa.

, ( NSLocalizedString()). , , . "util" .

, , . , +randomizeValue:byPercent: - NSNumber:

// NSNumber+SLExtensions.h
- (double)randomizeByPercent:(CGFloat)percent;
+ (double)randomDoubleNear:(CGFloat)percent byPercent:(double)number;
+ (NSNumber *)randomNumberNear:(CGFloat)percent byPercent:(double)number;

// Some other file that wants to use this
#import "NSNumber+SLExtensions.h"
randomDouble = [aNumber randomizeByPercent:5.0];
randomDouble = [NSNumber randomDoubleNear:5.0 byPercent:7.0];

, , NSNumber + Random. , , , .

+5

singleton, .

C. , @selector , performSelectorOnMainThread.

+1

, " " , Objective-C , , , . , , .

+1

, .

, , , , , . , objective-c .

0
source

Source: https://habr.com/ru/post/1708758/


All Articles