Code Monkey home page Code Monkey logo

objc-guide's Introduction

Introduction

The Objective-C coding conventions guide documents many best practices learned from over a decade of experience developing software on Apple platforms at Microsoft, such as Office for Mac and iOS. This is not a style guide (use clang-format for that), but guidelines that affect implementation.

This document is organized by language area. We have documented many conventions we regularly use by language area, which are accompanied with rationale, examples, and a history if applicable.

Not all teams use every convention documented here and others may be tweaked to be more amendable to team preferences. The intent is to facilitate developing maintainable and robust code. The conventions evolve with the Objective-C language, frameworks, and experience. Feedback and discussion is welcome and encouraged. For details on how to contribute, see the contribution guidelines.

Table of Contents

Legal Notices

Microsoft and any contributors grant you a license to the Microsoft documentation and other content in this repository under the Creative Commons Attribution 4.0 International Public License, see the LICENSE file, and grant you a license to any code in the repository under the MIT License, see the LICENSE-CODE file.

Microsoft, Windows, Microsoft Azure and/or other Microsoft products and services referenced in the documentation may be either trademarks or registered trademarks of Microsoft in the United States and/or other countries. The licenses for this project do not grant you rights to use any Microsoft names, logos, or trademarks. Microsoft's general trademark guidelines can be found at http://go.microsoft.com/fwlink/?LinkID=254653.

Privacy information can be found at https://privacy.microsoft.com/en-us/

Microsoft and any contributors reserve all other rights, whether under their respective copyrights, patents, or trademarks, whether by implication, estoppel or otherwise.

objc-guide's People

Contributors

cy255 avatar dependabot[bot] avatar gvasquez11 avatar harrieshin avatar markavitale avatar microsoft-github-operations[bot] avatar microsoftopensource avatar nodes11 avatar saadnajmi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

objc-guide's Issues

Property Patterns

Convention

  • Properties should maintain state
  • No mutable collections in properties
  • Property accessor methods should have minimal, predictable side effects
  • When overriding the getter, we almost certainly want to override the setter
  • If overriding the setter, you need to override the getter unless the setter override calls super

Rationale

Different rationales per convention, but generally these are good patterns to follow to make your code more maintainable and match the way Apple use properties.

Example

TBD

Classes/Naming page has a typo in the example code

What convention is being referenced?

Classes/Naming.md

How do you propose this convention could be improved? Please provide rationale and examples if possible.

Current code has a typo of @nd instead of @end, it should be fixed.

@interface XYZObject : NSObject // good: XYZ three letter prefix
@nd

Property Naming

Convention

  • First letter should be lower case
  • Use camel case
  • No underscores
  • BOOL properties have special names (e.g., setHidden & isHidden:)
  • Getters don't begin with the word "get"
  • Setters do begin with the word "set"
  • Category Properties - point to the existing category naming section

Rationale

Matches Apple's recommendations and style

Example

TBD

Blocks

I was asked if we had anything about blocks in here. We should probably add this?
No idea what the convention should be :D I always go here to remind myself how to use em

Convention

Rationale

Example

Curly Braces

Convention

Use curly braces for single line scope statements, such as if and for statements

Rationale

Adding curly braces for single line logic statements prevents errors in maintenance and refactoring over time. These types of refactoring issues around single-line if statements being refactored for multiple lines have caused real-world regressions.

Example

https://www.imperialviolet.org/2014/02/22/applebug.html

Common Initializers

Convention

When there is more than one way to init an object (e.g. initWithFrame, initWithCoder) and these perform common routines (such as setting ivars, sizing, etc.), consider using c-style function to perform to handle this vs. an Obj-C function.

Rationale

Particularly if the name is a common one (e.g. initCommon:, etc.), there is some risk that the function could be overridden without calling super.

Example

@implementation XYZView

void XYZCommonInit(XYZView *self) {
  // ...
}

- (instancetype)initWithFrame:(CGRect)frame {
    // ...
   XYZCommonInit(self);
   return self;
}
- (instancetype)initWithCoder:(NSCoder *)coder {
    // ...
   XYZCommonInit(self);
   return self;
}
@end

Note:
Could put a bit of discussion in the rationale about how this truly applies to any private selector but in practice we have hit the common initializer case more than anything else. We can even add that if shipping a framework, private functionality of anything publicly exposed for subclassing should potentially avoid private selectors and stick to C-style functions to prevent clients of your framework from unintentionally overriding private functionality.

Application Framework Anti-Patterns

Convention

  • Don't make assumptions about a reverse-dependency's implementation. AKA, don't assume NSApp or NSApp's delegate conforms to a given protocol (even checking for conformance is wrong, because you shouldn't know about
  • Provide a delegate property in this case, don't directly access the application delegate. The application delegate could be your delegate, but you shouldn't know or care, only the Application Delegate would know or care in that case.

Rationale

These types of reverse dependencies make it harder to refactor and reorganize code and make components inherently less reusable.

Example

TBD

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.