Open
Conversation
Introduction Code Naming Basics Naming Methods Naming Functions Naming Properties and Data Types Acceptable Abbreviations and Acronyms Tips and Techniques for Framework Developers Revision History NextPrevious Naming Methods Methods are perhaps the most common element of your programming interface, so you should take particular care in how you name them. This section discusses the following aspects of method naming: General Rules Here are a few general guidelines to keep in mind when naming methods: Start the name with a lowercase letter and capitalize the first letter of embedded words. Don’t use prefixes. See Typographic Conventions. There are two specific exceptions to these guidelines. You may begin a method name with a well-known acronym in uppercase (such as TIFF or PDF)), and you may use prefixes to group and identify private methods (see Private Methods). For methods that represent actions an object takes, start the name with a verb: - (void)invokeWithTarget:(id)target; - (void)selectTabViewItem:(NSTabViewItem *)tabViewItem Do not use “do” or “does” as part of the name because these auxiliary verbs rarely add meaning. Also, never use adverbs or adjectives before the verb. If the method returns an attribute of the receiver, name the method after the attribute. The use of “get” is unnecessary, unless one or more values are returned indirectly. - (NSSize)cellSize; Right. - (NSSize)calcCellSize; Wrong. - (NSSize)getCellSize; Wrong. See also Accessor Methods. Use keywords before all arguments. - (void)sendAction:(SEL)aSelector toObject:(id)anObject forAllCells:(BOOL)flag; Right. - (void)sendAction:(SEL)aSelector :(id)anObject :(BOOL)flag; Wrong. Make the word before the argument describe the argument. - (id)viewWithTag:(NSInteger)aTag; Right. - (id)taggedView:(int)aTag; Wrong. Add new keywords to the end of an existing method when you create a method that is more specific than the inherited one. - (id)initWithFrame:(CGRect)frameRect; NSView, UIView. - (id)initWithFrame:(NSRect)frameRect mode:(int)aMode cellClass:(Class)factoryId numberOfRows:(int)rowsHigh numberOfColumns:(int)colsWide; NSMatrix, a subclass of NSView Don’t use “and” to link keywords that are attributes of the receiver. - (int)runModalForDirectory:(NSString *)path file:(NSString *) name types:(NSArray *)fileTypes; Right. - (int)runModalForDirectory:(NSString *)path andFile:(NSString *)name andTypes:(NSArray *)fileTypes; Wrong. Although “and” may sound good in this example, it causes problems as you create methods with more and more keywords. If the method describes two separate actions, use “and” to link them. - (BOOL)openFile:(NSString *)fullPath withApplication:(NSString *)appName andDeactivate:(BOOL)flag; NSWorkspace. Accessor Methods Accessor methods are those methods that set and return the value of a property of an object. They have certain recommended forms, depending on how the property is expressed: If the property is expressed as a noun, the format is: - (type)noun; - (void)setNoun:(type)aNoun; For example: - (NSString *)title; - (void)setTitle:(NSString *)aTitle; If the property is expressed as an adjective, the format is: - (BOOL)isAdjective; - (void)setAdjective:(BOOL)flag; For example: - (BOOL)isEditable; - (void)setEditable:(BOOL)flag; If the property is expressed as a verb, the format is: - (BOOL)verbObject; - (void)setVerbObject:(BOOL)flag; For example: - (BOOL)showsAlpha; - (void)setShowsAlpha:(BOOL)flag; The verb should be in the simple present tense. Don’t twist a verb into an adjective by using a participle: - (void)setAcceptsGlyphInfo:(BOOL)flag; Right. - (BOOL)acceptsGlyphInfo; Right. - (void)setGlyphInfoAccepted:(BOOL)flag; Wrong. - (BOOL)glyphInfoAccepted; Wrong. You may use modal verbs (verbs preceded by “can”, “should”, “will”, and so on) to clarify meaning, but don’t use “do” or “does”. - (void)setCanHide:(BOOL)flag; Right. - (BOOL)canHide; Right. - (void)setShouldCloseDocument:(BOOL)flag; Right. - (BOOL)shouldCloseDocument; Right. - (void)setDoesAcceptGlyphInfo:(BOOL)flag; Wrong. - (BOOL)doesAcceptGlyphInfo; Wrong. Use “get” only for methods that return objects and values indirectly. You should use this form for methods only when multiple items need to be returned. - (void)getLineDash:(float *)pattern count:(int *)count phase:(float *)phase; NSBezierPath. In methods such as these, the implementation should accept NULL for these in–out parameters as an indication that the caller is not interested in one or more of the returned values. Delegate Methods Delegate methods (or delegation methods) are those that an object invokes in its delegate (if the delegate implements them) when certain events occur. They have a distinctive form, which apply equally to methods invoked in an object’s data source: Start the name by identifying the class of the object that’s sending the message: - (BOOL)tableView:(NSTableView *)tableView shouldSelectRow:(int)row; - (BOOL)application:(NSApplication *)sender openFile:(NSString *)filename; The class name omits the prefix and the first letter is in lowercase. A colon is affixed to the class name (the argument is a reference to the delegating object) unless the method has only one argument, the sender. - (BOOL)applicationOpenUntitledFile:(NSApplication *)sender; An exception to this are methods that invoked as a result of a notification being posted. In this case, the sole argument is the notification object. - (void)windowDidChangeScreen:(NSNotification *)notification; Use “did” or “will” for methods that are invoked to notify the delegate that something has happened or is about to happen. - (void)browserDidScroll:(NSBrowser *)sender; - (NSUndoManager *)windowWillReturnUndoManager:(NSWindow *)window; Although you can use “did” or “will” for methods that are invoked to ask the delegate to do something on behalf of another object, “should” is preferred. - (BOOL)windowShouldClose:(id)sender; Collection Methods For objects that manage a collection of objects (each called an element of that collection), the convention is to have methods of the form: - (void)addElement:(elementType)anObj; - (void)removeElement:(elementType)anObj; - (NSArray *)elements; For example: - (void)addLayoutManager:(NSLayoutManager *)obj; - (void)removeLayoutManager:(NSLayoutManager *)obj; - (NSArray *)layoutManagers; The following are some qualifications and refinements to this guideline: If the collection is truly unordered, return an NSSet object rather than an NSArray object. If it’s important to insert elements into a specific location in the collection, use methods similar to the following instead of or in addition to the ones above: - (void)insertLayoutManager:(NSLayoutManager *)obj atIndex:(int)index; - (void)removeLayoutManagerAtIndex:(int)index; There are a couple of implementation details to keep in mind with collection methods: These methods typically imply ownership of the inserted objects, so the code that adds or inserts them must retain them, and the code that removes them must also release them. If the inserted objects need to have a pointer back to the main object, you do this (typically) with a set... method that sets the back pointer but does not retain. In the case of the insertLayoutManager:atIndex: method, the NSLayoutManager class does this in these methods: - (void)setTextStorage:(NSTextStorage *)textStorage; - (NSTextStorage *)textStorage; You would normally not call setTextStorage: directly, but might want to override it. Another example of the above conventions for collection methods comes from the NSWindow class: - (void)addChildWindow:(NSWindow *)childWin ordered:(NSWindowOrderingMode)place; - (void)removeChildWindow:(NSWindow *)childWin; - (NSArray *)childWindows; - (NSWindow *)parentWindow; - (void)setParentWindow:(NSWindow *)window; Method Arguments There are a few general rules concerning the names of method arguments: As with methods, arguments start with a lowercase letter and the first letter of successive words are capitalized (for example, removeObject:(id)anObject). Don’t use “pointer” or “ptr” in the name. Let the argument’s type rather than its name declare whether it’s a pointer. Avoid one- and two-letter names for arguments. Avoid abbreviations that save only a few letters. Traditionally (in Cocoa), the following keywords and arguments are used together:
+Update READMe.md
This file contains hidden or 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
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
include
using namespace std;
class Shape {
protected:
int width, height;
public:
Shape( int a=0, int b=0)
{
width = a;
height = b;
}
// pure virtual function
virtual int area() = 0;
};
class Rectangle: public Shape{
public:
Rectangle( int a=0, int b=0):Shape(a, b) { }
int area ()
{
cout << "Rectangle class area :" <<endl;
return (width * height);
}
};
class Triangle: public Shape{
public:
Triangle( int a=0, int b=0):Shape(a, b) { }
int area ()
{
cout << "Triangle class area :" <<endl;
return (width * height / 2);
}
};
// Main function for the program
int main( )
{
Shape *shape;
Rectangle rec(10,7);
Triangle tri(10,5);
// store the address of Rectangle
shape = &rec;
// call rectangle area.
shape->area();
// store the address of Triangle
shape = &tri;
// call triangle area.
shape->area();
return 0;
}