Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Be the Quality You Want to See in Your App [Swift Edition]

Be the Quality You Want to See in Your App [Swift Edition]

This talk has been given on Thursday, June 16th at AltConf 2016.

Once upon a time, back in the dark ages of Objective-C, monitoring the quality metrics of a codebase was a cumbersome operation, which involved the setup of obscure tools and easily produced unreliable results.

Times have changed and, just a few months after the announcement of Swift, it is now straightforward for iOS and macOS developers to improve their toolbelts and analyse the evolution of their code throughout the weeks.

During this talk you will discover why and how to monitor your codebase’s health, by leveraging metrics such as code complexity, duplication, coverage and others, by adopting open-source tools such as Fastlane, Lizard, SwiftLint, Slather, and the Swift SonarQube plugin.

Simone Civetta

June 16, 2016
Tweet

More Decks by Simone Civetta

Other Decks in Programming

Transcript

  1. Cyclomatic complexity func example(a: Int, b: Int, c:Int, d:Int) {

    // 1 if a == b { // 2 print("I") } else if (c == d) { // 3 for index in 1...5 { // 4 print("Have") } } else { // 5 switch c { case 1: // 6 print("No") case 2: // 7 print("Idea") default: // 8 print("What I'm doing") } } }
  2. NPath complexity func example(a:Int, b:Int) { if a > 10

    { print(1) } else { print(2) } if a > b { print(3) } else { print(4) } } // Result: 4
  3. SwiftLint ☞ Static analysis ☞ via AST... ☞ ...and Regular

    Expressions ☞ Supports custom rules ☞ Detects cyclomatic complexity
  4. Slather ☞ Reads Xcode Code Coverage information ☞ Outputs to

    a number of different formats (included Cobertura) ☞ Supports a plethora of systems, such as Coveralls, Codecov, etc
  5. Slather $ gem install slather $ slather coverage [FORMAT] -b

    [BUILD_DIR] --scheme [SCHEME] [PROJECT]
  6. SonarQube ☞ Acts as an hub for a variety of

    code metrics ☞ Code Coverage ☞ Code Issues ☞ SCM ☞ Complexity (Project-wide) ☞ Duplications
  7. SonarQube Swift plugin - Installing a. Build from sources: $

    cd path/to/swift/plugin $ ./build-and-deploy.sh b. Or download pre-built jar file into $SONARQUBE_HOME/extensions/plugins
  8. 5.1 Sonar: sonar-project.properties sonar.projectKey = MyProject sonar.projectName = MyProject sonar.language

    = swift sonar.sources = MyProject # Sources Folder sonar.tests = MyProjectTests # Test Sources Folder # Report paths sonar.junit.reportsPath = reports/ sonar.swift.lizard.report = reports/lizard-report.xml sonar.swift.coverage.reportPattern = reports/cobertura.xml sonar.swift.SwiftLint.report = reports/*SwiftLint.txt
  9. Complete example platform :ios do desc "Collect all the metrics"

    lane :metrics do scan(scheme: "MyProject", code_coverage: true, xcargs: "-derivedDataPath ./DerivedData", output_directory: "./reports") slather(cobertura_xml: true, jenkins: true, scheme: "MyProject", output_directory: "./reports", build_directory: "./DerivedData", binary_basename: "MyProject", proj: "./MyProject.xcodeproj") sh("cd .. && lizard ./MyProject -l swift --xml > ./reports/lizard-report.xml") swiftlint(output_file: "./reports/swiftlint.txt", ignore_exit_status: true) sonar(project_version: Time.new.strftime("%Y.%m.%d.%H.%M")) end end
  10. Why should we use them? ☞ understand which components need

    reworking ☞ identify potential risks ☞ track progress ☞ have an overall view of large systems
  11. THE USE OF ANY COPYRIGHTED MATERIAL IS USED UNDER THE

    GUIDELINES OF "FAIR USE" IN TITLE 17 § 107 OF THE UNITED STATES CODE. SUCH MATERIAL REMAINS THE COPYRIGHT OF THE ORIGINAL HOLDER AND IS USED HERE FOR THE PURPOSES OF EDUCATION, COMPARISON, AND CRITICISM ONLY. NO INFRINGEMENT OF COPYRIGHT IS INTENDED.