任何类别中的方法顺序

时间:2023-01-27 20:28:56

I'm still very new to programming and I want to write the cleanest code possible.

我仍然是编程的新手,我想编写最干净的代码。

This may sound like a silly question, but what order should I put my methods in? Functionally of course it doesn't matter, but layout it makes a huge difference. So say we have the following code in one class:

这可能听起来像一个愚蠢的问题,但我应该把我的方法放在什么顺序?功能上当然没关系,但布局却有很大的不同。所以说我们在一个类中有以下代码:

-(void)testCreateProjectWithStartDate {
    [self setUpTestCreateProjectWithStartDate];
    ...
}

-(void)setUpTestCreateProjectWithStartDate {
    testMOC = [self setUpInMemoryStore];
    [self mockOutXMLStoreForInMemoryStore];
}

- (NSManagedObjectContext *)setUpInMemoryStore {
    ...
    NSPersistentStoreCoordinator *coordMock = [self pscMock];
    ...
}

- (NSPersistentStoreCoordinator *)pscMock {
    ...
}

-(void)mockOutXMLStoreForInMemoryStore {
    ...
}

Do I have the methods ordered in the order that they are called? What if a method is called from two places within a class?

我按顺序调用方法吗?如果从一个类中的两个地方调用一个方法怎么办?

This code snippet looks a complete mess to me - it's very confusing to have to skip about as much as this just to figure out what is a very simple flow.

这个代码片段对我来说看起来完全一团糟 - 为了弄清楚什么是非常简单的流程,必须跳过这个问题是非常困惑的。

What order of methods would make more sense?

什么样的方法更有意义?

5 个解决方案

#1


You have multiple options to group your methods:

您有多种方法可以对方法进行分组:

  • By functionality (i.e. methods which need each other are close together)
  • 通过功能(即彼此需要的方法靠得很近)

  • By visibility (e.g. public methods declared in the interface come first)
  • 通过可见性(例如,首先在界面中声明的公共方法)

  • By name (i.e. methods are just sorted by their name)
  • 按名称(即方法只按名称排序)

Personally I prefer to group methods by their functionality, so I do not need to jump too far if I trace the flow. With modern IDE's which do the jump for you this is not a big issue any more though.

我个人更喜欢按照功能对方法进行分组,所以如果跟踪流程,我不需要跳得太远。有了现代化的IDE,它可以为您带来跳跃,但这不再是一个大问题。

In your specific example, you might want to reduce the number of methods to improve readability. If your methods are all very short (2-3 lines) and only get called from one place, you could inline the code and omit some methods.

在您的特定示例中,您可能希望减少提高可读性的方法数。如果你的方法都很短(2-3行)并且只能从一个地方调用,你可以内联代码并省略一些方法。

#2


I have found this to be more true of methods than of classes - but I think it's because I'm just not doing it enough: keep it short enough, and questions like these disappear.

我发现这对方法而不是类更为正确 - 但我认为这是因为我只是做得不够:保持足够短,这些问题就消失了。

So, for methods - there have long been questions about whether to initialize all the variables at the top, or to initialize them near where they're used (the answer is near where they're used, fwiw) - but if you keep your methods short enough, it just doesn't matter. Near where they're used is at the top.

所以,对于方法 - 长期以来一直存在关于是否在顶部初始化所有变量,或者在它们被使用的地方附近初始化它们的问题(答案就在它们被使用的地方附近,fwiw) - 但是如果你保留你的方法足够短,这没关系。在他们使用的地方附近是顶部。

Same goes, at least in theory, for classes: keep them short enough, and the internal organization just doesn't matter (limiting and impossible case: just one method per class; it's automatically in whatever order you declare). If your classes have so many methods that you're wondering how to organize - think about extracting some methods that belong together into their own class. Smaller is better.

同样,至少在理论上,对于类来说:保持它们足够短,内部组织无关紧要(限制和不可能的情况:每个类只有一个方法;它会自动按照你声明的顺序)。如果你的类有很多方法,你想知道如何组织 - 考虑提取一些属于他们自己的类的方法。越小越好。

#3


There's doesn't seem to me to be a definitive answer to this, unless you have a standard to follow for your project/workplace. Personally, if I'm not following another standard, I put the constructor(s) first, followed by the destructor(s). After that, I just put them in alphabetical order by method name. I'm a bit of a dinosaur (I've been programming since the Carter administration), so I adopted the alphabetical approach for functions before I ever heard of object-oriented programming and just carried it over when I started doing objects.

除非你有一个标准要遵循你的项目/工作场所,否则在我看来似乎不是一个明确的答案。就个人而言,如果我没有遵循另一个标准,我首先放置构造函数,然后是析构函数。之后,我只是按方法名称按字母顺序排列它们。我有点像恐龙(自卡特管理以来我一直在编程),所以在我听说面向对象编程之前我采用了按字母顺序排列的函数方法,并在我开始做对象时将它带过来。

#4


I always order my methods like this :

我总是按这样命令我的方法:

first constructor/destructor, then accessors, then the other methods sorted by importance.

第一个构造函数/析构函数,然后是访问器,然后是按重要性排序的其他方法。

Important methods first, so when I open a file I have to scroll the least to get to the most important methods.

首先是重要的方法,所以当我打开一个文件时,我必须滚动最少才能获得最重要的方法。

Same for public/protected/private : public first, so everything that can be used from other files is quickly visible when opening the file.

对于public / protected / private相同:public first,因此在打开文件时可以快速看到可以从其他文件中使用的所有内容。

#5


I've had this problem too. I did try to group methods based on the role of the class. That approach doesn't really work because it's leaves a lot of things unaddressed. I've recently started using the following conventions for Objective-C:

我也遇到过这个问题。我确实尝试根据类的角色对方法进行分组。这种方法并没有真正起作用,因为它留下了很多未解决的问题。我最近开始使用Objective-C的以下约定:

Methods are in order of decreasing audience size with the exception of initialize, init, dealloc, awakeFromNib and convenience methods (ie class methods that return an auto-released instance of the class).

除了initialize,init,dealloc,awakeFromNib和便捷方法(即返回类的自动释放实例的类方法)之外,方法按顺序减少了受众大小。

//initialze, init, dealloc, awakeFromNib
//convenience methods
//properties
//IBActions
//General public methods
//Delegate methods (eg NSResponder, NSTableview etc)
//Binding and notification call back methods
//private methods

I'm still not 100% sure how to order ivars in the header file. For the time being I'm ordering them by memory management responsibility:

我仍然不能100%确定如何在头文件中订购ivars。暂时我按照内存管理责任命令他们:

//IBOutlets
//other non-retained objects (eg delegates - these can most likely be labeled IBOutlet too)
//primative data types (no mm responsibilties)
//retained objects

#1


You have multiple options to group your methods:

您有多种方法可以对方法进行分组:

  • By functionality (i.e. methods which need each other are close together)
  • 通过功能(即彼此需要的方法靠得很近)

  • By visibility (e.g. public methods declared in the interface come first)
  • 通过可见性(例如,首先在界面中声明的公共方法)

  • By name (i.e. methods are just sorted by their name)
  • 按名称(即方法只按名称排序)

Personally I prefer to group methods by their functionality, so I do not need to jump too far if I trace the flow. With modern IDE's which do the jump for you this is not a big issue any more though.

我个人更喜欢按照功能对方法进行分组,所以如果跟踪流程,我不需要跳得太远。有了现代化的IDE,它可以为您带来跳跃,但这不再是一个大问题。

In your specific example, you might want to reduce the number of methods to improve readability. If your methods are all very short (2-3 lines) and only get called from one place, you could inline the code and omit some methods.

在您的特定示例中,您可能希望减少提高可读性的方法数。如果你的方法都很短(2-3行)并且只能从一个地方调用,你可以内联代码并省略一些方法。

#2


I have found this to be more true of methods than of classes - but I think it's because I'm just not doing it enough: keep it short enough, and questions like these disappear.

我发现这对方法而不是类更为正确 - 但我认为这是因为我只是做得不够:保持足够短,这些问题就消失了。

So, for methods - there have long been questions about whether to initialize all the variables at the top, or to initialize them near where they're used (the answer is near where they're used, fwiw) - but if you keep your methods short enough, it just doesn't matter. Near where they're used is at the top.

所以,对于方法 - 长期以来一直存在关于是否在顶部初始化所有变量,或者在它们被使用的地方附近初始化它们的问题(答案就在它们被使用的地方附近,fwiw) - 但是如果你保留你的方法足够短,这没关系。在他们使用的地方附近是顶部。

Same goes, at least in theory, for classes: keep them short enough, and the internal organization just doesn't matter (limiting and impossible case: just one method per class; it's automatically in whatever order you declare). If your classes have so many methods that you're wondering how to organize - think about extracting some methods that belong together into their own class. Smaller is better.

同样,至少在理论上,对于类来说:保持它们足够短,内部组织无关紧要(限制和不可能的情况:每个类只有一个方法;它会自动按照你声明的顺序)。如果你的类有很多方法,你想知道如何组织 - 考虑提取一些属于他们自己的类的方法。越小越好。

#3


There's doesn't seem to me to be a definitive answer to this, unless you have a standard to follow for your project/workplace. Personally, if I'm not following another standard, I put the constructor(s) first, followed by the destructor(s). After that, I just put them in alphabetical order by method name. I'm a bit of a dinosaur (I've been programming since the Carter administration), so I adopted the alphabetical approach for functions before I ever heard of object-oriented programming and just carried it over when I started doing objects.

除非你有一个标准要遵循你的项目/工作场所,否则在我看来似乎不是一个明确的答案。就个人而言,如果我没有遵循另一个标准,我首先放置构造函数,然后是析构函数。之后,我只是按方法名称按字母顺序排列它们。我有点像恐龙(自卡特管理以来我一直在编程),所以在我听说面向对象编程之前我采用了按字母顺序排列的函数方法,并在我开始做对象时将它带过来。

#4


I always order my methods like this :

我总是按这样命令我的方法:

first constructor/destructor, then accessors, then the other methods sorted by importance.

第一个构造函数/析构函数,然后是访问器,然后是按重要性排序的其他方法。

Important methods first, so when I open a file I have to scroll the least to get to the most important methods.

首先是重要的方法,所以当我打开一个文件时,我必须滚动最少才能获得最重要的方法。

Same for public/protected/private : public first, so everything that can be used from other files is quickly visible when opening the file.

对于public / protected / private相同:public first,因此在打开文件时可以快速看到可以从其他文件中使用的所有内容。

#5


I've had this problem too. I did try to group methods based on the role of the class. That approach doesn't really work because it's leaves a lot of things unaddressed. I've recently started using the following conventions for Objective-C:

我也遇到过这个问题。我确实尝试根据类的角色对方法进行分组。这种方法并没有真正起作用,因为它留下了很多未解决的问题。我最近开始使用Objective-C的以下约定:

Methods are in order of decreasing audience size with the exception of initialize, init, dealloc, awakeFromNib and convenience methods (ie class methods that return an auto-released instance of the class).

除了initialize,init,dealloc,awakeFromNib和便捷方法(即返回类的自动释放实例的类方法)之外,方法按顺序减少了受众大小。

//initialze, init, dealloc, awakeFromNib
//convenience methods
//properties
//IBActions
//General public methods
//Delegate methods (eg NSResponder, NSTableview etc)
//Binding and notification call back methods
//private methods

I'm still not 100% sure how to order ivars in the header file. For the time being I'm ordering them by memory management responsibility:

我仍然不能100%确定如何在头文件中订购ivars。暂时我按照内存管理责任命令他们:

//IBOutlets
//other non-retained objects (eg delegates - these can most likely be labeled IBOutlet too)
//primative data types (no mm responsibilties)
//retained objects