34
Be armed to the teeth to maintain a high quality iOS code Anastasia Kazakova

Be armed to the teeth to maintain a high quality iOS code

Embed Size (px)

Citation preview

Be armed to the teethto maintain a high quality iOS code

Anastasia Kazakova

Program vs Product

• ACM-ICPC: Solve quicker than others, pass tests, start another task.

• Real word: How often do you rewrite the project from scratch?

What is code quality?

Some say:Easy to understand

Easy to maintainEasy to useWorks right

Some say:high quality == meets

requirements

Some say (CISQ):ReliabilityEfficiencySecurity

MaintainabilitySize

What is code quality?

WTFs/minute metric

Have you ever encountered this?

… or this …

…or this?

GOTO FAIL?

Possible problems• Dead code

• Copy/paste, duplicated code

• Hiding scope

• Too much happening at one place

• Code style inconsistency

• Patterns violations

• Bad coding practices

• Over-generalized code

• …

What do we have to do?

1. Follow the code style

2. Generate code

3. Run static analysis

4. Refactor

5. Run unit tests

"We need tools” Bjarne Stroustrup at CppCon2015

Armed to the teeth #1: Code style

• Apple’s coding guidelines for Cocoa

• raywenderlich.com:

• https://github.com/raywenderlich/objective-c-style-guide

• https://github.com/raywenderlich/swift-style-guide

• GitHub:

• https://github.com/github/objective-c-style-guide

• https://github.com/github/swift-style-guide

• Spotify

• https://github.com/spotify/ios-style

Armed to the teeth #1: Code style

Clang-Format:

• Stand-alone, tools integration.

• Plugin for Xcode: https://github.com/travisjeffery/ClangFormat-Xcode with option to enable format on save.

• Indents, braces, breaks, line length, base styles, aligns, and much more.

• Objective-C, no Swift :(

Armed to the teeth #1: Code style

Clang-Format

Armed to the teeth #1: Code style

• Pre-commit hooks: ocstyle, SwiftFormat, SpaceCommander (uses Clang-Format) & more on GitHub.

• Some check in SwiftLint

Armed to the teeth #1: Code style

AppCode

• Settings for Objective-C, C, C++, Swift.

• Reformat action

• On-the-fly

• Format on commit stage

Armed to the teeth #2: Generate code

• Xcode pre-generates some methods when creating files/classes.

• Xcode code snippets for Objective-C and Swift.

• Override/Implement code generation on completion in Xcode.

• There were some 3d-party tools like xobjc on GitHub.

Armed to the teeth #2: Generate code

AppCode:

• Generate menu

• Live Templates

• Create from usage

• Override/Implement

• File Templates

Armed to the teeth #3: Static analysis

Tools:

• Guidelines/rules

• Configuration settings

• On-the-fly mode

• Quick fixes

Armed to the teeth #3: Static analysis

Clang analyzer:

• Open source, part of Clang project

• Integrated into Xcode

• Basic checks, dead code, API checks, language specific checks

But don’t forget to switch them all on!

Armed to the teeth #3: Static analysis

OCLint: https://github.com/oclint

• Metric-based defects:

• complicated code;

• redundant code;

• long methods, long parameters lists, etc.;

• bad practices (like parameter reassignment).

• 58 rules that can be customized per project

• Integrated with clang analyzer

• Can be used inside the Xcode

Armed to the teeth #3: Static analysis

SwiftLint: https://github.com/realm/SwiftLint

• Similar to OCLint

• Limited in number of rules, planned rules:

• Code style checks (spaces, newlines);

• let over var rule; struct over classes;

• Working with optionals (like not forcing unwrapping);

• Specify access control and more.

• Contribute!

Armed to the teeth #3: Static analysis

OCLint and SwiftLint

Armed to the teeth #3: Static analysis

OCLint and SwiftLint

Armed to the teeth #3: Static analysis

AppCode

• Integrated with clang analyzer

• 60 inspections for Objective-C, 40 for C/C++, on-the-fly mode

• Settings, severity level, profiles

Armed to the teeth #3: Static analysis

AppCode: complicated cases, bulk mode

Armed to the teeth #3: Static analysis

AppCode: quick fixes, DFA

Armed to the teeth #3: Static analysis

AppCode:

Checks on commit stage

• formatter

• code analysis

• TODO

• imports

Armed to the teeth #4: Refactoring

• Xcode refactorings

• Rename, Extract (method/function), Move up/down, Create superclass, Encapsulate, Convert…

• C and Objective-C only

• AppCode refactorings

• + Inline, Safe delete, Change signature, Extract everything, Rename with non-code usages (strings, comment, xib, etc.) for Objective-C, C, C++

• Rename in Swift

• Some projects on GitHub, like Refactorial

Armed to the teeth #5: Unit testing

• XCTest

• Kiwi: BDD framework for Objective-C

• Google Test: tests for C/C++

• Specta/Expecta: TDD / BDD framework for Objective-C

• Quick/Nimble: Swift BDD framework

Armed to the teeth #5: Unit testing

AppCode: unit tests code generation

SummaryTODO:

• Follow selected code style

• Generate standard pieces of code

• Use the swiss-army knife thoughtfully to find smells

• Refactor to clean up the smells

• Cover every inch with the unit tests

Tools:

• Xcode

• Clang-Format

• OC/Swift-Lint

• AppCode

Thanks for listening!

[email protected]

@anastasiak2512

IDE for iOS/OS X dev: http://jetbrains.com/appcode, @appcode