Weekly Tech Tidbits #2: Disagree and Commit, Spotless, and Error Handling in Kotlin.
Disagree and Commit, Keep your Code Tidy and improve your error handling!
Hi, it's ! 👋 Welcome to this week's edition of Weekly Tech Tidbits. In today's post, we will cover three interesting topics:
the concept of Disagree and Commit 🗣️✅
how to keep your code tidy 🧼
Typed Error Handling in Kotlin ⚠️🎥
If you missed the previous issue, find it here.
Enjoy reading and feel free to share your thoughts in the comments below. Don't forget to subscribe for more insights and share this post with your network! 📢
Disagree and Commit 🗣️✅
In any collaborative environment, disagreements are inevitable. However, the principle of "disagree and commit" provides a powerful attitude for maintaining momentum and unity.
The idea is simple: after thoroughly considering all viewpoints, a decision is made. Even if you initially disagreed with the chosen direction, once the decision is finalized, you fully commit to its implementation. You leave all your doubts behind and accept that your suggestion was not accepted.
However, this only works effectively if all opinions on the issue are heard and taken seriously prior to making the decision; otherwise, it can lead to entrenched conflicts, negative sentiments, and hidden rivalries.
It's important to work on yourself to adopt this attitude, cultivating humility and avoiding stubbornness to ensure the process is genuine and effective.
Several companies live by this principle, including GitLab, Netflix, and most prominently Amazon. Even if your company isn't as big as Amazon1, adopting this mindset can still drive significant improvements in team collaboration.
Keep your Code Tidy with Spotless 🧼
Maintaining a consistent code style is essential for noise-free merges and readable code, yet synchronizing these styles across a team can be a hassle. The key is to automate this process, allowing developers to focus on coding and not on manual formatting.
Spotless addresses this need by enabling you to write code without worrying about style inconsistencies until it’s time to merge. Spotless can be easily integrated to your build system as plenty plugins exist.
Here's how it works:
Define which formatters and linters you want to use (e.g. ktlint and diktat for Kotlin). This is only done once in your build system, so it is the same for everyone.
Write your code without focusing on style.
Before opening a pull request, run
spotless apply
to ensure your code adheres to the predefined style guidelines.Finally, run the
spotless check
before merging to maintain consistency on the main branch.
This approach is challenging to implement 1:1 in a trunk-based development style, but you can use pre-commit hooks to automatically run Spotless before each commit, ensuring consistency throughout the development process.
By integrating spotless into your workflow, you ensure that your team’s code remains clean and consistent, enhancing both code quality and team efficiency.
Typed Error Handling in Kotlin 🎯🎥
Typed error handling is an alternative to the traditional try-catch
method, offering a more structured way to manage errors in your code. This approach uses types to explicitly represent possible errors, enhancing code readability and maintainability by ensuring that all potential failures are clearly defined within the type system.
Kotlin is well-suited for typed error handling due to its type system and functional programming capabilities. Here is a short example of typed error handling:
// try-catch
fun parseNumber(str: String): Int {
return try {
str.toInt()
} catch (e: NumberFormatException) {
-1 // or some error handling logic
}
}
// typed error handling
sealed class ParseError {
object NotANumber : ParseError()
}
fun parseNumberTyped(str: String): Either<ParseError, Int> {
return str.toIntOrNull()?.right() ?: ParseError.NotANumber.left()
}
For a practical and in-depth look at functional error handling in Kotlin, I highly recommend the video "Functional Error Handling – A Practical Approach" by Bas de Groot.
This talk provides a short and useful way to understand typed errors, addressing the problems they solve, how to implement them, and leveraging the Arrow library.
Wrapping Up
Thank you for reading this week's insights. To summarize, adopting 'disagree and commit' can enhance team unity, using Spotless ensures clean code, and leveraging typed error handling in Kotlin can make your code more reliable.
I'd love to hear your thoughts, questions, and feedback in the comments below. 💬 Don't forget to subscribe for more insights 🔔 and share this post with your network! 📢
Stay tuned for next week's post!
However if your company IS indeed Amazon, let me know in the comments ;-)