5 Kotlin Tips for a Cleaner Codebase
<p>Kotlin offers a lot of useful concepts and structures that make it easy to write concise code. But when working in a team, the primary goal should be to write code that is easy to read, understand and maintain. We’re going to take a look at a few good practices that should maintain a healthier codebase.</p>
<p><img alt="" src="https://miro.medium.com/v2/resize:fit:630/1*XoAJ5gpuIAbpjEnZMcIr6Q.jpeg" style="height:394px; width:700px" /></p>
<blockquote>
<p>Note: these are just suggestions and do not imply this is the correct way to do things. The style of the code you write is down to your and your team’s preference.</p>
</blockquote>
<h2>1. Pay attention to the visibility of classes</h2>
<p>Pay close attention to the visibility modifier that you apply to new classes and functions. Classes are public by default, which means that the class will be accessible from any other module that depends on this one.</p>
<p><strong>Supported visibilities for classes:</strong></p>
<ul>
<li><code>public</code>: default modifier, visible to all the classes inside the module plus any module that depends on this module</li>
<li><code>internal</code>: visible to all the classes inside the module, but not outside the module</li>
<li><code>private</code>: only visible inside the file or class</li>
</ul>
<p>Additionally, for class members (functions and properties), there is a <code>protected</code> modifier, that makes them visible to any class that extends this one.</p>
<p>Try to use the <code>internal</code> modifier whenever possible for classes to limit their visibility to only inside the current module. This way you reduce the external API of the module.</p>
<p><a href="https://medium.com/@domen.lanisnik/5-kotlin-tips-for-a-cleaner-codebase-3582f2e4e2af">Click Here</a></p>