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

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

Jan 26, 2017

Download

Technology

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Be armed to the teeth to maintain a high quality iOS code

Be armed to the teethto maintain a high quality iOS code

Anastasia Kazakova

Page 2: Be armed to the teeth to maintain a high quality iOS code

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?

Page 3: Be armed to the teeth to maintain a high quality iOS code

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

Page 4: Be armed to the teeth to maintain a high quality iOS code

What is code quality?

Page 5: Be armed to the teeth to maintain a high quality iOS code

WTFs/minute metric

Page 6: Be armed to the teeth to maintain a high quality iOS code

Have you ever encountered this?

Page 7: Be armed to the teeth to maintain a high quality iOS code

… or this …

Page 8: Be armed to the teeth to maintain a high quality iOS code

…or this?

Page 9: Be armed to the teeth to maintain a high quality iOS code

GOTO FAIL?

Page 10: Be armed to the teeth to maintain a high quality iOS code

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

• …

Page 11: Be armed to the teeth to maintain a high quality iOS 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

Page 12: Be armed to the teeth to maintain a high quality iOS code

"We need tools” Bjarne Stroustrup at CppCon2015

Page 13: Be armed to the teeth to maintain a high quality iOS code

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

Page 14: Be armed to the teeth to maintain a high quality iOS code

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 :(

Page 15: Be armed to the teeth to maintain a high quality iOS code

Armed to the teeth #1: Code style

Clang-Format

Page 16: Be armed to the teeth to maintain a high quality iOS code

Armed to the teeth #1: Code style

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

• Some check in SwiftLint

Page 17: Be armed to the teeth to maintain a high quality iOS code

Armed to the teeth #1: Code style

AppCode

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

• Reformat action

• On-the-fly

• Format on commit stage

Page 18: Be armed to the teeth to maintain a high quality iOS code

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.

Page 19: Be armed to the teeth to maintain a high quality iOS code

Armed to the teeth #2: Generate code

AppCode:

• Generate menu

• Live Templates

• Create from usage

• Override/Implement

• File Templates

Page 20: Be armed to the teeth to maintain a high quality iOS code

Armed to the teeth #3: Static analysis

Tools:

• Guidelines/rules

• Configuration settings

• On-the-fly mode

• Quick fixes

Page 21: Be armed to the teeth to maintain a high quality iOS code

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!

Page 22: Be armed to the teeth to maintain a high quality iOS code

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

Page 23: Be armed to the teeth to maintain a high quality iOS code

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!

Page 24: Be armed to the teeth to maintain a high quality iOS code

Armed to the teeth #3: Static analysis

OCLint and SwiftLint

Page 25: Be armed to the teeth to maintain a high quality iOS code

Armed to the teeth #3: Static analysis

OCLint and SwiftLint

Page 26: Be armed to the teeth to maintain a high quality iOS code

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

Page 27: Be armed to the teeth to maintain a high quality iOS code

Armed to the teeth #3: Static analysis

AppCode: complicated cases, bulk mode

Page 28: Be armed to the teeth to maintain a high quality iOS code

Armed to the teeth #3: Static analysis

AppCode: quick fixes, DFA

Page 29: Be armed to the teeth to maintain a high quality iOS code

Armed to the teeth #3: Static analysis

AppCode:

Checks on commit stage

• formatter

• code analysis

• TODO

• imports

Page 30: Be armed to the teeth to maintain a high quality iOS code

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

Page 31: Be armed to the teeth to maintain a high quality iOS code

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

Page 32: Be armed to the teeth to maintain a high quality iOS code

Armed to the teeth #5: Unit testing

AppCode: unit tests code generation

Page 33: Be armed to the teeth to maintain a high quality iOS code

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

Page 34: Be armed to the teeth to maintain a high quality iOS code

Thanks for listening!

[email protected]

@anastasiak2512

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