Phase 7 · Professional & AppliedModule 40~36 min read

Clean Code, Idioms & Professional Workflow

Write idiomatic Kotlin and collaborate professionally with Git and CI.

What you'll learn

Knowing the language is one thing; writing code other professionals respect is another. This module is about craft and workflow — writing idiomatic Kotlin, keeping it clean automatically, collaborating with Git and code review, and shipping safely with CI/CD.

By the end you'll be able to:

  • Write idiomatic, expressive Kotlin
  • Enforce style automatically with ktlint and detekt
  • Collaborate with a Git branch → PR → review workflow
  • Understand CI/CD and basic security hygiene

Idiomatic Kotlin

Idiomatic Kotlin leans on everything you've learned: prefer val over var, expressions over statements, immutable data, and null safety over defensive checks. Compare a Java-style function with its idiomatic rewrite:

Before.kt
// Java-flavoured Kotlin — works, but not idiomatic
fun describe(user: User?): String {
    if (user == null) {
        return "no user"
    }
    var result = ""
    if (user.isActive == true) {
        result = "active: " + user.name
    } else {
        result = "inactive: " + user.name
    }
    return result
}
After.kt
// Idiomatic Kotlin — concise, immutable, null-safe
fun describe(user: User?): String {
    user ?: return "no user"                    // early return with Elvis
    val status = if (user.isActive) "active" else "inactive"  // if-expression
    return "$status: ${user.name}"             // string template, immutable val
}

Key idea

A short checklist for idiomatic Kotlin: default to val; use if/whenas expressions; embrace data class, scope functions, and extensions; handle absence with ?. and ?: instead of == null checks; and let the standard library (map, filter, let…) do the heavy lifting.

Formatting & linting

Don't argue about style — automate it. ktlint enforces the official Kotlin style guide and can auto-format, while detekt is a static analyzer that flags code smells, complexity, and potential bugs. Wire them into Gradle so every build stays consistent:

terminal
./gradlew ktlintCheck     # verify formatting against the style guide
./gradlew ktlintFormat    # auto-fix formatting issues
./gradlew detekt          # static analysis: find code smells & bugs

Tip

Run these locally before you commit, and again automatically in CI. Consistent formatting makes diffs smaller and reviews about logic, not whitespace — a small habit with a big payoff on a team.

Git & GitHub workflow

Professional work flows through Git. The everyday loop: create a branch for your change, make small focused commits with clear messages, push, and open a Pull Request so a teammate can review before it merges to the main branch:

terminal
git checkout -b feature/add-login    # a branch for your change
git add .
git commit -m "Add login screen"     # small, focused, well-described commit
git push -u origin feature/add-login
# then open a Pull Request so a teammate can review it

Note

Good commit messages explain why, not just what. "Fix null crash when profile image is missing" helps a future teammate (often you) far more than "fix bug." Keep commits small and each one a single logical change.

Code review & CI/CD

Code review catches bugs, spreads knowledge, and keeps quality high — approach it with humility on both sides. CI (Continuous Integration) automatically builds and tests every push, so problems surface immediately; CD (Continuous Delivery/Deployment) automates releasing that verified code. A typical CI pipeline runs ktlint, detekt, and your full test suite on every Pull Request — nothing merges unless it's green.

Security fundamentals

Every developer shares responsibility for security. The essentials:

  • Never hard-code secrets — keep API keys and passwords out of source control; use environment variables or a secrets manager
  • Validate all input — never trust data from users or the network
  • Use parameterised queries — never build SQL by string concatenation
  • Keep dependencies updated — outdated libraries carry known vulnerabilities
  • Prefer HTTPS and least privilege — encrypt in transit; grant only the access that's needed

Watch out

The most common breaches are mundane: a secret committed to Git, an unvalidated input, an out-of-date dependency. Kotlin's null safety and immutability help, but security is a habit, not a feature — build it in from the start.

Recap & quick check

Key takeaways

  • Idiomatic Kotlin favours val, expressions, immutable data, and null-safe operators over defensive code.
  • Automate style with ktlint (formatting) and detekt (static analysis) so reviews focus on logic.
  • Work in branches, make small clear commits, and merge through reviewed Pull Requests.
  • CI builds and tests every change automatically; CD automates releasing verified code.
  • Security is a habit: never commit secrets, validate input, parameterise queries, and update dependencies.

Quick check

1. Which is more idiomatic in Kotlin?

2. What does ktlint do?

3. What is the purpose of a Pull Request?

4. What does Continuous Integration (CI) do?

5. Which is a basic security must-do?

That completes Phase 7 — you now code and collaborate like a professional. All that's left is to prove it. In the final phase you'll build a portfolio of real Kotlin projects. 🚀