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

Make your code shine with Static Analysis and S...

Make your code shine with Static Analysis and SonarQube

Here we will cover different static lint checks for code analysis for the Kotlin Jetpack Compose. That will helps any developer team size to validate the code quality before having the code review. On another side, Sonarqube help to identify the code quality in depth. That will identify the Code smells, Duplication, vulnerabilities, Major issues, Blocker issues, security Issues, and more. Ultimately the goal of the developer should be to make a flag PASS on SonarQube. That indicates your code is aligned with best practices and easy to adopt by any other team members.

Approach:

What is code review and Why it is required?
Code Review Terminologies
Static Code Analysis in Jetpack Compose 👨‍💻
SonarQube Code Review automation for Android and Flutter 👨‍💻
Summary & Conclusion
Q&A

Avatar for chintan khetiya

chintan khetiya

December 14, 2022
Tweet

Other Decks in Technology

Transcript

  1. 1. What is Manual Code Review? 2. What is Static

    Code Analysis? 3. Why Static Code Analysis? This talk
  2. 1. What is manual code review? 2. What is static

    code analysis? 3. Why Static Code Analysis? 4. How to do Static Code Analysis? This talk
  3. 1. What is Manual Code Review? 2. What is Static

    Code Analysis? 3. Why Static Code Analysis? 4. How to do Static Code Analysis? 5. Static Code Analysis In Jetpack Compose 󰞵 This talk
  4. 1. What is Manual Code Review? 2. What is Static

    Code Analysis? 3. Why Static Code Analysis? 4. How to do Static Code Analysis? 5. Static Code Analysis in Jetpack Compose 󰞵 6. Automate your code analysis using Sonarqube This talk
  5. What is static code analysis? • A method of debugging

    that is done by automatically examining the source code without having to execute the program.
  6. What is static code analysis? • A method of debugging

    that is done by automatically examining the source code without having to execute the program. • Identify the patterns in the code and detect possible issues in the quality of the code.
  7. • Write bug-free code. • Following common code practices •

    Better resource utilization Why Static Code Analysis?
  8. Twitter's Jetpack Compose Rules A set of custom ktlint rules

    to ensure that your composables don't fall into common pitfalls, that might be easy to miss in code reviews. https://twitter.github.io/compose-rules/
  9. • Set of Compose static checks to start with same

    patterns. • Detect as many potential issues as we can • Show errors prior to code review Why Twitter's Jetpack Compose Rules?
  10. kotlinter-gradle Painless Gradle plugin for linting and formatting Kotlin source

    files using the awesome ktlint engine. https://github.com/jeremymailen/kotlinter-gradle
  11. // Root build.gradle.kts plugins { id("org.jmailen.kotlinter") version "3.12.0" apply true

    } buildscript {...} subprojects { apply(plugin = "org.jmailen.kotlinter") } Install kotlinter-gradle Step 1:
  12. // Root build.gradle.kts plugins { id("org.jmailen.kotlinter") version "3.12.0" apply true

    } buildscript { dependencies { classpath "com.twitter.compose.rules:ktlint:<version>" } } Install Twitter Compose Rules Step 2:
  13. ./gradlew formatKotlin: format Kotlin source code according to ktlint rules

    or warn when auto-format not possible. ./gradlew lintKotlin: report Kotlin lint errors and by default fail the build. kotlinter-gradle adds these gradle tasks
  14. Automate process! if ! ./gradlew lintKotlin ; then printf 1>&2

    "\nlintKotlin found problems, running formatKotlin; commit the result and re-push" $GRADLEW formatKotlin exit 1 fi Step 1: Create shell script using gradle tasks
  15. Git Hooks tasks.register("installGitHook", Copy::class) { from("${rootProject.rootDir}/scripts/git-hooks") into("${rootProject.rootDir}/.git/hooks") fileMode = 7

    * 64 + 7 * 8 + 7 } Step 2: Install pre-commit git hook https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks
  16. ✅ Set of Compose static checks to start with same

    patterns. ✅ Detect as many potential issues as we can ✅ Show errors prior to code review It’s helps. right?
  17. • Assign Bugs • Invite developers • Track bug by

    active/inactive status • Check list of rules • Track project all the time to maintain the quality • Get Code quality Report at single Dashboard Why?
  18. SonarLint provides immediate feedback in your IDE as you write

    code so you can find and fix issues before a commit. Solution Stages
  19. ड े वलपम ें ट क े साथ भी, ड

    े वलपम ें ट क े बाद भी
  20. Quality Gates keep code with issues from being released to

    production. Always know your code health Passed Failed
  21. Types of Issues Code Smell 03 A maintainability issue that

    makes your code confusing and difficult to maintain. Vulnerability 02 A point in your code that's open to attack. Bug 01 A coding mistake that can lead to an error or unexpected behavior at runtime.
  22. BLOCKER: Bug with a high probability to impact the behavior

    of the application in production. Example: a memory leak, or an unclosed JDBC connection
  23. CRITICAL: Either a bug with a low probability to impact

    the behavior of the application in production or an issue that represents a security flaw. Example: An empty catch block or SQL injection
  24. MAJOR: A quality flaw that can highly impact the developer's

    productivity. Example: An uncovered piece of code, duplicated blocks, or unused parameters are examples of MAJOR issues
  25. MINOR: A quality flaw that can slightly impact the developer's

    productivity. Example: Lines should not be too long, and "switch" statements should have at least 3 cases
  26. Key Learnings • What is manual code review? • What

    is static code analysis? • How to implement with Compose? • Code analysis using Sonarqube • Understand the types and stages of issue • How to automate code review process? • Sonar for Android and Flutter • Hands-On