<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Mikhail Levchenko</title>
    <description>The latest articles on DEV Community by Mikhail Levchenko (@mishkun).</description>
    <link>https://dev.to/mishkun</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F160501%2F0dbdf3be-9caf-4a35-9e55-d9f32dc74f7b.jpeg</url>
      <title>DEV Community: Mikhail Levchenko</title>
      <link>https://dev.to/mishkun</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mishkun"/>
    <language>en</language>
    <item>
      <title>Carving the Path to Modularity: A Lobzik Tool Case Study on the ProtonMail Android App</title>
      <dc:creator>Mikhail Levchenko</dc:creator>
      <pubDate>Sat, 01 Jul 2023 15:39:07 +0000</pubDate>
      <link>https://dev.to/mishkun/carving-the-path-to-modularity-a-lobzik-tool-case-study-on-the-protonmail-android-app-mid</link>
      <guid>https://dev.to/mishkun/carving-the-path-to-modularity-a-lobzik-tool-case-study-on-the-protonmail-android-app-mid</guid>
      <description>&lt;h2&gt;
  
  
  Premature and overdue modularisation problems
&lt;/h2&gt;

&lt;p&gt;When you begin building your project, there may be a strong temptation to modularise it right from the start in order to save costs down the line. However, I believe that premature modularisation not only drains your resources but can also hinder the long-term success of your project. At the early stages, your product vision may not be fully formed, and it can undergo significant changes. The module boundaries that you establish initially can quickly become outdated as the project evolves through numerous iterations and achieves success.&lt;/p&gt;

&lt;p&gt;However, as your team grows and the number of features starts to accumulate, the tech debt of modularisation can send a chill down your spine. You begin to notice an increase in git conflicts and heisenbugs caused by a lack of clear separation of concerns within your application. Eventually, you reach a point where you declare "enough is enough" and decide to modularise your monolith. But the question remains: where do you begin unraveling this tangled mess? You don't want to stop developers from pumping out new features for your project, so you need to pinpoint the most impactful areas that can be extracted with minimal effort. But how can you achieve this without spending a lot of time delving into the void your codebase had become?&lt;/p&gt;

&lt;h2&gt;
  
  
  Introducing Lobzik: The Modularisation Toolkit
&lt;/h2&gt;

&lt;p&gt;Having been tasked with modularising the codebase of my work project, with a whopping 200kloc monolith, I embarked on a quest to find a way to reason about modularising this chonky boy. Being an enthusiast of graphs, I was interested in the network of dependencies within the monolith. Soon enough, I discovered that this network could serve as a good place for for community detection algorithms, which could reveal structures that looked like modules. After weeks of experimentation, I successfully devised a way to extract the dependency graph, carefully selected the most suitable community detection methods, and came up with the tricks to yield optimal results.&lt;/p&gt;

&lt;p&gt;These insightful findings led to the birth of my pet-project: the &lt;a href="https://github.com/Mishkun/lobzik"&gt;Lobzik Gradle Plugin&lt;/a&gt;. Rather than relying on complex GUI graph toolkits like Gephi or spinning up Jupyter Notebooks filled with NetworkX Python code, you can effortlessly integrate my tool into your build pipeline. Lobzik provides guidance, pointing you towards the optimal path for modularising your project. However, this tool needs some knowledge to operate, so let this article serve as your guide to use this tool correctly.&lt;/p&gt;

&lt;h2&gt;
  
  
  Applying Lobzik to the ProtonMail Android App
&lt;/h2&gt;

&lt;p&gt;For the reference project, I've chosen the &lt;a href="https://github.com/ProtonMail/proton-mail-android"&gt;ProtonMail Android App&lt;/a&gt;, which is one of the largest open-source Android apps that has not been modularised yet. With over 50kloc in the main module, it truly represents a monolith that is worth modularising.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;cloc app/src/main &lt;span class="nt"&gt;--include-lang&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;Kotlin,Java

github.com/AlDanial/cloc v 1.96  &lt;span class="nv"&gt;T&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;0.39 s &lt;span class="o"&gt;(&lt;/span&gt;2388.4 files/s, 249735.1 lines/s&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="nt"&gt;-------------------------------------------------------------------------------&lt;/span&gt;
Language                     files          blank        comment           code
&lt;span class="nt"&gt;-------------------------------------------------------------------------------&lt;/span&gt;
Kotlin                         766           8867          16675          52105
Java                           156           2371           3381          13006
&lt;span class="nt"&gt;-------------------------------------------------------------------------------&lt;/span&gt;
SUM:                           922          11238          20056          65111
&lt;span class="nt"&gt;-------------------------------------------------------------------------------&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Setting up Lobzik
&lt;/h3&gt;

&lt;p&gt;To start using Lobzik, we need to apply the &lt;code&gt;xyz.mishkun.lobzik&lt;/code&gt; plugin in the root &lt;code&gt;build.gradle.kts&lt;/code&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="nf"&gt;plugins&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="c1"&gt;// ...&lt;/span&gt;
   &lt;span class="nf"&gt;id&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"xyz.mishkun.lobzik"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;version&lt;/span&gt; &lt;span class="s"&gt;"0.6.0"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, we can set up the basic configuration in the same &lt;code&gt;build.gradle.kts&lt;/code&gt; file as shown below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="nf"&gt;lobzik&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;monolithModule&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;":app"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;packagePrefix&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"ch.protonmail.android"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;variantName&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"betaDebug"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we set the name of our monolith module (notice the ":" in the module name!), the name of the variant we will be analyzing, and the package prefix of our classes. With this configuration, only the code in packages starting with &lt;code&gt;ch.protonmail.android&lt;/code&gt; inside the &lt;code&gt;:app&lt;/code&gt; module will be checked, using the &lt;code&gt;betaDebug&lt;/code&gt; variant. This is crucial for our tool to work, because we don't want to deal with all of the library dependencies and standard kotlin library messing our dependency graph.&lt;/p&gt;

&lt;h3&gt;
  
  
  Running Lobzik for the first time
&lt;/h3&gt;

&lt;p&gt;Now that we are all set up, we can run Lobzik for the first time using the command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;./gradlew lobzikReport
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If everything was set up correctly, you will find &lt;code&gt;build/reports/lobzik/analysis/report.html&lt;/code&gt; file in your project root. Now let's take a closer look at how to interpret this report.&lt;/p&gt;

&lt;h3&gt;
  
  
  Interpreting the Lobzik Report
&lt;/h3&gt;

&lt;p&gt;Lobzik report consists of four sections:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Core Candidates&lt;/li&gt;
&lt;li&gt;Monolith Modules Table&lt;/li&gt;
&lt;li&gt;Module Graphs&lt;/li&gt;
&lt;li&gt;Whole Graph&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Monolith Modules Table
&lt;/h4&gt;

&lt;p&gt;The first thing that catches our eye is the Monolith Modules Table. It lists all of the modules detected by Lobzik. They can be sorted by several metrics: coductance, cut and monolithCut.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KDQRrO26--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ozsaoq8nctxym0ortq4w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KDQRrO26--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ozsaoq8nctxym0ortq4w.png" alt="Monolith Modules Table screenshot" width="800" height="664"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;conductance&lt;/strong&gt; score is the core metric of this part of the report, as it indicates the benefit-to-effort ratio of extracting modules. A lower score is preferable, with a score of 0 indicating that extracting the module requires virtually no effort since it has no dependencies on other modules.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;cut&lt;/strong&gt; and &lt;strong&gt;monolithCut&lt;/strong&gt; scores show us how many dependencies should be broken to successfully extract the module. It helps to refine the estimates on how much effort we need to extract this module.&lt;/p&gt;

&lt;p&gt;The names of the modules are automatically generated from their classes using the &lt;a href="https://en.wikipedia.org/wiki/Tf%E2%80%93idf"&gt;TF-IDF&lt;/a&gt; method. Clicking on a module name will take us to the detailed report in the Module Graphs section.&lt;/p&gt;

&lt;h4&gt;
  
  
  Module Graphs
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--yj91zptn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wsmaj0yq4z021kdz1jxa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--yj91zptn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wsmaj0yq4z021kdz1jxa.png" alt="Module Graphs section screenshot with module dependency graph and list of its classes" width="800" height="540"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This section contains per-module detailed reports, each presenting three subsections:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Dependency graph of the module and its neighbourhood&lt;/li&gt;
&lt;li&gt;List of all of classes belonging to this module&lt;/li&gt;
&lt;li&gt;List of dependencies that need to be broken to extract this module&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Whole Graph
&lt;/h4&gt;

&lt;p&gt;This section at the bottom of the report represents the module dependency graph, which can help identify modules that are relatively easier to extract due to their fewer dependencies on the rest of the project.&lt;/p&gt;

&lt;h2&gt;
  
  
  The "Star" problem
&lt;/h2&gt;

&lt;p&gt;A careful reader may notice that I have omitted the first section of the report, called Core Candidates. This section is collapsed under a spoiler, but it plays a crucial role in enhancing the report's effectiveness. To fully comprehend its value, let's explore what I refer to as the "star" problem.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--WTb66fgg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7x16fbmyosas020v6lig.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--WTb66fgg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7x16fbmyosas020v6lig.png" alt="Graph of dependencies with many connections to the ListUtil.kt node" width="800" height="475"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's consider a scenario where we have a class called &lt;code&gt;ListUtil.kt&lt;/code&gt; that contains various list utilities. This class is heavily used throughout our codebase, resulting in numerous connections to other nodes in the network. Due to the high degree of connections, our community detection algorithm of choice, the Louvain method, may mistakenly identify this class as the core of a large community. It's important to note that community detection algorithms were initially designed for social networks, where such hubs represent a significant community led by an outstanding individual.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--f9eL7T-x--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/da2jdrhu510uy2tw8xoe.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--f9eL7T-x--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/da2jdrhu510uy2tw8xoe.png" alt="Same graph as above but with ListUtil.kt removed to reveal modularised structure" width="800" height="401"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;However, for a codebase modularisation problem this class should be extracted to the core modules. By doing so, we can reveal a better modularisation path for the rest of the code, as depicted in the image above. To assist in visualizing the benefits of extracting such classes, Lobzik offers the &lt;code&gt;ignoredClasses&lt;/code&gt; configuration parameter which accepts a list of regexes of class names that should be excluded from the analysis.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="nf"&gt;lobzik&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// ...&lt;/span&gt;
    &lt;span class="n"&gt;ignoredClasses&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"^ListUtils$"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But how we identify such classes, you may ask? These classes are commonly found in sections responsible for &lt;strong&gt;Dependency Injection (DI)&lt;/strong&gt; and &lt;strong&gt;Navigation&lt;/strong&gt;, as they serve as the glue that connects otherwise loosely coupled features code.  It is a good choice to ignore your &lt;strong&gt;Application&lt;/strong&gt; class and &lt;strong&gt;well-known utility classes&lt;/strong&gt; too. But can we automatically identify more core classes if we have already eliminated the obvious ones? This is where the Core Candidates section of the report becomes valuable.&lt;/p&gt;

&lt;h3&gt;
  
  
  Core Candidates
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--SUdnsgWN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m7fqd9jsw5zwi6ib2n3b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--SUdnsgWN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m7fqd9jsw5zwi6ib2n3b.png" alt="Core Candidates section screenshot" width="757" height="674"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The Core Candidates section presents a table that consists of the top 95 percentile of classes based on Degree or Authority metrics. Thoroughly reviewing this list can help identify the classes that should be excluded from the report. In the case of ProtonMail, the following classes might be considered for elimination:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="nf"&gt;lobzik&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// ...&lt;/span&gt;
    &lt;span class="n"&gt;ignoredClasses&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="s"&gt;".*UserManager$"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s"&gt;".*Constants$"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s"&gt;".*ProtonMailApiManager$"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s"&gt;".*Util.*"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s"&gt;".*ProtonMailApplication$"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s"&gt;".*ResponseBody$"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s"&gt;"Base.*"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s"&gt;".*Module"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s"&gt;"^Message$"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s"&gt;"^User$"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s"&gt;"^ProtonMailApi$"&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By eliminating these classes from the report, we can improve our algorithm's performance, measured by the modularity score, going from 0.586 to 0.684. A great improvement! Now we can use Lobzik report to start extracting each of detected 24 modules one by one.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;You can find a fork of ProtonMail client with integrated Lobzik on my &lt;a href="https://github.com/Mishkun/proton-mail-android-lobzik-showcase"&gt;github&lt;/a&gt;. I hope you will enjoy using Lobzik for modularising you codebase. I encourage you to try it and don't hesitate to submit any issues to the project's &lt;a href="https://github.com/Mishkun/lobzik"&gt;github&lt;/a&gt;&lt;/p&gt;

</description>
      <category>android</category>
      <category>kotlin</category>
      <category>modularization</category>
      <category>gradle</category>
    </item>
  </channel>
</rss>
