{"data":{"article":{"id":"bf1481d7-2c1e-5f8f-9b67-0b9fec66ad62","frontmatter":{"category":"Engineering","title":"IntelliJ IDEA plugin development - focus on UI based plugins","date":"2021-07-10","summary":"This post is in addition to the previous article about plugin development but with a focus on UI based plugin implementations.","thumbnail":{"relativePath":"pages/IntelliJ IDEA plugin development - focus on UI based plugins/logo.png","childImageSharp":{"resolutions":{"base64":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAACXBIWXMAAAsSAAALEgHS3X78AAAEyElEQVQ4y3WVe0xTVxzHz723rfZBsVeqnc4Yw0aYFSiFFZlZxnSo4w+gSBWEglTmA59jCt4WCOUxq+J4zGwaMqfJfDBi2GbMxjJfMXMzJks0Y5ngfE0mqAwrFLHtPb+de4CKf+wkv5zfOed3Pr/vOb+Te9FAzEokNV9cLvq/Nmpew8L8DIQOANJUPEdh+wGpBB8jrSldo0hd7n0RLJry0fA4bCg2RxYw5S3zm/ISnsWt1vvi8qZMxD1KLkVvb+lmJifS7hrmpJ4lCfidAy8WAqZ8dqzPSwFzIQRN+UEy95SMbxK7MhK3umQiVlnuLVQKwxcVW//SS2O94JNFFZyja3NLHyD03JSHIN5OMxHQZ5C4Fgg0CAlFAIkOAMs6gIWbiV9sQ0cgTlMl+tW7ATSV+E64y2+R9kWVDXJR226PZZSAUvPG5mpxvP0+MfjjjfTg9eh0/Nv8TPGXBdmBS3H5cC52tS8253SffNMN0BZfHdVu7AK+tC9gIIon1MdsvkXvkKojd2aT1N02ZgXDWBnmGBbLJWM5IL4oZzhQEEOMTESsHBBiAjNzz8BrVSJEb721b3taOzdWlIQien+kECfB8gHciM8NkCHOyLLC711d+MPSUkzGIM159njE5IVJ1CfVgXnZbeKbpQ9Ey84+iCy7kxUqylB8QeQoUTeatAWumteIMhKs43m42dMDFotFAgDDMHD+/HlIT88YA7IsNmUcwWmbb4hRu+4BX9mfGQI+SSiY/SBxneeflJLHZ5NKQM7IqKofOzth1qxZMK4QOjo6CDCd+izLwNLlLXhh+R2sr3oEM4SHy8bUOYz07H0daFrQNWPw57XJQG8iIQEwxmC1WiE1NRWampqgu7sbzGYzBUqniLV9gee4B3GE6zHonAMpFAh7ZPSx+sqmH4Pa6XB5++vSHcJbixbhpsZGSEtLA6PRCK2trZCTkxNSK5m+oB2/4h6WYDjcOZgcOvLDHbxyZJfuT6ifDhc3zA8qZIqXNr5sTMiPsJ/CevcoEFgwzPnETGHPBB1VeG+TwQDV2rNnC98FBmlEhUKG1So1TJ06FeRyOchkMuA47iWgzv4t5msCQGB+ldNrDCns3b6AQgGOs42LS67PnR0J+z9pCDY3N+ODhw7i9RvW08qOW8jX2k/jaTUY1M6nI0rhaSSF9e2IRlXvXKD+paJrEbVL9varp6hBGx7m53lejNDxYrhKE6TKGAYj8tiJT4Ea+5mA1o1B5Rz2KwXvnJDClszDtNKfWw9bW7KOwrYl9bAltQGKF3sgb/k+WJpzAGIcXwYiHV/j2Y7vxAjHD4FwxznQurygqQdQunw9yrIBLYV5rEdRQ/YJCty74qujhwp/gt253/R/bP/+eJPj8vvr3JdWvld95X6Spxeiq//Gc2uegMEDoHP7Iaxy5ILKOVREjjtN2k+UIlS34hgF12UeVtRln/i02ta2aWN6q37ydy9mfQdvFK61zqu4CwZnbx8v9Ddrd95PnBxDVI455dknaE9A7OSA+lWn2Orsdi7zo05uYu5V513LTKHXMDHW12Gkcvk4pTBEi6qegLptJ2nfU9CJam1tXKWtjRFWtSND1S06b3H+yiRWXg8l1Ff8y5G3x6oELyJK0cwa6TfwmK79B0KrRX19cxThAAAAAElFTkSuQmCC","width":325,"height":325,"src":"/static/b2f976ed5fa752977d8f8f9b2516534c/b3029/logo.png","srcSet":"/static/b2f976ed5fa752977d8f8f9b2516534c/b3029/logo.png 1x,\n/static/b2f976ed5fa752977d8f8f9b2516534c/8d141/logo.png 1.5x,\n/static/b2f976ed5fa752977d8f8f9b2516534c/ee72c/logo.png 2x,\n/static/b2f976ed5fa752977d8f8f9b2516534c/5dfa8/logo.png 3x"}}},"authorName":"Vadim Shchenev","authorDescription":"Vadim is an Android Developer at AUTO1 Group.","authorAvatar":{"relativePath":"pages/IntelliJ IDEA plugin development - focus on UI based plugins/avatar.png","childImageSharp":{"resolutions":{"base64":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAACXBIWXMAAAsSAAALEgHS3X78AAACyElEQVQ4y4VUO2gUURR9mZmN8YcItoKVjeXuzsxu9jOzm92NkBBTqDEmjZAEgtqIpSgI1gp+uhS6kEI7wc7GQsFCLERiISGCaGHhF02yu+M5s/cujyC4cJY379573r3n3XdNvV51xsYiJ4rKbqlUyBj8wjAYyWazp4BVYB3YAraBDeAhMBcE/l76lkrFTLVachuNyCGXEbJMrVbx6ADnaWAtn88nvu8nWBMdQcJ9Auv3wCxjWq2aC9IMSF3DzCyyG1YAs9pSYkL2melmLpfT71tKGkUlz2iZJAuCgA5dEjGAwPoV0AbuAy+tfR7YkZjb5CiXR1OutEw5jWQdCfgCnEiSxNg/7LWAj+KTSiHSzBvrAtascrriPCmnDkP4AYQ0Em3TGPHf8P38ARpP/4PsHQ7aU69Hqdi4OJeI47JXLg9IX1tVbUuWCzSsygcJO+L0goGNRpySoB1colarerhEvcCn4tuxCB/TsC5ZdS3Ct2Ho75uZmRoigWbIbgCxt7Q0O8TLsgg17o2R2+JHzzJcV/1IoBlybel42SLsCcc3I6UqoaZ+lUHFYjjCzJSQa+4J4UVLKiX8ScOGVXJXDN+xd7SfZXFYS65U+n2Wz+cOw+ezHadSkfCRdVJiZdlmMG9ZtHMpgWR3Z0eMSvWExjmrbXoCbZ951dIim7Yk6u1IYtlwavChW51Pp01tXOwf01eC9RHs/Rbbnx2Hf4IUh/Q5zdol8H3KG22Pj9edOK54LF1876pd37TELhqdZ+J4MwxDHUvXgDOFQrB7YqLpjI4WdhG8GLYNbCeBK/1+TWNWRG+Pfy5Hj5Dek3IXzX9+8DkL/AIeLC/PDU1NHXdSQk5altNsDkjPAT+ArzKyFnK5bB2IxbYik4hanmfM5GTL6Q/Y2DUc25y0HLToubR8TI39cL4APJOxr/35AXgOXEJnHKQvepP6eiQj119kDrZjqLo8HAAAAABJRU5ErkJggg==","width":50,"height":50,"src":"/static/26a7a327ccc4b335712e5a7086f2b26d/45876/avatar.png","srcSet":"/static/26a7a327ccc4b335712e5a7086f2b26d/45876/avatar.png 1x,\n/static/26a7a327ccc4b335712e5a7086f2b26d/eb85b/avatar.png 1.5x,\n/static/26a7a327ccc4b335712e5a7086f2b26d/4f71c/avatar.png 2x,\n/static/26a7a327ccc4b335712e5a7086f2b26d/9ec3e/avatar.png 3x"}}},"headerImage":{"relativePath":"pages/IntelliJ IDEA plugin development - focus on UI based plugins/header.png","childImageSharp":{"resolutions":{"base64":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAALCAYAAAB/Ca1DAAAACXBIWXMAAAsSAAALEgHS3X78AAADGklEQVQozx3R60/bVRjA8V80m2N4IVkWcfpmqyulTdd7oRYopEDbyaXlVgptobANkAzrUpwbyjZvgMpQ7CxhGCaXUTcv04mRRYMkKpB1MLYpIKkymb5S/4WvZ7745DkvnnOey5FS87qQWbqY0zWyIEzrmxkxHOFHcXZkRZCsZ0ixnmKH9TQPWV7m0eIo22q/JmjvI6ENsijyh7I62G7rYXt2F1KgpIo2TZhlbYBFkTCjDaE2Rdhn6mS36TiPC+nml9ilP0a6rQ91wxwZhxJ4ar9kMbuDdeMR+h0DyAOzyBwxpJDhMCsHAlzXBFgQ7mj8DGqaeUr3Agrt8yh0YWQiZolOq9wTuLxXcFZ/SmHNFVrKL/DKwXMcrPkcu2+aYkGKqkKsquu5rvZzQ7gf19R1nFTUo1M0Y5E3YNWGidu6mSzsQdc0j+ZwgrzgLFUVcWEKn/uiMInfPY40pPKxrvWypPFyU7ilqWZJX8dr9l4UrjFUzlFy3OJS8BqWtmVkRzeQtf/K3qNJCsT4EWeUU65+ely9HHOdQ3pR4Wa1OI8Vez637DaWiwp5tfxtIidmGbywxsilJH2xn3lneI3RyXU+iK3Q1votPu9VfP5vOOGMEbeHmXEcoregV+wwvYx4volk816SoSeZrS3lfPmbfFj2OqPeQcbqo4z7+pnwDvBxYJjJ2iFGSt8j5jhLzPUuY7ndXLREmMruZMJyHCmyq4jO3UWMGg1MO+XM5z7LkiHEDwcamVM28JM6wFpODas5Xm7omljRt3DH1Mptcxu/GFv4yNlNZ3CQPk8P14ytSG+l5XHmESund+ZzcmcuX+xx8ldmNckMD78pKthUevjHXMK/5lLuZlYIlWwpq9lSVJJUeQkFL2N77ntsHfO8URpFGk41M572DLFUI++nGDj/sJnb+0q4l1nFH8LW/yrZ2O8mKfdwVxT5/X4x4c8MN+3ip1XtGxj8X+GouYw081gW9+TlfJaiY/xBJWMPKPlkh5bEEwXc3GMnkV7Ad2nZxLepuZpqYHN/GVvisU3h76dLmDKGsTQuoK+7RJNrgP8AU8rtTZmilCkAAAAASUVORK5CYII=","width":1280,"height":720,"src":"/static/6fae10ea7503a11a8f308ac2e2a5217a/26421/header.png","srcSet":"/static/6fae10ea7503a11a8f308ac2e2a5217a/26421/header.png 1x"}}}},"html":"<p><strong>BACKGROUND</strong></p>\n<p>This post is in addition to the previous <a href=\"https://auto1.tech/intellij-plugin/\">article</a> about plugin development but with a focus on UI based plugin implementations. Sometimes it can be really useful to have an UI and make it possible to interact with it by doing some checkboxes selections or maybe to have some input areas for text. Let's investigate how to develop a simple plugin with input text area as input entry point and some new generated classes, based on input text as a result. As a source code example we will have a look at source code based on my pet <a href=\"https://github.com/robohorse/RoboPOJOGenerator\">project</a> that also was used as a development tool in AUTO1 to generate non GraphQl based POJO classes.</p>\n<p><strong>USER STORY</strong></p>\n<p>As an Android developer I need to interact with backend data. It can be usually presented as JSON data or if we can speak about new and modern practices it also can be presented as a <a href=\"https://graphql.org/\">GraphQl</a> scheme. GraphQl is a really powerful framework with its own classes generation framework. But let's go back  one step and imagine that we have to do some data presentation classes updated for the project without GraphQl support. To do it, we need to find the target class, compare its fields with new JSON fields and update it. It can be easy if we have only one field changed, but for multiple inner fields and classes with its own inner fields it can be too complicated. We can find some online services to convert JSON to POJO and copy/paste output files, but let's try to integrate this technology to our IDE! And our job will be only to copy/paste new JSON to some window input field! Like this one:</p>\n<p><a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/e5f23ade9299ab35fc67e79b19642e81/a8455/ss1.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-wrapper\"\n    style=\"position: relative; display: block;  max-width: 512px; margin-left: auto; margin-right: auto;\"\n  >\n    <span\n      class=\"gatsby-resp-image-background-image\"\n      style=\"padding-bottom: 59.5703125%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAMCAIAAADtbgqsAAAACXBIWXMAAAsSAAALEgHS3X78AAABp0lEQVQoz32SWY7cIBCGfZhkpMngBQx4Axsbbxiv7ZnJptwjJ4iSlzzlwKluJ61RZ/n1CSHwX1VU2UkwaVsd1zLLhJD5lc6YwY6DtV1vMpknmciETIWEPSDygjHuEBI2WqZpItKKMk4pO+BRHMXJQd12dpoK3SitZVHIIs9VSXnk3HvBqtCc+9PcxBFByPUucj33EOzLfhr3D/mwq35Jqy5WbVoZwmLHC7AQCSUe5TikBJMbQow9/fEz/vTj1frl9enr3f7tDtan70hMDmTy/QD9yvEXwfEwL9O2y6rJVJUVZ4SqQxY5yPMxxv+0nuV3djTzoprucL40e0HwX7Pv665vjU1zlQFFCQilQ+j2pWwfoQcXeoWQeysEt2ATqpKlFueyX5jBGyUJTAZmw3kcUhayWxoz7M/vHt++t/MKIc6xysPsB3bZxvU0rad526u2T3/XdiWvaii7HazuTFHViSygcwTMPiZ23oZp6YYRomxPz5AHProC4arO1P1QGzufHiFNY8Z+nHmSOvdvHshlxAGhBGrmEax/wCmLABbFFJ4GP2IUw3t/AoHwpaT67vbNAAAAAElFTkSuQmCC'); background-size: cover; display: block;\"\n    ></span>\n    <img\n        class=\"gatsby-resp-image-image\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;box-shadow:inset 0px 0px 0px 400px white;\"\n        alt=\"ss1\"\n        title=\"\"\n        src=\"/static/e5f23ade9299ab35fc67e79b19642e81/a8455/ss1.png\"\n        srcset=\"/static/e5f23ade9299ab35fc67e79b19642e81/45806/ss1.png 148w,\n/static/e5f23ade9299ab35fc67e79b19642e81/ae1c8/ss1.png 295w,\n/static/e5f23ade9299ab35fc67e79b19642e81/a8455/ss1.png 512w\"\n        sizes=\"(max-width: 512px) 100vw, 512px\"\n      />\n  </span>\n  </a></p>\n<p>To make it possible, let's think about our problem and try to do small decomposition into logical steps. Lets formulate our goals by imaging simple user stories:</p>\n<ol>\n<li>\n<p>As a user I want to input JSON text into the input field.</p>\n</li>\n<li>\n<p>As a user I want to transform JSON text into POJO files.</p>\n</li>\n<li>\n<p>As a user I want to see the result notification.</p>\n</li>\n</ol>\n<p>We will skip JSON to POJO transformation for this article, but we will learn how to develop a plugin with required user interface. Let's think about option number two as about some library that we already have and can use as a part of our small project.</p>\n<p><strong>TECHNOLOGY STACK</strong></p>\n<p>Before we will start let's have a look on basis plugin implementation. There are 2 base options to develop new plugin:</p>\n<ul>\n<li>\n<p>using <a href=\"https://plugins.jetbrains.com/docs/intellij/using-dev-kit.html\">DevKit</a></p>\n</li>\n<li>\n<p>using <a href=\"https://plugins.jetbrains.com/docs/intellij/gradle-build-system.html\">gradle</a></p>\n</li>\n</ul>\n<p>We will speak about gradle implementation. Gradle is a really powerful framework and it can be really helpful if we want to simplify our building, releasing and testing. The project structure is a common <a href=\"https://plugins.jetbrains.com/docs/intellij/gradle-prerequisites.html\">structure</a> for gradle based projects:</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">my_gradle_plugin\n\n├── build.gradle\n\n├── gradle\n\n│ └── wrapper\n\n│ ├── gradle-wrapper.jar\n\n│ └── gradle-wrapper.properties\n\n├── gradlew\n\n├── gradlew.bat\n\n├── settings.gradle\n\n└── src\n\n├── main\n\n│ ├── java\n\n│ └── resources\n\n│ └── META-INF\n\n│ └── plugin.xml\n\n└── test\n\n├── java\n\n└── resources\n\n  </code></pre></div>\n<p>Most important topic for us will be the plugin.xml  file. We already discussed some details in the previous article and we can be focused on some UI related topics.</p>\n<p>During the plugin development we can use Java/Kotlin and all utility tools that we want like jUnit testing and dependency injection frameworks. In our current example Koin and JUnit can be found. So, now we have and know about project structure, some utility tools, language to write the code and now we are ready to think how it can be possible to create a user interface and connect it with our IDE. Let's remember about our major goals:</p>\n<p><strong>GOAL 1</strong> -  As a user I want to input JSON text into the input field.\n<br>\n<strong>GOAL 2</strong> -  As a user I want to see the result notification.\n</p>\n<p>and see how we can implement it.</p>\n<p><strong>GOAL 1</strong></p>\n<p>First of all, let's learn how to connect our plugin with the IDE user interface. As we can see the IDE has a lot of action groups to do something (create new files for example). According to our idea to have some logic that can help us to convert JSON to POJO it would be great to have a new item in the new file list. It will be something like an action “Create new POJO file”.</p>\n<p>To connect our logic with the IDE by following this path we have to remember about the plugin.xml  file that was discussed early. We can use this configuration file to describe our plugin and how it can be used.</p>\n<p>Let's find the actions section and define <a href=\"https://github.com/robohorse/RoboPOJOGenerator/blob/master/src/main/resources/META-INF/plugin.xml#L28\">new action</a> to show our input field dialog view.</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">&lt;actions>\n\n&lt;action id=\"GeneratePOJOAction\" class=\"GeneratePOJOAction\"\n\nicon=\"/icons/pojo.png\"\n\ntext=\"Generate POJO from JSON\"\n\ndescription=\"GeneratePOJOAction\">\n\n&lt;add-to-group group-id=\"NewGroup\" anchor=\"last\"/>\n\n&lt;/action>\n\n&lt;/actions></code></pre></div>\n<p>As we can see we can use some properties to describe how users will be available to interact with our plugin. We can define a custom icon for our action, define the position inside of the items list (“NewGroup” anchor “last”) and we can define the class to execute when action will be invoked by the user.  </p>\n<p><a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/6a7a1fe33666ed623520c2623f9c6a69/2a77b/ss2.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-wrapper\"\n    style=\"position: relative; display: block;  max-width: 590px; margin-left: auto; margin-right: auto;\"\n  >\n    <span\n      class=\"gatsby-resp-image-background-image\"\n      style=\"padding-bottom: 50.27027027027027%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAKCAIAAAA7N+mxAAAACXBIWXMAAAsSAAALEgHS3X78AAABjElEQVQoz2VRu27cMBDUpxhx59aNT+L7oQcpUSQlnnQnnxPAQKrE8K8Evi5OFxdBfjLr0yGxEWAKYndnZ2eYXR5e2OPvD4eXi9v/8fPi8OtqOaLtR+dDSJPpeqbKgvIVmfN9ZSwiuMAIE/wWCKO2s0Qquvs0pG3n47CddW1yyjeY5oRltvfjNBvnqdI3mG4I22CWnxZDuzQtEVqlpXWe6woARWiBPiDTjU3z3rheVo0oa65LmKBCrWQoYq7CvIzTDmRV1bzWKQeWqk1GpepCrG3XtA4s1W3XdE5WNQwhxplUiMvCT9b1rY9wCDyY0uezMRdtH6zzLgw3iEB1bZyUOZhiWqvRhiG5EHVjCsoQF/gEIMtpv4S09WMC8b9JAqCVdnvwEne3fhj7OLg4Cl39SxsWwPqVRoSEU9/iVYFJbXvwHNKc5mVeDrysIVcKgQEBVoITmDtPv4OkQjrfhhhCjPDZAExJjjH87ll5zQn03ysLTFlRevP4VD38qB+ey6/P+sv3/P7IPh+v7779AQwRq7SC2ek/AAAAAElFTkSuQmCC'); background-size: cover; display: block;\"\n    ></span>\n    <img\n        class=\"gatsby-resp-image-image\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;box-shadow:inset 0px 0px 0px 400px white;\"\n        alt=\"ss2\"\n        title=\"\"\n        src=\"/static/6a7a1fe33666ed623520c2623f9c6a69/40fad/ss2.png\"\n        srcset=\"/static/6a7a1fe33666ed623520c2623f9c6a69/707e9/ss2.png 148w,\n/static/6a7a1fe33666ed623520c2623f9c6a69/649e0/ss2.png 295w,\n/static/6a7a1fe33666ed623520c2623f9c6a69/40fad/ss2.png 590w,\n/static/6a7a1fe33666ed623520c2623f9c6a69/b3fef/ss2.png 885w,\n/static/6a7a1fe33666ed623520c2623f9c6a69/2a77b/ss2.png 1110w\"\n        sizes=\"(max-width: 590px) 100vw, 590px\"\n      />\n  </span>\n  </a></p>\n<p>Custom action class should extend the abstract AnAction class with the implementation of actionPerformed() method.</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">import com.intellij.openapi.actionSystem.AnAction\n\nimport com.intellij.openapi.actionSystem.AnActionEvent\n\nclass GeneratePOJOAction : AnAction() {\n\noverride fun actionPerformed(e: AnActionEvent) { \n\n//TODO: Add your UI implementation here \n\n} \n}</code></pre></div>\n<p>Inside of actionPerformed() method implementation we can add the logic to show a dialog window with the input field for JSON text. To show dialog view we can easily use DialogBuilder from plugin development SDK and Java swing to create a user interface.</p>\n<p>More information about actions can be found on the official documentation <a href=\"https://plugins.jetbrains.com/docs/intellij/basic-action-system.html\">page</a>.</p>\n<p><strong>GOAL 2</strong></p>\n<p>Let's imagine now that we already implemented logic to show the input text dialog with some selectable options to transform this text. We also added some logic of transformation - that can be some library to work with it or maybe our own implementation. And as the result of that transformation some new POJO files were generated added into our project (We can interact directly with the project or we can use project representation, provided by the plugin development <a href=\"https://plugins.jetbrains.com/docs/intellij/virtual-file-system.html\">SDK</a>).</p>\n<p>But what about the next steps? We also need to notify the user about the result: it can be a success result notification, or maybe some error case notification. To implement it we can use default tools, provided by plugin development SDK. We can show some information to the user by using <a href=\"https://plugins.jetbrains.com/docs/intellij/popups.html\">popup</a>, <a href=\"https://plugins.jetbrains.com/docs/intellij/notifications.html#editor-hints\">notifications</a> or <a href=\"https://plugins.jetbrains.com/docs/intellij/misc-swing-components.html\">messages</a>.</p>\n<p>Let's imagine the following case: we have a text input field and we have some logic to convert/transform the text from this input field. If we can speak about JSON to POJO transformation, we have to understand that any random text cannot be converted to POJO correctly. Target text should satisfy some conditions and also should have the correct JSON structure and it means that we have to not only show some notification if text structure is not valid, but we have to block UI and break our transformation flow.</p>\n<p><a href=\"https://plugins.jetbrains.com/docs/intellij/misc-swing-components.html#messages\">Messages</a> can really help us with it! According to the official documentation:</p>\n<p>The Messages class provides a way to show simple message boxes, input dialogs (modal dialogs with a text field), and chooser dialogs (modal dialogs with a combo box). The function of different methods of the class should be clear from their names. When running on macOS, the message boxes shown by the Messages class use the native UI.</p>\n<p>Lets see the following code:</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">companion object {\n\nprivate const val BUTTON_TITLE = \"OK\"\n\nprivate const val DEFAULT_OPTION_INDEX = -1\n\nprivate val DEFAULT_ICON = null\n\n}\n\nfun onPluginExceptionHandled(exception: RoboPluginException) {\n\nMessages.showDialog(\n\nexception.message, exception.title,\n\narrayOf(BUTTON_TITLE),\n\nDEFAULT_OPTION_INDEX,\n\nDEFAULT_ICON\n\n)\n\n}</code></pre></div>\n<p>As we can see we can use the show dialog method, provided by SDK. We can define some dialog parameters, like title, message, button and of course we can define our custom plugin icon. If the image is not defined, standart Java icon or IDE icon would be shown. More information about images support can be found <a href=\"https://plugins.jetbrains.com/docs/intellij/work-with-icons-and-images.html\">here</a>. Also as it was already discussed <a href=\"https://auto1.tech/intellij-plugin/\">here</a> we can add support for internationalization instead of hardcoding only english string values.</p>\n<p>And as the result we can see this amazing small UI blocking window with our message text:</p>\n<p><a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/ccbe6b5c7965f5c43ecabbe66bf5c2c8/adc68/ss3.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-wrapper\"\n    style=\"position: relative; display: block;  max-width: 590px; margin-left: auto; margin-right: auto;\"\n  >\n    <span\n      class=\"gatsby-resp-image-background-image\"\n      style=\"padding-bottom: 59.91561181434599%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAMCAIAAADtbgqsAAAACXBIWXMAAAsSAAALEgHS3X78AAAB9UlEQVQoz22S3W7TMBTH8zJMWtd8dCSxHefLSdw4H07SNl27cssFghcYl4hK44bxACBxz7ob1o7uYlofjbN2Kwxh/WT9j33+OsdHVs7O3t/d3a9ubta3t7BfXl39WCx+Xi8XIC4XoEFcL5fL1QqAhF/rNez3m818Plc8j2WZjJI45jxOxYeP888XX0aT6ehkOp7O2snpZPaqGrZpWQlZi0JmRZ6XRVlJPwwVTTe6R91O5/Coc6j2Xn7/+m2zWmV1k8u6qJtE5FzkLEkDFhGHIOqbvjD91ArynkUUo3esG4a2XaaNoc6bt+88FgcxD6LEDSMaRG7AiGhxMbP4+MXg/GD06WB8oXrNH7OqqjahtuMd29jxQ+IFAIgHvACJEzJ4jZmgXkiD2A25iZ5X1nTd6PUAOPkbnhWcc0oJcX0agDl0Q2YivDer2rb6f9A0mJNshm4YPzbigzl+Muu6qnbV7frX2e3C7W4EfpQAICCEWTyYj00TOxRhggjBxDFtZFr2cxDPcjkY1sM2K6uoL8D/aLYwyaomLaQoq0zWLOlDVzRgeyBkPIWEXFZJmsEUMbwczDZWbOLkW3NalFlVN+2Y5wXrp1EqdjDeT+BnVA0wmpzKYQsh/BnkuIpmGOC3ELZsbGOCHRc5FE72QGtPmsLtLgFTF4b1G06By+5CDYTEAAAAAElFTkSuQmCC'); background-size: cover; display: block;\"\n    ></span>\n    <img\n        class=\"gatsby-resp-image-image\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;box-shadow:inset 0px 0px 0px 400px white;\"\n        alt=\"ss3\"\n        title=\"\"\n        src=\"/static/ccbe6b5c7965f5c43ecabbe66bf5c2c8/40fad/ss3.png\"\n        srcset=\"/static/ccbe6b5c7965f5c43ecabbe66bf5c2c8/707e9/ss3.png 148w,\n/static/ccbe6b5c7965f5c43ecabbe66bf5c2c8/649e0/ss3.png 295w,\n/static/ccbe6b5c7965f5c43ecabbe66bf5c2c8/40fad/ss3.png 590w,\n/static/ccbe6b5c7965f5c43ecabbe66bf5c2c8/b3fef/ss3.png 885w,\n/static/ccbe6b5c7965f5c43ecabbe66bf5c2c8/301c0/ss3.png 1180w,\n/static/ccbe6b5c7965f5c43ecabbe66bf5c2c8/adc68/ss3.png 1422w\"\n        sizes=\"(max-width: 590px) 100vw, 590px\"\n      />\n  </span>\n  </a></p>\n<p>It looks now that we found a solution to cover the error cases. Now we can prevent users from wrong input and show notification about it. Let's think about the success use case. Let's imagine that the input text was correct and we processed our JSON to POJO transformation as expected. Maybe some new files were generated, maybe it was only one new file. It will be a really good user experience if we can find a possibility to inform our users about it. Maybe one of the possible ways can be to show notification messages. It can be a small popup view with some text, like this one:</p>\n<p><a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/7886ad65f05e9b387cd9fcc518e532eb/a8455/ss4.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-wrapper\"\n    style=\"position: relative; display: block;  max-width: 512px; margin-left: auto; margin-right: auto;\"\n  >\n    <span\n      class=\"gatsby-resp-image-background-image\"\n      style=\"padding-bottom: 19.7265625%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAECAIAAAABPYjBAAAACXBIWXMAAAsSAAALEgHS3X78AAAAiklEQVQI123O2Q6DIBAFUH6rspRtEOgWY9rHqoCWP+jvFy1PrcnJZO7kPgyiXArjNRilC5AavkuNCoTUu7hQyDyGa37PMYa0pDmvc8nPcRpDiuk1hNj19123rkdCA1gHrTXWGes3JbpytP7sTpca/2jTogMmDWWYsobQiq4wYaK8bh09crwVfpTmB6TCRCxGPmcyAAAAAElFTkSuQmCC'); background-size: cover; display: block;\"\n    ></span>\n    <img\n        class=\"gatsby-resp-image-image\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;box-shadow:inset 0px 0px 0px 400px white;\"\n        alt=\"ss4\"\n        title=\"\"\n        src=\"/static/7886ad65f05e9b387cd9fcc518e532eb/a8455/ss4.png\"\n        srcset=\"/static/7886ad65f05e9b387cd9fcc518e532eb/45806/ss4.png 148w,\n/static/7886ad65f05e9b387cd9fcc518e532eb/ae1c8/ss4.png 295w,\n/static/7886ad65f05e9b387cd9fcc518e532eb/a8455/ss4.png 512w\"\n        sizes=\"(max-width: 512px) 100vw, 512px\"\n      />\n  </span>\n  </a></p>\n<p>and maybe we also can print some information to the event log:</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">12:00  Response.java was updated.\n  \n12:00  POJO generation: Success</code></pre></div>\n<p>It looks now that we are ready to totally satisfy our user story requirements! Let's do the final steps and implement the following code:</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">companion object{\n\nprivate const val MESSAGE_SUCCESS = \"POJO generation: Success\"\n\nprivate const val GROUP_ID = \"RoboPOJO Generator\"\n\n}\n\nfun showSuccessMessage() {\n\nshowMessage(\n\nproject = ProjectManager.getInstance().openProjects.first(),\n\nmessage = MESSAGE_SUCCESS,\n\ntype = NotificationType.INFORMATION,\n\n)\n\n}\n\nprivate fun showMessage(\n\nmessage: String, type: NotificationType, project: Project\n\n) = NotificationGroupManager.getInstance()\n\n.getNotificationGroup(GROUP_ID)\n\n.createNotification(message, type)\n\n.notify(project)\n\n  </code></pre></div>\n<p>As we can see we can use the create notification method, provided by SDK. Unlike dialog related logic, we also have to define the project that we want to use to show the notification. As an example let's get only the first project (most popular use case), but we have to understand that users can open not only one project in the IDE and we have to take care about it too. So, as we can see it is also possible to define message text. Also we can define the message type (it can be INFORMATION, WARNING or ERROR) and we have to take care about the <a href=\"https://plugins.jetbrains.com/docs/intellij/notifications.html#top-level-notifications-balloons\">notification group</a> that was defined inside of our plugin.xml file:</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">&lt;extensions defaultExtensionNs=\"com.intellij\">\n\n&lt;notificationGroup id=\"RoboPOJO Generator\"\n\ndisplayType=\"BALLOON\"\n\nkey=\"com.robohorse.robopojogenerator\"/>\n\n&lt;/extensions></code></pre></div>\n<p><strong>FEW WORDS AT THE END</strong></p>\n<p>As we can see it was not really a big deal to develop some small and hopefully useful tools for daily use or to help your teammates. The major idea is just to understand that integration with IDE can be really simple and when we will finish with it, we can be focused on the functionality of our tool. Perhaps in the future when we will have a stable version with some really cool features we can share our tool not only with teammates that we are working with directly, but maybe it also can be helpful for some developers from some random parts of the world. And maybe together we can simplify our daily job!</p>","fields":{"slug":"/IntelliJ IDEA plugin development - focus on UI based plugins/","tags":["auto1","engineering","android","mobile","intellij","idea"]}},"categoryArticles":{"edges":[{"node":{"id":"cb45e6dc-ba61-576d-adc2-ba609b0e722a","frontmatter":{"category":"Engineering","title":"Go 1.23: the new Pattern property in http.Request","date":"2024-11-26","summary":"A short review of the new Pattern property in net/http.Request","thumbnail":null,"authorName":"Andrei Gusev","authorDescription":"Senior Software Engineer at AUTO1 Group","authorAvatar":{"relativePath":"pages/go-123-the-new-pattern-property-in-http-request/avatar_Andrei.jpg","childImageSharp":{"resolutions":{"base64":"data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAUABQDASIAAhEBAxEB/8QAGAABAQEBAQAAAAAAAAAAAAAAAAUBBgT/xAAWAQEBAQAAAAAAAAAAAAAAAAAAAQL/2gAMAwEAAhADEAAAAaeTsi2mDmfWV0wzf//EAB0QAAICAQUAAAAAAAAAAAAAAAECAAMEEBESEyH/2gAIAQEAAQUCtYiUvyE71ZkuQNuzaYvuQTP/xAAUEQEAAAAAAAAAAAAAAAAAAAAg/9oACAEDAQE/AR//xAAUEQEAAAAAAAAAAAAAAAAAAAAg/9oACAECAQE/AR//xAAcEAACAgIDAAAAAAAAAAAAAAABEQACEBIhMTL/2gAIAQEABj8C4KnprGnRhqdnHWpWKPH/xAAdEAACAgMAAwAAAAAAAAAAAAABEQAhEEFRMXGB/9oACAEBAAE/IRoFjyQHGILdksEYAYLXeSuANz4mvAhI736hHP/aAAwDAQACAAMAAAAQNBc+/8QAFhEAAwAAAAAAAAAAAAAAAAAAABAR/9oACAEDAQE/ECv/xAAUEQEAAAAAAAAAAAAAAAAAAAAg/9oACAECAQE/EB//xAAcEAEBAAMBAAMAAAAAAAAAAAABEQAhUTEQYaH/2gAIAQEAAT8Q0H5agDhjJzCu2+nxvKetJ9/fd9x0k6GIzjdTPf69KX9xcAAKlBR2wEDDmf/Z","width":50,"height":50,"src":"/static/479e481648d69b84a342bd2575a225d4/d2d31/avatar_Andrei.jpg","srcSet":"/static/479e481648d69b84a342bd2575a225d4/d2d31/avatar_Andrei.jpg 1x,\n/static/479e481648d69b84a342bd2575a225d4/0b804/avatar_Andrei.jpg 1.5x,\n/static/479e481648d69b84a342bd2575a225d4/753c3/avatar_Andrei.jpg 2x,\n/static/479e481648d69b84a342bd2575a225d4/31ca8/avatar_Andrei.jpg 3x"}}},"headerImage":null},"html":"<h1>Go 1.23: the new Pattern property in http.Request</h1>\n<p>In Go 1.23, a new <code class=\"language-text\">Pattern</code> property in <code class=\"language-text\">net/http.Request</code> was added.\nIt contains the route pattern used to handle the request. This improvement may have gone unnoticed by many, but it can\nsimplify request processing and improve performance in cases where we need to obtain the matched routing pattern\nfor the current request. Let’s see how we can use this feature.</p>\n<p>At AUTO1, we have more than 200 microservices, some of which are written in Go and include an HTTP server.\nIt is critically important for us to collect metrics from these services in order to quickly respond to incidents,\nmake data-driven decisions, and optimize system performance.\nFor example, we collect <code class=\"language-text\">http_requests_total</code> metric:</p>\n<div class=\"gatsby-highlight\" data-language=\"go\"><pre class=\"language-go\"><code class=\"language-go\">requestsTotal <span class=\"token operator\">=</span> promauto<span class=\"token punctuation\">.</span><span class=\"token function\">NewCounterVec</span><span class=\"token punctuation\">(</span>prometheus<span class=\"token punctuation\">.</span>CounterOpts<span class=\"token punctuation\">{</span>\n\t\tName<span class=\"token punctuation\">:</span> <span class=\"token string\">\"http_requests_total\"</span><span class=\"token punctuation\">,</span>\n\t\tHelp<span class=\"token punctuation\">:</span> <span class=\"token string\">\"total request count\"</span><span class=\"token punctuation\">,</span>\n\t<span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span> <span class=\"token punctuation\">[</span><span class=\"token punctuation\">]</span><span class=\"token builtin\">string</span><span class=\"token punctuation\">{</span><span class=\"token string\">\"path\"</span><span class=\"token punctuation\">,</span> <span class=\"token string\">\"method\"</span><span class=\"token punctuation\">,</span> <span class=\"token string\">\"code\"</span><span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span></code></pre></div>\n<p>Obviously, for the <code class=\"language-text\">path</code> label, we should store the path template, not the specific value, for example, <code class=\"language-text\">/test/{id}</code> instead of <code class=\"language-text\">/test/123</code>.\nOtherwise, we risk a cardinality explosion, which can significantly increase the amount of stored data. You can read how to choose labels properly in <a href=\"https://prometheus.io/docs/practices/naming/#labels\">\"Prometheus best practices\"</a>.</p>\n<h2>Before v1.23</h2>\n<p>Previously, to extract the route pattern, we had to pass the multiplexer to a middleware function:</p>\n<div class=\"gatsby-highlight\" data-language=\"go\"><pre class=\"language-go\"><code class=\"language-go\"><span class=\"token keyword\">func</span> <span class=\"token function\">metricsMiddleware</span><span class=\"token punctuation\">(</span>next http<span class=\"token punctuation\">.</span>Handler<span class=\"token punctuation\">,</span> mux <span class=\"token operator\">*</span>http<span class=\"token punctuation\">.</span>ServeMux<span class=\"token punctuation\">)</span> http<span class=\"token punctuation\">.</span>Handler <span class=\"token punctuation\">{</span>\n\t<span class=\"token keyword\">return</span> http<span class=\"token punctuation\">.</span><span class=\"token function\">HandlerFunc</span><span class=\"token punctuation\">(</span><span class=\"token keyword\">func</span><span class=\"token punctuation\">(</span>w http<span class=\"token punctuation\">.</span>ResponseWriter<span class=\"token punctuation\">,</span> r <span class=\"token operator\">*</span>http<span class=\"token punctuation\">.</span>Request<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n\t\t<span class=\"token boolean\">_</span><span class=\"token punctuation\">,</span> pattern <span class=\"token operator\">:=</span> mux<span class=\"token punctuation\">.</span><span class=\"token function\">Handler</span><span class=\"token punctuation\">(</span>r<span class=\"token punctuation\">)</span>\n\t\t\n\t\t<span class=\"token operator\">...</span>\n\t\trequestsTotal<span class=\"token punctuation\">.</span><span class=\"token function\">WithLabelValues</span><span class=\"token punctuation\">(</span>pattern<span class=\"token punctuation\">,</span> method<span class=\"token punctuation\">,</span> statusCode<span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">Inc</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n\t<span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>In this approach, we matched the request to the route a second time, and that negatively affected performance.</p>\n<h2>Since 1.23</h2>\n<p>Starting with Go 1.23, we can get the route pattern directly from the request, without passing the multiplexer to the middleware:</p>\n<div class=\"gatsby-highlight\" data-language=\"go\"><pre class=\"language-go\"><code class=\"language-go\"><span class=\"token keyword\">func</span> <span class=\"token function\">middleware</span><span class=\"token punctuation\">(</span>f http<span class=\"token punctuation\">.</span>Handler<span class=\"token punctuation\">)</span> http<span class=\"token punctuation\">.</span>Handler <span class=\"token punctuation\">{</span>\n\t<span class=\"token keyword\">return</span> http<span class=\"token punctuation\">.</span><span class=\"token function\">HandlerFunc</span><span class=\"token punctuation\">(</span><span class=\"token keyword\">func</span><span class=\"token punctuation\">(</span>w http<span class=\"token punctuation\">.</span>ResponseWriter<span class=\"token punctuation\">,</span> r <span class=\"token operator\">*</span>http<span class=\"token punctuation\">.</span>Request<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n\t\tpattern <span class=\"token operator\">:=</span> r<span class=\"token punctuation\">.</span>Pattern\n\t\t\n\t\t<span class=\"token operator\">...</span>\n\t\trequestsTotal<span class=\"token punctuation\">.</span><span class=\"token function\">WithLabelValues</span><span class=\"token punctuation\">(</span>pattern<span class=\"token punctuation\">,</span> method<span class=\"token punctuation\">,</span> statusCode<span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">Inc</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n\t<span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>This significantly simplifies the middleware, making the code cleaner and more readable. We are no longer dependent on <code class=\"language-text\">ServeMux</code> and no longer need to re-match the request.</p>\n<h2>Limitations</h2>\n<p>It is important to remember that <code class=\"language-text\">Request.Pattern</code> is only available after the request has been matched to a route. For example:</p>\n<div class=\"gatsby-highlight\" data-language=\"go\"><pre class=\"language-go\"><code class=\"language-go\"><span class=\"token keyword\">func</span> <span class=\"token function\">main</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n\tmux <span class=\"token operator\">:=</span> http<span class=\"token punctuation\">.</span><span class=\"token function\">NewServeMux</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n\tmux<span class=\"token punctuation\">.</span><span class=\"token function\">Handle</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"/test/{id}\"</span><span class=\"token punctuation\">,</span> <span class=\"token function\">routeMiddleware</span><span class=\"token punctuation\">(</span><span class=\"token function\">handler</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span>\n\n\tsrv <span class=\"token operator\">:=</span> http<span class=\"token punctuation\">.</span>Server<span class=\"token punctuation\">{</span>\n\t\tAddr<span class=\"token punctuation\">:</span>    <span class=\"token string\">\":8080\"</span><span class=\"token punctuation\">,</span>\n\t\tHandler<span class=\"token punctuation\">:</span> <span class=\"token function\">globalMiddleware</span><span class=\"token punctuation\">(</span>mux<span class=\"token punctuation\">)</span><span class=\"token punctuation\">,</span>\n\t<span class=\"token punctuation\">}</span>\n\n\t<span class=\"token keyword\">if</span> err <span class=\"token operator\">:=</span> srv<span class=\"token punctuation\">.</span><span class=\"token function\">ListenAndServe</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token operator\">!</span>errors<span class=\"token punctuation\">.</span><span class=\"token function\">Is</span><span class=\"token punctuation\">(</span>err<span class=\"token punctuation\">,</span> http<span class=\"token punctuation\">.</span>ErrServerClosed<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n\t\tlog<span class=\"token punctuation\">.</span><span class=\"token function\">Fatalf</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"server error: %s\"</span><span class=\"token punctuation\">,</span> err<span class=\"token punctuation\">)</span>\n\t<span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span>\n\n<span class=\"token keyword\">func</span> <span class=\"token function\">routeMiddleware</span><span class=\"token punctuation\">(</span>h http<span class=\"token punctuation\">.</span>Handler<span class=\"token punctuation\">)</span> http<span class=\"token punctuation\">.</span>Handler <span class=\"token punctuation\">{</span>\n\t<span class=\"token keyword\">return</span> http<span class=\"token punctuation\">.</span><span class=\"token function\">HandlerFunc</span><span class=\"token punctuation\">(</span><span class=\"token keyword\">func</span><span class=\"token punctuation\">(</span>w http<span class=\"token punctuation\">.</span>ResponseWriter<span class=\"token punctuation\">,</span> r <span class=\"token operator\">*</span>http<span class=\"token punctuation\">.</span>Request<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n\t\tlog<span class=\"token punctuation\">.</span><span class=\"token function\">Printf</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"ROUTE pattern: %s\"</span><span class=\"token punctuation\">,</span> r<span class=\"token punctuation\">.</span>Pattern<span class=\"token punctuation\">)</span>\n\t\th<span class=\"token punctuation\">.</span><span class=\"token function\">ServeHTTP</span><span class=\"token punctuation\">(</span>w<span class=\"token punctuation\">,</span> r<span class=\"token punctuation\">)</span>\n\t<span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span>\n<span class=\"token punctuation\">}</span>\n\n<span class=\"token keyword\">func</span> <span class=\"token function\">globalMiddleware</span><span class=\"token punctuation\">(</span>h http<span class=\"token punctuation\">.</span>Handler<span class=\"token punctuation\">)</span> http<span class=\"token punctuation\">.</span>Handler <span class=\"token punctuation\">{</span>\n\t<span class=\"token keyword\">return</span> http<span class=\"token punctuation\">.</span><span class=\"token function\">HandlerFunc</span><span class=\"token punctuation\">(</span><span class=\"token keyword\">func</span><span class=\"token punctuation\">(</span>w http<span class=\"token punctuation\">.</span>ResponseWriter<span class=\"token punctuation\">,</span> r <span class=\"token operator\">*</span>http<span class=\"token punctuation\">.</span>Request<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n\t\tlog<span class=\"token punctuation\">.</span><span class=\"token function\">Printf</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"GLOBAL pattern: %s\"</span><span class=\"token punctuation\">,</span> r<span class=\"token punctuation\">.</span>Pattern<span class=\"token punctuation\">)</span>\n\t\th<span class=\"token punctuation\">.</span><span class=\"token function\">ServeHTTP</span><span class=\"token punctuation\">(</span>w<span class=\"token punctuation\">,</span> r<span class=\"token punctuation\">)</span>\n\t\tlog<span class=\"token punctuation\">.</span><span class=\"token function\">Printf</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"GLOBAL pattern after: %s\"</span><span class=\"token punctuation\">,</span> r<span class=\"token punctuation\">.</span>Pattern<span class=\"token punctuation\">)</span>\n\t<span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>If we send a request <code class=\"language-text\">GET http://localhost:8080/test/123</code>, in logs we will see:</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">2024/11/10 13:20:37 GLOBAL pattern: \n2024/11/10 13:20:37 ROUTE pattern: /test/{id}\n2024/11/10 13:20:37 GLOBAL pattern after: /test/{id}</code></pre></div>\n<p>This is the expected behavior. The multiplexer implements the <code class=\"language-text\">http.Handler</code> interface, and inside the <code class=\"language-text\">ServeHTTP</code> method, it searches for the handler that matches the request. Starting from Go 1.23, it fills the <code class=\"language-text\">Pattern</code> property of the request with the matching route pattern:</p>\n<div class=\"gatsby-highlight\" data-language=\"go\"><pre class=\"language-go\"><code class=\"language-go\"><span class=\"token comment\">// net/http/server.go</span>\n<span class=\"token keyword\">func</span> <span class=\"token punctuation\">(</span>mux <span class=\"token operator\">*</span>ServeMux<span class=\"token punctuation\">)</span> <span class=\"token function\">ServeHTTP</span><span class=\"token punctuation\">(</span>w ResponseWriter<span class=\"token punctuation\">,</span> r <span class=\"token operator\">*</span>Request<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n\t<span class=\"token operator\">...</span>\n\t<span class=\"token keyword\">var</span> h Handler\n\t<span class=\"token keyword\">if</span> use121 <span class=\"token punctuation\">{</span>\n\t\th<span class=\"token punctuation\">,</span> <span class=\"token boolean\">_</span> <span class=\"token operator\">=</span> mux<span class=\"token punctuation\">.</span>mux121<span class=\"token punctuation\">.</span><span class=\"token function\">findHandler</span><span class=\"token punctuation\">(</span>r<span class=\"token punctuation\">)</span>\n\t<span class=\"token punctuation\">}</span> <span class=\"token keyword\">else</span> <span class=\"token punctuation\">{</span>\n\t\th<span class=\"token punctuation\">,</span> r<span class=\"token punctuation\">.</span>Pattern<span class=\"token punctuation\">,</span> r<span class=\"token punctuation\">.</span>pat<span class=\"token punctuation\">,</span> r<span class=\"token punctuation\">.</span>matches <span class=\"token operator\">=</span> mux<span class=\"token punctuation\">.</span><span class=\"token function\">findHandler</span><span class=\"token punctuation\">(</span>r<span class=\"token punctuation\">)</span>\n\t<span class=\"token punctuation\">}</span>\n\th<span class=\"token punctuation\">.</span><span class=\"token function\">ServeHTTP</span><span class=\"token punctuation\">(</span>w<span class=\"token punctuation\">,</span> r<span class=\"token punctuation\">)</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>Thus, the <code class=\"language-text\">r.Pattern</code> is filled only after the <code class=\"language-text\">ServeHTTP</code> method of the multiplexer has been called, and the logs confirm this behavior.</p>\n<h2>Conclusion</h2>\n<p>In the last two Go releases, the <code class=\"language-text\">net/http</code> package has received significant improvements that reduced the gap with third-party packages, like <code class=\"language-text\">gorilla/mux</code>. Now, <code class=\"language-text\">net/http.ServeMux</code> supports HTTP methods and wildcards in routing patterns, bringing its functionality closer to what third-party packages previously provided.</p>\n<p>Additionally, the introduction of the <code class=\"language-text\">net/http.Request.Pattern</code> property in Go 1.23 has simplified middleware development by allowing us to access the route pattern without needing to re-match the request, improving performance and reducing code complexity.</p>\n<p>With each of these improvements, there are fewer reasons to choose third-party routers. While the choice between the standard package and third-party routers used to be more obvious, today I would recommend starting with the standard <code class=\"language-text\">net/http</code> package.</p>","fields":{"slug":"/go-123-the-new-pattern-property-in-http-request/","tags":["auto1","go","network_programming"]}}},{"node":{"id":"9720ef25-7d81-5307-9ea5-229d55ec766f","frontmatter":{"category":"Engineering","title":"Android JetPack Compose + Paging 3","date":"2024-03-29","summary":"A short trip around the AUTO1 Application Cockpit data model","thumbnail":null,"authorName":"Sergey Bakhtiarov","authorDescription":"Senior Android Developer at AUTO1 Group","authorAvatar":{"relativePath":"pages/android-compose-pagination/avatar_Sergey.jpeg","childImageSharp":{"resolutions":{"base64":"data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAbABQDASIAAhEBAxEB/8QAGAAAAwEBAAAAAAAAAAAAAAAAAAIEBgX/xAAWAQEBAQAAAAAAAAAAAAAAAAABAgD/2gAMAwEAAhADEAAAAessVEW4wzlnkZ2tJg3/xAAdEAACAQQDAAAAAAAAAAAAAAABAgMABCExECIz/9oACAEBAAEFAsAAq1EVIe0OSdtKzEXEgbfMHh//xAAXEQADAQAAAAAAAAAAAAAAAAAQERIh/9oACAEDAQE/AZxhj//EABURAQEAAAAAAAAAAAAAAAAAABEg/9oACAECAQE/AWP/xAAdEAACAgEFAAAAAAAAAAAAAAABEQAQAiAhImFx/9oACAEBAAY/AtzOJt9U3GCtGHk//8QAGxABAAIDAQEAAAAAAAAAAAAAAQARECExQVH/2gAIAQEAAT8hd4gIS0NTZPIjUV1DC40rgQLqvDjL0fpeUqrP/9oADAMBAAIAAwAAABD/APKz/8QAFREBAQAAAAAAAAAAAAAAAAAAEBH/2gAIAQMBAT8QBR//xAAXEQEBAQEAAAAAAAAAAAAAAAABEBEh/9oACAECAQE/EOnIBP/EAB0QAQEBAAMAAwEAAAAAAAAAAAERACExQVFhcdH/2gAIAQEAAT8QaCJVXrPnop5ivjMtoBBeM7oBFG2x3Zjtir0D+518AlC+E9yQMgcPsxpw/mVwqOXf/9k=","width":50,"height":50,"src":"/static/27c2b15cdd840519c853a653e9b049f0/d2d31/avatar_Sergey.jpeg","srcSet":"/static/27c2b15cdd840519c853a653e9b049f0/d2d31/avatar_Sergey.jpeg 1x,\n/static/27c2b15cdd840519c853a653e9b049f0/0b804/avatar_Sergey.jpeg 1.5x,\n/static/27c2b15cdd840519c853a653e9b049f0/753c3/avatar_Sergey.jpeg 2x,\n/static/27c2b15cdd840519c853a653e9b049f0/31ca8/avatar_Sergey.jpeg 3x"}}},"headerImage":null},"html":"<h1>Android JetPack Compose + Paging 3</h1>\n<h2>About pagination</h2>\n<p>Paginated APIs, often encountered when dealing with servers and databases, break down large datasets into manageable chunks or pages. This methodology not only optimizes resource usage but also enhances the overall user experience by delivering content progressively. However, integrating and managing paginated data in a mobile app can be a challenge, from maintaining loading states to ensuring a seamless transition between pages and supporting content filtering.</p>\n<p>In this guide I will show you how to work with paginated APIs in Android with Jetpack Compose and Paging 3 libraries. </p>\n<p><img src=\"/static/paging_demo-a325402259bf9864dced311c9eafa7c8.gif\" alt=\"Paging UI\"></p>\n<h2>Basics, architecture overview</h2>\n<p><br></br>\n<img src=\"https://developer.android.com/static/topic/libraries/architecture/images/paging3-library-architecture.svg\" alt=\"Paging library overview\"></p>\n<p>The key components of the Paging 3 architecture include</p>\n<h3>PagingSource</h3>\n<ul>\n<li>The foundational element responsible for loading data in chunks.</li>\n<li>Developers implement a custom PagingSource, defining how to retrieve data from a particular source or use implementation provided by a library supporting Paging 3. (Room can generate a paging source for your data query)</li>\n</ul>\n<h3>Remote Mediator</h3>\n<ul>\n<li>An integral part of the Paging 3 architecture, RemoteMediator manages the coordination between remote data sources, typically backed by a network service, and the local database.</li>\n<li>Responsible for loading pages of data from the network and storing them in the local database, ensuring efficient and reliable pagination.</li>\n</ul>\n<h3>Paging Data</h3>\n<ul>\n<li>Represents the paginated data stream emitted by the PagingSource.</li>\n<li>A Flow of PagingData is observed in the UI layer, enabling dynamic updates as new data is loaded or existing data is invalidated.</li>\n</ul>\n<h3>Pager</h3>\n<ul>\n<li>Coordinates the interaction between the PagingSource and the UI layer.</li>\n<li>Configures the pagination parameters, such as page size, prefetch distance, and initial load size, providing fine-grained control over the loading behavior.</li>\n</ul>\n<h2>Data layer</h2>\n<p>We start with the data layer and define a Retrofit API for fetching movies from network service.</p>\n<div class=\"gatsby-highlight\" data-language=\"kotlin\"><pre class=\"language-kotlin\"><code class=\"language-kotlin\"><span class=\"token keyword\">interface</span> MoviesApi <span class=\"token punctuation\">{</span>\n  <span class=\"token annotation builtin\">@GET</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"/3/discover/movie?language=en-US&amp;sort_by=popularity.desc\"</span><span class=\"token punctuation\">)</span>\n  <span class=\"token keyword\">suspend</span> <span class=\"token keyword\">fun</span> <span class=\"token function\">discover</span><span class=\"token punctuation\">(</span>\n    <span class=\"token annotation builtin\">@Query</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"api_key\"</span><span class=\"token punctuation\">)</span> api_key<span class=\"token operator\">:</span> String<span class=\"token punctuation\">,</span>\n    <span class=\"token annotation builtin\">@Query</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"page\"</span><span class=\"token punctuation\">)</span> page<span class=\"token operator\">:</span> Int<span class=\"token punctuation\">,</span>\n    <span class=\"token annotation builtin\">@Query</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"with_genres\"</span><span class=\"token punctuation\">)</span> genres<span class=\"token operator\">:</span> String<span class=\"token punctuation\">,</span>\n  <span class=\"token punctuation\">)</span><span class=\"token operator\">:</span> Response<span class=\"token operator\">&lt;</span>MoviesNetworkResponse<span class=\"token operator\">></span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>Next, we will leverage the Room library, which is compatible with Paging 3, to create a local paging source. </p>\n<div class=\"gatsby-highlight\" data-language=\"kotlin\"><pre class=\"language-kotlin\"><code class=\"language-kotlin\"><span class=\"token annotation builtin\">@Dao</span>\n<span class=\"token keyword\">interface</span> MoviesDao <span class=\"token punctuation\">{</span>\n  <span class=\"token annotation builtin\">@Query</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"SELECT * FROM movies ORDER BY id ASC\"</span><span class=\"token punctuation\">)</span>\n  <span class=\"token keyword\">fun</span> <span class=\"token function\">getMovies</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token operator\">:</span> DataSource<span class=\"token punctuation\">.</span>Factory<span class=\"token operator\">&lt;</span>Int<span class=\"token punctuation\">,</span> MovieDbEntity<span class=\"token operator\">></span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>It's important to highlight that we utilize <code class=\"language-text\">DataSource.Factory</code> as the return type instead of <code class=\"language-text\">PagingSource</code>. This choice is made specifically because <code class=\"language-text\">DataSource.Factory</code> offers the <code class=\"language-text\">map()</code> method, enabling us to map <code class=\"language-text\">MovieDbEntity</code> instances into domain layer models.</p>\n<p>Now we need to put together local and remote data sources in Remote mediator.</p>\n<ol>\n<li>Determine which page to load depending on the loadType and next page value. Next page number (<code class=\"language-text\">getRemoteKey().nextPage</code>) is stored in the database after each successful network request.</li>\n</ol>\n<div class=\"gatsby-highlight\" data-language=\"kotlin\"><pre class=\"language-kotlin\"><code class=\"language-kotlin\"><span class=\"token keyword\">val</span> page <span class=\"token operator\">=</span> <span class=\"token keyword\">when</span> <span class=\"token punctuation\">(</span>loadType<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n        LoadType<span class=\"token punctuation\">.</span>REFRESH <span class=\"token operator\">-></span> <span class=\"token number\">1</span>\n        LoadType<span class=\"token punctuation\">.</span>PREPEND <span class=\"token operator\">-></span> <span class=\"token keyword\">null</span>\n        LoadType<span class=\"token punctuation\">.</span>APPEND <span class=\"token operator\">-></span> repository<span class=\"token punctuation\">.</span><span class=\"token function\">getRemoteKey</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token operator\">?</span><span class=\"token punctuation\">.</span>nextPage\n      <span class=\"token punctuation\">}</span> <span class=\"token operator\">?:</span> <span class=\"token keyword\">return</span> <span class=\"token function\">Success</span><span class=\"token punctuation\">(</span>endOfPaginationReached <span class=\"token operator\">=</span> <span class=\"token boolean\">true</span><span class=\"token punctuation\">)</span></code></pre></div>\n<p>For bidirectional pagination support, provide the preceding page number for the PREPEND load type. The REFRESH load type is employed when content is refreshed, initiating loading from the initial page.</p>\n<ol start=\"2\">\n<li>Request data from network</li>\n</ol>\n<p>Call our network data source method to fetch a page of data from the server.</p>\n<div class=\"gatsby-highlight\" data-language=\"kotlin\"><pre class=\"language-kotlin\"><code class=\"language-kotlin\"><span class=\"token keyword\">val</span> movies <span class=\"token operator\">=</span> moviesDataSource<span class=\"token punctuation\">.</span><span class=\"token function\">discover</span><span class=\"token punctuation\">(</span>page<span class=\"token punctuation\">)</span></code></pre></div>\n<ol start=\"3\">\n<li>Insert data into the database or refresh database if loadType is refresh</li>\n</ol>\n<p>The RemoteMediator code will appear as follows:</p>\n<div class=\"gatsby-highlight\" data-language=\"kotlin\"><pre class=\"language-kotlin\"><code class=\"language-kotlin\"><span class=\"token keyword\">class</span> <span class=\"token function\">MoviesRemoteMediator</span><span class=\"token punctuation\">(</span>\n  <span class=\"token keyword\">private</span> <span class=\"token keyword\">val</span> repository<span class=\"token operator\">:</span> MoviesRepository<span class=\"token punctuation\">,</span>\n  <span class=\"token keyword\">private</span> <span class=\"token keyword\">val</span> moviesDataSource<span class=\"token operator\">:</span> NetworkMoviesDataSource<span class=\"token punctuation\">,</span>\n<span class=\"token punctuation\">)</span> <span class=\"token operator\">:</span> RemoteMediator<span class=\"token operator\">&lt;</span>Int<span class=\"token punctuation\">,</span> Movie<span class=\"token operator\">></span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n\n  <span class=\"token keyword\">override</span> <span class=\"token keyword\">suspend</span> <span class=\"token keyword\">fun</span> <span class=\"token function\">load</span><span class=\"token punctuation\">(</span>loadType<span class=\"token operator\">:</span> LoadType<span class=\"token punctuation\">,</span> state<span class=\"token operator\">:</span> PagingState<span class=\"token operator\">&lt;</span>Int<span class=\"token punctuation\">,</span> Movie<span class=\"token operator\">></span><span class=\"token punctuation\">)</span><span class=\"token operator\">:</span> MediatorResult <span class=\"token punctuation\">{</span>\n\n    <span class=\"token keyword\">return</span> <span class=\"token keyword\">try</span> <span class=\"token punctuation\">{</span>\n\n      <span class=\"token keyword\">val</span> page <span class=\"token operator\">=</span> <span class=\"token keyword\">when</span> <span class=\"token punctuation\">(</span>loadType<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n        LoadType<span class=\"token punctuation\">.</span>REFRESH <span class=\"token operator\">-></span> <span class=\"token number\">1</span>\n        LoadType<span class=\"token punctuation\">.</span>PREPEND <span class=\"token operator\">-></span> <span class=\"token keyword\">null</span>\n        LoadType<span class=\"token punctuation\">.</span>APPEND <span class=\"token operator\">-></span> repository<span class=\"token punctuation\">.</span><span class=\"token function\">getRemoteKey</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token operator\">?</span><span class=\"token punctuation\">.</span>nextPage\n      <span class=\"token punctuation\">}</span> <span class=\"token operator\">?:</span> <span class=\"token keyword\">return</span> <span class=\"token function\">Success</span><span class=\"token punctuation\">(</span>endOfPaginationReached <span class=\"token operator\">=</span> <span class=\"token boolean\">true</span><span class=\"token punctuation\">)</span>\n\n      <span class=\"token keyword\">val</span> movies <span class=\"token operator\">=</span> moviesDataSource<span class=\"token punctuation\">.</span><span class=\"token function\">getMovies</span><span class=\"token punctuation\">(</span>page<span class=\"token punctuation\">)</span>\n\n      <span class=\"token keyword\">val</span> nextPage <span class=\"token operator\">=</span> <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span>movies<span class=\"token punctuation\">.</span><span class=\"token function\">isNotEmpty</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span> page <span class=\"token operator\">+</span> <span class=\"token number\">1</span> <span class=\"token keyword\">else</span> <span class=\"token keyword\">null</span>\n\n      repository<span class=\"token punctuation\">.</span><span class=\"token function\">insertMovies</span><span class=\"token punctuation\">(</span>movies<span class=\"token punctuation\">,</span> nextPage<span class=\"token punctuation\">,</span> loadType <span class=\"token operator\">==</span> LoadType<span class=\"token punctuation\">.</span>REFRESH<span class=\"token punctuation\">)</span>\n\n      <span class=\"token function\">Success</span><span class=\"token punctuation\">(</span>endOfPaginationReached <span class=\"token operator\">=</span> movies<span class=\"token punctuation\">.</span><span class=\"token function\">isEmpty</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span>\n\n    <span class=\"token punctuation\">}</span> <span class=\"token keyword\">catch</span> <span class=\"token punctuation\">(</span>e<span class=\"token operator\">:</span> Exception<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n      MediatorResult<span class=\"token punctuation\">.</span><span class=\"token function\">Error</span><span class=\"token punctuation\">(</span>e<span class=\"token punctuation\">)</span>\n    <span class=\"token punctuation\">}</span>\n  <span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>RemoteMediator uses the repository to store movies list and next page number in the database.\nRepository also takes care of clearing the database table when data is refreshed:</p>\n<div class=\"gatsby-highlight\" data-language=\"kotlin\"><pre class=\"language-kotlin\"><code class=\"language-kotlin\"><span class=\"token keyword\">class</span> <span class=\"token function\">MoviesRepositoryImpl</span><span class=\"token punctuation\">(</span>\n  <span class=\"token keyword\">private</span> <span class=\"token keyword\">val</span> database<span class=\"token operator\">:</span> MoviesDatabase\n<span class=\"token punctuation\">)</span><span class=\"token operator\">:</span> MoviesRepository <span class=\"token punctuation\">{</span>\n\n  <span class=\"token keyword\">override</span> <span class=\"token keyword\">fun</span> <span class=\"token function\">getMovies</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=</span> database<span class=\"token punctuation\">.</span><span class=\"token function\">movies</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">getMovies</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">map</span> <span class=\"token punctuation\">{</span> it<span class=\"token punctuation\">.</span><span class=\"token function\">toDomain</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">}</span>\n\n  <span class=\"token keyword\">override</span> <span class=\"token keyword\">suspend</span> <span class=\"token keyword\">fun</span> <span class=\"token function\">insertMovies</span><span class=\"token punctuation\">(</span>movies<span class=\"token operator\">:</span> List<span class=\"token operator\">&lt;</span>Movie<span class=\"token operator\">></span><span class=\"token punctuation\">,</span> nextPage<span class=\"token operator\">:</span> Int<span class=\"token operator\">?</span><span class=\"token punctuation\">,</span> clear<span class=\"token operator\">:</span> Boolean<span class=\"token punctuation\">)</span><span class=\"token punctuation\">{</span>\n\n    database<span class=\"token punctuation\">.</span><span class=\"token function\">withTransaction</span> <span class=\"token punctuation\">{</span>\n      <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span>clear<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n        database<span class=\"token punctuation\">.</span><span class=\"token function\">movies</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">clear</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n      <span class=\"token punctuation\">}</span>\n\n      database<span class=\"token punctuation\">.</span><span class=\"token function\">movies</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">putMovies</span><span class=\"token punctuation\">(</span>movies<span class=\"token punctuation\">.</span><span class=\"token function\">map</span> <span class=\"token punctuation\">{</span> it<span class=\"token punctuation\">.</span><span class=\"token function\">toDbEntity</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span>\n\n      <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span>nextPage <span class=\"token operator\">!=</span> <span class=\"token keyword\">null</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n        database<span class=\"token punctuation\">.</span><span class=\"token function\">remoteKey</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">insertKey</span><span class=\"token punctuation\">(</span><span class=\"token function\">RemoteKey</span><span class=\"token punctuation\">(</span>MoviesDatabase<span class=\"token punctuation\">.</span>MOVIES_REMOTE_KEY<span class=\"token punctuation\">,</span> nextPage<span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span>\n      <span class=\"token punctuation\">}</span>\n    <span class=\"token punctuation\">}</span>\n  <span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<h2>UI layer</h2>\n<p>The Pager object serves as a bridge between remote and local data sources. It is managing the page loading, prefetching, and handling the transitions between various data sources to provide a seamless and responsive flow of paginated content within your application. You can configure the page loading behavior with the following parameters:</p>\n<ul>\n<li><strong>pageSize</strong> parameter defines the number of items loaded in each page of data.</li>\n<li><strong>initialLoadSize</strong> is the number of items loaded initially when the paging library is first initialized.</li>\n<li><strong>prefetchDistance</strong> parameter determines the distance from the edge of the loaded content at which the next page should start loading.</li>\n</ul>\n<div class=\"gatsby-highlight\" data-language=\"kotlin\"><pre class=\"language-kotlin\"><code class=\"language-kotlin\"><span class=\"token keyword\">class</span> <span class=\"token function\">MoviesScreenViewModel</span><span class=\"token punctuation\">(</span>\n  moviesSource<span class=\"token operator\">:</span> MoviesRemoteMediator<span class=\"token punctuation\">,</span>\n  repository<span class=\"token operator\">:</span> MoviesRepository<span class=\"token punctuation\">,</span>\n  <span class=\"token keyword\">private</span> <span class=\"token keyword\">val</span> movieDetails<span class=\"token operator\">:</span> MovieDetailsApi<span class=\"token punctuation\">,</span>\n<span class=\"token punctuation\">)</span> <span class=\"token operator\">:</span> <span class=\"token function\">ViewModel</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n   <span class=\"token keyword\">val</span> moviesFlow <span class=\"token operator\">=</span> <span class=\"token function\">Pager</span><span class=\"token punctuation\">(</span>\n      config <span class=\"token operator\">=</span> <span class=\"token function\">PagingConfig</span><span class=\"token punctuation\">(</span>\n        pageSize <span class=\"token operator\">=</span> PAGE_SIZE<span class=\"token punctuation\">,</span>\n        prefetchDistance <span class=\"token operator\">=</span> PREFETCH_DISTANCE<span class=\"token punctuation\">,</span>\n        initialLoadSize <span class=\"token operator\">=</span> INITIAL_LOAD<span class=\"token punctuation\">,</span>\n      <span class=\"token punctuation\">)</span><span class=\"token punctuation\">,</span>\n      remoteMediator <span class=\"token operator\">=</span> moviesSource<span class=\"token punctuation\">,</span>\n      pagingSourceFactory <span class=\"token operator\">=</span> repository<span class=\"token punctuation\">.</span><span class=\"token function\">getMovies</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">asPagingSourceFactory</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n    <span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span>flow<span class=\"token punctuation\">.</span><span class=\"token function\">cachedIn</span><span class=\"token punctuation\">(</span>viewModelScope<span class=\"token punctuation\">)</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<h3>Consuming paging data in Jetpack Compose UI</h3>\n<p>Now, let's explore how to consume the paginated data in your Compose UI.</p>\n<p>Use the <code class=\"language-text\">collectAsLazyPagingItems</code> extension function to collect the <code class=\"language-text\">PagingData</code> and observe changes.</p>\n<div class=\"gatsby-highlight\" data-language=\"kotlin\"><pre class=\"language-kotlin\"><code class=\"language-kotlin\"><span class=\"token keyword\">val</span> movies <span class=\"token operator\">=</span> viewModel<span class=\"token punctuation\">.</span>moviesFlow<span class=\"token punctuation\">.</span><span class=\"token function\">collectAsLazyPagingItems</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span></code></pre></div>\n<p>Use the <code class=\"language-text\">LazyColumn</code> or <code class=\"language-text\">LazyVerticalGrid</code> to efficiently display paginated data in your Compose UI.</p>\n<div class=\"gatsby-highlight\" data-language=\"kotlin\"><pre class=\"language-kotlin\"><code class=\"language-kotlin\"><span class=\"token function\">LazyVerticalGrid</span><span class=\"token punctuation\">(</span>\n      modifier <span class=\"token operator\">=</span> Modifier\n        <span class=\"token punctuation\">.</span><span class=\"token function\">fillMaxSize</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n        <span class=\"token punctuation\">.</span><span class=\"token function\">padding</span><span class=\"token punctuation\">(</span><span class=\"token number\">16</span><span class=\"token punctuation\">.</span>dp<span class=\"token punctuation\">)</span><span class=\"token punctuation\">,</span>\n      columns <span class=\"token operator\">=</span> GridCells<span class=\"token punctuation\">.</span><span class=\"token function\">Fixed</span><span class=\"token punctuation\">(</span><span class=\"token number\">3</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">,</span>\n      horizontalArrangement <span class=\"token operator\">=</span> Arrangement<span class=\"token punctuation\">.</span><span class=\"token function\">spacedBy</span><span class=\"token punctuation\">(</span><span class=\"token number\">16</span><span class=\"token punctuation\">.</span>dp<span class=\"token punctuation\">)</span><span class=\"token punctuation\">,</span>\n      verticalArrangement <span class=\"token operator\">=</span> Arrangement<span class=\"token punctuation\">.</span><span class=\"token function\">spacedBy</span><span class=\"token punctuation\">(</span><span class=\"token number\">16</span><span class=\"token punctuation\">.</span>dp<span class=\"token punctuation\">)</span><span class=\"token punctuation\">,</span>\n    <span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n\n      <span class=\"token function\">items</span><span class=\"token punctuation\">(</span>\n        count <span class=\"token operator\">=</span> movies<span class=\"token punctuation\">.</span>itemCount<span class=\"token punctuation\">,</span>\n        key <span class=\"token operator\">=</span> movies<span class=\"token punctuation\">.</span><span class=\"token function\">itemKey</span> <span class=\"token punctuation\">{</span> it<span class=\"token punctuation\">.</span>id <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n      <span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span> index <span class=\"token operator\">-></span>\n\n        movies<span class=\"token punctuation\">[</span>index<span class=\"token punctuation\">]</span><span class=\"token operator\">?</span><span class=\"token punctuation\">.</span><span class=\"token function\">let</span> <span class=\"token punctuation\">{</span> movie <span class=\"token operator\">-></span>\n          <span class=\"token function\">MovieCardSmall</span><span class=\"token punctuation\">(</span>\n            movie <span class=\"token operator\">=</span> movie<span class=\"token punctuation\">,</span>\n            onClick <span class=\"token operator\">=</span> <span class=\"token punctuation\">{</span> <span class=\"token function\">onClick</span><span class=\"token punctuation\">(</span>movie<span class=\"token punctuation\">.</span>id<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n          <span class=\"token punctuation\">)</span>\n        <span class=\"token punctuation\">}</span>\n      <span class=\"token punctuation\">}</span>\n    <span class=\"token punctuation\">}</span></code></pre></div>\n<hr>\n<blockquote>\n<p><em>Note the usage of the key property for items. The key property uniquely identifies each item, allowing Compose to efficiently update and recycle composables as data changes.</em></p>\n</blockquote>\n<hr>\n<h3>Paging state handling</h3>\n<p>LazyPagingItems offers insights into the ongoing data loading process, allowing you to effectively manage errors during pagination. By examining the loadState.append property, you can determine the loading state when appending new items and present a user-friendly indication of the current status.</p>\n<p>For instance, the following code snippet demonstrates how to react to different loading states during item appending:</p>\n<div class=\"gatsby-highlight\" data-language=\"kotlin\"><pre class=\"language-kotlin\"><code class=\"language-kotlin\"><span class=\"token keyword\">when</span> <span class=\"token punctuation\">(</span>movies<span class=\"token punctuation\">.</span>loadState<span class=\"token punctuation\">.</span>append<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n  <span class=\"token keyword\">is</span> LoadState<span class=\"token punctuation\">.</span>Loading <span class=\"token operator\">-></span> <span class=\"token punctuation\">{</span> <span class=\"token function\">ProgressFooter</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">}</span>\n  <span class=\"token keyword\">is</span> LoadState<span class=\"token punctuation\">.</span>Error <span class=\"token operator\">-></span> <span class=\"token punctuation\">{</span> <span class=\"token function\">ErrorFooter</span><span class=\"token punctuation\">(</span>onRetryClick <span class=\"token operator\">=</span> <span class=\"token punctuation\">{</span> movies<span class=\"token punctuation\">.</span><span class=\"token function\">retry</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">}</span> <span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>Similarly, you can assess the loading status when the list data is refreshed by checking the loadState.refresh property. Extension functions isError() and isLoading() provide convenient checks for error and loading states, respectively.</p>\n<div class=\"gatsby-highlight\" data-language=\"kotlin\"><pre class=\"language-kotlin\"><code class=\"language-kotlin\"><span class=\"token keyword\">fun</span> LazyPagingItems<span class=\"token operator\">&lt;</span>Movie<span class=\"token operator\">></span><span class=\"token punctuation\">.</span><span class=\"token function\">isError</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token operator\">:</span> Boolean <span class=\"token operator\">=</span> \n    loadState<span class=\"token punctuation\">.</span>refresh <span class=\"token keyword\">is</span> LoadState<span class=\"token punctuation\">.</span>Error <span class=\"token operator\">&amp;&amp;</span> itemSnapshotList<span class=\"token punctuation\">.</span><span class=\"token function\">isEmpty</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n\n<span class=\"token keyword\">fun</span> LazyPagingItems<span class=\"token operator\">&lt;</span>Movie<span class=\"token operator\">></span><span class=\"token punctuation\">.</span><span class=\"token function\">isLoading</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token operator\">:</span> Boolean <span class=\"token operator\">=</span> \n    loadState<span class=\"token punctuation\">.</span>refresh <span class=\"token keyword\">is</span> LoadState<span class=\"token punctuation\">.</span>Loading <span class=\"token operator\">&amp;&amp;</span> itemSnapshotList<span class=\"token punctuation\">.</span><span class=\"token function\">isEmpty</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span></code></pre></div>\n<p>Incorporate these checks into your UI logic to display appropriate screens based on the loading or error states. The following code snippet demonstrates how to handle loading and error states along with providing a retry option:</p>\n<div class=\"gatsby-highlight\" data-language=\"kotlin\"><pre class=\"language-kotlin\"><code class=\"language-kotlin\"><span class=\"token keyword\">when</span> <span class=\"token punctuation\">{</span>\n   movies<span class=\"token punctuation\">.</span><span class=\"token function\">isLoading</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">-></span> <span class=\"token function\">LoadingScreen</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n   movies<span class=\"token punctuation\">.</span><span class=\"token function\">isError</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">-></span> ErrorScreen <span class=\"token punctuation\">{</span> movies<span class=\"token punctuation\">.</span><span class=\"token function\">retry</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">}</span>\n   <span class=\"token keyword\">else</span> <span class=\"token operator\">-></span> <span class=\"token function\">MoviesGrid</span><span class=\"token punctuation\">(</span>movies <span class=\"token operator\">=</span> movies<span class=\"token punctuation\">,</span> onClick <span class=\"token operator\">=</span> viewModel<span class=\"token operator\">::</span>onMovieClick<span class=\"token punctuation\">)</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>Additionally, <code class=\"language-text\">LazyPagingItems</code> offers a convenient <code class=\"language-text\">retry()</code> method, allowing you to retry the last failed data loading attempt with ease.</p>\n<h3>Adding filters</h3>\n<p>Now lets add filters to our movie list to see paginated list of movies filtered by genre.</p>\n<p><img src=\"/static/paging_demo_filters-ff80a7606fb30961e2bda56bd6ed9985.gif\" alt=\"Filters UI\"></p>\n<p>We start with requesting genres from the server and saving them in our database. We will keep genre data and selection in the database so that we can observe it from our domain and UI layers.</p>\n<div class=\"gatsby-highlight\" data-language=\"kotlin\"><pre class=\"language-kotlin\"><code class=\"language-kotlin\"><span class=\"token keyword\">interface</span> MoviesApi <span class=\"token punctuation\">{</span>\n  <span class=\"token annotation builtin\">@GET</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"/3/genre/movie/list?language=en-US\"</span><span class=\"token punctuation\">)</span>\n  <span class=\"token keyword\">suspend</span> <span class=\"token keyword\">fun</span> <span class=\"token function\">genres</span><span class=\"token punctuation\">(</span><span class=\"token annotation builtin\">@Query</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"api_key\"</span><span class=\"token punctuation\">)</span> api_key<span class=\"token operator\">:</span> String<span class=\"token punctuation\">)</span><span class=\"token operator\">:</span> Response<span class=\"token operator\">&lt;</span>GenresNetworkResponse<span class=\"token operator\">></span>\n<span class=\"token punctuation\">}</span>\n\n<span class=\"token annotation builtin\">@Dao</span>\n<span class=\"token keyword\">interface</span> GenresDao <span class=\"token punctuation\">{</span>\n\n  <span class=\"token annotation builtin\">@Insert</span><span class=\"token punctuation\">(</span>onConflict <span class=\"token operator\">=</span> OnConflictStrategy<span class=\"token punctuation\">.</span>REPLACE<span class=\"token punctuation\">)</span>\n  <span class=\"token keyword\">suspend</span> <span class=\"token keyword\">fun</span> <span class=\"token function\">putGenres</span><span class=\"token punctuation\">(</span>genres<span class=\"token operator\">:</span> List<span class=\"token operator\">&lt;</span>GenreDbEntity<span class=\"token operator\">></span><span class=\"token punctuation\">)</span>\n\n  <span class=\"token annotation builtin\">@Upsert</span><span class=\"token punctuation\">(</span>entity <span class=\"token operator\">=</span> GenreDbEntity<span class=\"token operator\">::</span><span class=\"token keyword\">class</span><span class=\"token punctuation\">)</span>\n  <span class=\"token keyword\">suspend</span> <span class=\"token keyword\">fun</span> <span class=\"token function\">updateGenres</span><span class=\"token punctuation\">(</span>genres<span class=\"token operator\">:</span> List<span class=\"token operator\">&lt;</span>GenreDbUpdateEntity<span class=\"token operator\">></span><span class=\"token punctuation\">)</span>\n\n  <span class=\"token annotation builtin\">@Query</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"SELECT * FROM genres\"</span><span class=\"token punctuation\">)</span>\n  <span class=\"token keyword\">suspend</span> <span class=\"token keyword\">fun</span> <span class=\"token function\">getGenres</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token operator\">:</span> List<span class=\"token operator\">&lt;</span>GenreDbEntity<span class=\"token operator\">></span>\n\n  <span class=\"token annotation builtin\">@Query</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"SELECT * FROM genres\"</span><span class=\"token punctuation\">)</span>\n  <span class=\"token keyword\">fun</span> <span class=\"token function\">observeGenres</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token operator\">:</span> Flow<span class=\"token operator\">&lt;</span>List<span class=\"token operator\">&lt;</span>GenreDbEntity<span class=\"token operator\">></span><span class=\"token operator\">></span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>Now we can add <code class=\"language-text\">with_genres</code> parameter to our network request:</p>\n<div class=\"gatsby-highlight\" data-language=\"kotlin\"><pre class=\"language-kotlin\"><code class=\"language-kotlin\">  <span class=\"token annotation builtin\">@GET</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"/3/discover/movie?language=en-US&amp;sort_by=popularity.desc\"</span><span class=\"token punctuation\">)</span>\n  <span class=\"token keyword\">suspend</span> <span class=\"token keyword\">fun</span> <span class=\"token function\">discover</span><span class=\"token punctuation\">(</span>\n    <span class=\"token annotation builtin\">@Query</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"page\"</span><span class=\"token punctuation\">)</span> page<span class=\"token operator\">:</span> Int<span class=\"token punctuation\">,</span>\n    <span class=\"token annotation builtin\">@Query</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"with_genres\"</span><span class=\"token punctuation\">)</span> genres<span class=\"token operator\">:</span> String<span class=\"token punctuation\">,</span>\n    <span class=\"token annotation builtin\">@Query</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"api_key\"</span><span class=\"token punctuation\">)</span> api_key<span class=\"token operator\">:</span> String<span class=\"token punctuation\">,</span>\n  <span class=\"token punctuation\">)</span><span class=\"token operator\">:</span> Response<span class=\"token operator\">&lt;</span>MoviesNetworkResponse<span class=\"token operator\">></span></code></pre></div>\n<p>Remote mediator will get selected genres from the repository</p>\n<div class=\"gatsby-highlight\" data-language=\"kotlin\"><pre class=\"language-kotlin\"><code class=\"language-kotlin\">  <span class=\"token operator\">..</span><span class=\"token punctuation\">.</span>\n  <span class=\"token keyword\">val</span> genres <span class=\"token operator\">=</span> repository<span class=\"token punctuation\">.</span><span class=\"token function\">getGenreSelection</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n  <span class=\"token keyword\">val</span> movies <span class=\"token operator\">=</span> moviesDataSource<span class=\"token punctuation\">.</span><span class=\"token function\">getMovies</span><span class=\"token punctuation\">(</span>page<span class=\"token punctuation\">,</span> genres<span class=\"token punctuation\">)</span>\n  <span class=\"token operator\">..</span><span class=\"token punctuation\">.</span></code></pre></div>\n<p>Every time user selects a new filter we stave it in the database and restart pagination by clearing movies in the database and resetting the page to 1.</p>\n<div class=\"gatsby-highlight\" data-language=\"kotlin\"><pre class=\"language-kotlin\"><code class=\"language-kotlin\"><span class=\"token keyword\">class</span> MoviesScreenViewModel <span class=\"token punctuation\">{</span>\n\n  <span class=\"token keyword\">fun</span> <span class=\"token function\">onGenreClick</span><span class=\"token punctuation\">(</span>genreItem<span class=\"token operator\">:</span> GenreItem<span class=\"token punctuation\">)</span> <span class=\"token operator\">=</span> viewModelScope<span class=\"token punctuation\">.</span><span class=\"token function\">launch</span> <span class=\"token punctuation\">{</span>\n    genresProvider<span class=\"token punctuation\">.</span><span class=\"token function\">setSelection</span><span class=\"token punctuation\">(</span>genreItem<span class=\"token punctuation\">.</span>id<span class=\"token punctuation\">,</span> <span class=\"token operator\">!</span>genreItem<span class=\"token punctuation\">.</span>isSelected<span class=\"token punctuation\">)</span>\n    repository<span class=\"token punctuation\">.</span><span class=\"token function\">clearMovies</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n  <span class=\"token punctuation\">}</span>\n  \n<span class=\"token punctuation\">}</span></code></pre></div>\n<div class=\"gatsby-highlight\" data-language=\"kotlin\"><pre class=\"language-kotlin\"><code class=\"language-kotlin\"><span class=\"token keyword\">class</span> MoviesRepositoryImpl <span class=\"token punctuation\">{</span>\n\n  <span class=\"token keyword\">override</span> <span class=\"token keyword\">suspend</span> <span class=\"token keyword\">fun</span> <span class=\"token function\">clearMovies</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=</span> <span class=\"token function\">withContext</span><span class=\"token punctuation\">(</span>Dispatchers<span class=\"token punctuation\">.</span>IO<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    database<span class=\"token punctuation\">.</span><span class=\"token function\">withTransaction</span> <span class=\"token punctuation\">{</span>\n      database<span class=\"token punctuation\">.</span><span class=\"token function\">movies</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">clear</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n      database<span class=\"token punctuation\">.</span><span class=\"token function\">remoteKey</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">.</span><span class=\"token function\">insertKey</span><span class=\"token punctuation\">(</span><span class=\"token function\">RemoteKey</span><span class=\"token punctuation\">(</span>MoviesDatabase<span class=\"token punctuation\">.</span>MOVIES_REMOTE_KEY<span class=\"token punctuation\">,</span> <span class=\"token number\">1</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span>\n    <span class=\"token punctuation\">}</span>\n  <span class=\"token punctuation\">}</span>\n\n<span class=\"token punctuation\">}</span></code></pre></div>\n<h3>Links</h3>\n<p><br>Source code: <a href=\"https://github.com/auto1-oss/android-compose-paging\">https://github.com/auto1-oss/android-compose-paging</a></br>\n<br>Official documentation: <a href=\"https://developer.android.com/topic/libraries/architecture/paging/v3-overview\">https://developer.android.com/topic/libraries/architecture/paging/v3-overview</a></br></p>","fields":{"slug":"/android-compose-pagination/","tags":["auto1","mobile","Android"]}}},{"node":{"id":"2cda3518-ba96-5257-97f4-6f0ec1f69eb1","frontmatter":{"category":"Engineering","title":"Bug Police Officer","date":"2023-11-21","summary":"What I present today is how to use police techniques and their mindset to detect and solve bugs in software, so that you can be the best software developer that society can provide.","thumbnail":{"relativePath":"pages/bug-police-officer/bug-police-officer.png","childImageSharp":{"resolutions":{"base64":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAOCAYAAAAvxDzwAAAACXBIWXMAAAsSAAALEgHS3X78AAAC20lEQVQ4y32S208TURCHF6rvvPpI/Ad8MzFIIlESaAuoAZVehEppd7fQFqjhIgiBQhEUaoFy6wUCghRrCFKQi4orhgQ0XOIFA8FoqOKTaHyg7e54zpYWUOsks2fOmd98Z85mCOIPqyNtUQf33Xp7XOZl+7oEua3AfvpgzkTZo4hIVq9oC8PQGluh7M4hpdYh0fl2aFFfh1a1AcQX2oGUWV0ol2si7bH/auAQsFptE+C4VesUZUnscPxMMxyLa2FXikXsKnIc4zOca9M5hVhbs1cT0fKzuniBkbTnSqU2iBeaA/KMZg47jmVSB9RSDjXW6BXdgv8+mf8vpDMar02qzlMG+gGkKz1cunI06Lkergid3VF1xR3URrRG2k48HZziRf31wxVrzCI01I4HZNQYh73RNBF4P7sAvUZXGdZMOMajcU1E2G3aRjDuJ4I9oAV+fQPY2fB9fbsE2OHHhg9+bkOP0dWENdP9k4JbtIP4C2qinfzq0LQTN8i+I8HTE0eZoak18H8B3/dN1rezyeKYGZx8h5L8pVjbr7EcYoTpnryG8AVLhuqYLk2Dtir/nnfrzQqA7xMLvs/s1uoylObd91ZRvboPRZUxIX2oNtxpB+osZDV0j44mR7wfKy+CRVsHGsM0++oxAwsTL8BgGGUTVAxc0r0EinJ7jXSvLlQXZmBqCTUQHBW6h1YWMhBPLkKfvsi/XprAJimfQbLCw4kVY9xZ5Qxco0ZZCenxn6MYyCmcAwSlcS1m8B3yHzXw066nh8dTtfOQSD3fldETsF12Emq1Fi6BmueSNLNcJjXDjZVL4GEJCSJybjcFaQtQDd8ZYuwDCQgOtWpgMl35CMRZrkCi3MMN5athuVgIomwPpGS7QCx3Q7WmGKrIm1ySfCSAZhNwTfCtQOwDY8v5DrVXO9wZGZ2Qkmb2J6fe5SipGV6XXIE8tKI9CNPMIEy1otjKiZAGa3ENz0MMzPoNzyOqI3a+TN4AAAAASUVORK5CYII=","width":460,"height":325,"src":"/static/19a57f273a6f7441d951955ce11fae27/b3029/bug-police-officer.png","srcSet":"/static/19a57f273a6f7441d951955ce11fae27/b3029/bug-police-officer.png 1x"}}},"authorName":"Bruno Morais","authorDescription":"Bruno is a Senior Software Engineer at AUTO1 Group.","authorAvatar":{"relativePath":"pages/bug-police-officer/avatar.jpeg","childImageSharp":{"resolutions":{"base64":"data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAUABQDASIAAhEBAxEB/8QAGAABAQEBAQAAAAAAAAAAAAAAAAQDAQX/xAAXAQEBAQEAAAAAAAAAAAAAAAABAwIE/9oADAMBAAIQAxAAAAGmbSadPSZMPIBLBy1//8QAHBAAAwABBQAAAAAAAAAAAAAAAQIDIQAQERIj/9oACAEBAAEFAqOVEql2047LLDc7A+i5H//EABcRAQEBAQAAAAAAAAAAAAAAAAEAEBL/2gAIAQMBAT8BC5jP/8QAGREAAwADAAAAAAAAAAAAAAAAAAECEBET/9oACAECAQE/AW9HSSsf/8QAGhAAAQUBAAAAAAAAAAAAAAAAAQACEBEhQf/aAAgBAQAGPwLBZRa4VFYt5Jj/xAAcEAADAAIDAQAAAAAAAAAAAAAAAREhURAxYXH/2gAIAQEAAT8ho6G+i0ElVBP0c6AzfMaH3OHLLoTIz//aAAwDAQACAAMAAAAQcC98/8QAGREAAwADAAAAAAAAAAAAAAAAABExEFGB/9oACAEDAQE/EGQbRPcf/8QAGxEAAgIDAQAAAAAAAAAAAAAAAAERMVFhscH/2gAIAQIBAT8QuM2dElucejs//8QAGxABAQEBAAMBAAAAAAAAAAAAAREAITFBYXH/2gAIAQEAAT8QEiRCoPrgpVComQJxnQidLfOE1gCkvzQ9j4TPv80LcHh6dw+UV3//2Q==","width":50,"height":50,"src":"/static/8d7223890dda036e0121799406a35506/d2d31/avatar.jpeg","srcSet":"/static/8d7223890dda036e0121799406a35506/d2d31/avatar.jpeg 1x,\n/static/8d7223890dda036e0121799406a35506/0b804/avatar.jpeg 1.5x,\n/static/8d7223890dda036e0121799406a35506/753c3/avatar.jpeg 2x,\n/static/8d7223890dda036e0121799406a35506/31ca8/avatar.jpeg 3x"}}},"headerImage":{"relativePath":"pages/bug-police-officer/bug-police-officer.png","childImageSharp":{"resolutions":{"base64":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAOCAYAAAAvxDzwAAAACXBIWXMAAAsSAAALEgHS3X78AAAC20lEQVQ4y32S208TURCHF6rvvPpI/Ad8MzFIIlESaAuoAZVehEppd7fQFqjhIgiBQhEUaoFy6wUCghRrCFKQi4orhgQ0XOIFA8FoqOKTaHyg7e54zpYWUOsks2fOmd98Z85mCOIPqyNtUQf33Xp7XOZl+7oEua3AfvpgzkTZo4hIVq9oC8PQGluh7M4hpdYh0fl2aFFfh1a1AcQX2oGUWV0ol2si7bH/auAQsFptE+C4VesUZUnscPxMMxyLa2FXikXsKnIc4zOca9M5hVhbs1cT0fKzuniBkbTnSqU2iBeaA/KMZg47jmVSB9RSDjXW6BXdgv8+mf8vpDMar02qzlMG+gGkKz1cunI06Lkergid3VF1xR3URrRG2k48HZziRf31wxVrzCI01I4HZNQYh73RNBF4P7sAvUZXGdZMOMajcU1E2G3aRjDuJ4I9oAV+fQPY2fB9fbsE2OHHhg9+bkOP0dWENdP9k4JbtIP4C2qinfzq0LQTN8i+I8HTE0eZoak18H8B3/dN1rezyeKYGZx8h5L8pVjbr7EcYoTpnryG8AVLhuqYLk2Dtir/nnfrzQqA7xMLvs/s1uoylObd91ZRvboPRZUxIX2oNtxpB+osZDV0j44mR7wfKy+CRVsHGsM0++oxAwsTL8BgGGUTVAxc0r0EinJ7jXSvLlQXZmBqCTUQHBW6h1YWMhBPLkKfvsi/XprAJimfQbLCw4kVY9xZ5Qxco0ZZCenxn6MYyCmcAwSlcS1m8B3yHzXw066nh8dTtfOQSD3fldETsF12Emq1Fi6BmueSNLNcJjXDjZVL4GEJCSJybjcFaQtQDd8ZYuwDCQgOtWpgMl35CMRZrkCi3MMN5athuVgIomwPpGS7QCx3Q7WmGKrIm1ySfCSAZhNwTfCtQOwDY8v5DrVXO9wZGZ2Qkmb2J6fe5SipGV6XXIE8tKI9CNPMIEy1otjKiZAGa3ENz0MMzPoNzyOqI3a+TN4AAAAASUVORK5CYII=","width":600,"height":424,"src":"/static/19a57f273a6f7441d951955ce11fae27/b7ca8/bug-police-officer.png","srcSet":"/static/19a57f273a6f7441d951955ce11fae27/b7ca8/bug-police-officer.png 1x"}}}},"html":"<h3>Table of contents:</h3>\n<ol>\n<li>\n<p>Sense of Security</p>\n<ol>\n<li>Why does feeling safe matter?</li>\n<li>Strategic actions</li>\n<li>A test as a Cop</li>\n</ol>\n</li>\n<li>\n<p>Treat the injured first</p>\n<ol>\n<li>Prioritizing what matters</li>\n<li>The appropriate Mental Model</li>\n<li>Ask before trying to find out</li>\n</ol>\n</li>\n<li>\n<p>Trust your tools</p>\n<ol>\n<li>When less is more</li>\n<li>Efficient distribution of efforts</li>\n<li>Confidence that avoids shame</li>\n</ol>\n</li>\n<li>\n<p>Communication via Radio</p>\n<ol>\n<li>Q-code</li>\n<li>The Language and the Domain</li>\n</ol>\n</li>\n<li>Bug is in jail</li>\n</ol>\n<p><strong>What I present today is how to use police techniques and their mindset to detect and solve bugs in software, so that you can be the best software developer that society can provide.</strong></p>\n<p>For some years, I worked as a police officer in Brazil and I can say that it is a challenging career. The first step was to pass a national test, where knowledge of various laws was required. Criminal laws, traffic laws, and even the laws of physics.</p>\n<p>The second step was to pass a battery of medical exams. Almost perfect health was necessary to be able to practice the profession, in addition to having most of the parts of the human body.</p>\n<p>The third step was the psychological examination. It was necessary to have a clear mind and not have impulses different from those expected from a person who must protect other people.</p>\n<p>As a fourth part, there was also the fitness test, a test in which career candidates had to prove they had sufficient strength and speed to carry out daily police tasks. Running, pull-ups, sit-ups and long jump are examples of what was required.</p>\n<p>The last stage was the Police Academy, where for three months those from now on known as students would learn and prove the learning of several new skills: first aid, use of firearms (pistols, carbines, submachine guns, etc.), non-lethal weapons, martial arts, public policy, and many other disciplines.</p>\n<p>All this so that society receives the best police officer that its people can provide. So, what about using some of the following principles and techniques to become the best software developer that society can provide?</p>\n<h2>Sense of Security</h2>\n<h3>Why does feeling safe matter?</h3>\n<p>Without needing to be a security expert, you already know that it is impossible to have a completely safe city. Even if we reached the point of having perfect police officers, which in itself is impossible, we would not have a police officer for every citizen. In general, the recommendation is 300 police officers for every 100 thousand inhabitants.</p>\n<p>Aware of this limitation, that it is not possible to guarantee complete security for all citizens, police agencies work with the concept of a sense of security. Through strategic actions to combat crime, citizens' feeling that they are safe is increased. This mental condition gives them peace of mind when working, studying, and carrying out their daily tasks.</p>\n<h3>Strategic actions</h3>\n<p>This sense of security also affects criminals. The common visibility of police officers carrying out their duties in strategic places, as well as effective actions to combat crime, make criminals stay away from that place.</p>\n<p>Notice that ending 100% of crime is impossible, what we do well is limit its actions. From here we can extract our first lesson when dealing with software: perfect code, totally bug-free, is impossible.</p>\n<p>Despite this conclusion, the fact that it is impossible does not mean that we should simply ignore the problem. On the contrary, we can, like the police, create strategic actions to combat Bugs. To the point that Bugs themselves will run away from our code.</p>\n<h3>A test as a Cop</h3>\n<p>Test Driven Development - TDD is a strategic action that provides a feeling of security in your code. Before the crime occurs, you position police officers in a specific region. Before the Bug occurs, you position the test in a specific place in your code.</p>\n<p>Robert C. Martin, in his book \"Clean Code: A HandBook of Agile Software Craftsmanship\", teaches us that writing unit tests before writing production code is just the tip of the iceberg in defining TDD. There must be a continuous cycle of improvement, in which there is always a test covering the code you are developing and this security that the code is protected allows you to play with it, and work with it. After all, whatever problem may happen, there is a police-test there to protect you.</p>\n<h2>Treat the injured first</h2>\n<h3>Prioritizing what matters</h3>\n<p>One of the functions of Highway Police Officers is to provide first aid in the event of traffic accidents. There are often serious injuries that must be treated as urgently as possible, as even 1 minute can make the difference between life and death.</p>\n<p>The first step when arriving at a traffic accident site is to mark the location appropriately to prevent other accidents. We must use lighting devices and other appliances to make it clear to other vehicles that there is a danger there.</p>\n<p>The police officer must then check the condition of all the injured to prioritize them according to the severity of each situation.</p>\n<p>Once the treatment of each injured person has been completed and, if necessary, they have been taken to the hospital, it is time to survey the accident site. At this stage, the police officer begins to collect all the information relating to the accident to have sufficient information so that a decision can be made on the cause of the accident and, when applicable, any culprits.</p>\n<h3>The appropriate Mental Model</h3>\n<p>This is the mental model that you and I must adopt when we are faced with a Bug. Although several procedures have been adopted to prevent it from occurring, it is a fatality that can happen. It's like when the highway was well designed, the vehicles were up to date with maintenance, but even so, due to carelessness or misunderstanding on the part of the driver, an accident happens.</p>\n<p>As in the case of traffic accidents, the first step to take is to secure the environment so that the Bug does not cause other problems. If possible, we can revert the application version to one in which the Bug did not appear. We can use a feature flag to temporarily disable a feature. Regardless of the method used to stop the bleeding, the important thing is that we must focus on treating the injured first rather than looking for culprits.</p>\n<p>We have to agree with David Thomas and Andrew Hunt, in their work \"The Pragmatic Programmer: your journey to mastery\" that facing Bugs is a sensitive subject for most developers. Instead of treating the issue as a problem to be solved, some approach it with denial, pointing fingers, lame excuses, or simply apathy. It's exactly the opposite of what is expected!</p>\n<p>Instead of looking for the culprit in a traffic accident, we must first assist the victims and stop the bleeding. In software, instead of finding out who inserted the Bug, we must solve it.</p>\n<h3>Ask before trying to find out</h3>\n<p>When treating an injured person in an emergency, one approach is to use the Glasgow Scale to determine the patient's consciousness. On this scale, one of the components is the verbal response.</p>\n<p>The injured person is asked simple questions, and by analyzing the responses, we can assess the level of consciousness and consequently the level of urgency of their care.</p>\n<p>You and I must carefully read every error message that is displayed during the Bug, it may contain vital information so that we can resolve the problem.</p>\n<p>You may be thinking, but what if there is no error message? If we are just faced with an incorrect result? Well, this also happens in traffic accidents, it is not uncommon for there to be an unconscious victim who cannot answer questions.</p>\n<p>When we discussed the sense of security, we talked about the importance of tests in our application. Go and build a test that can reproduce that Bug, so you can extract the information you need to solve it.</p>\n<p>Think of these tests like the motor tests that highway patrol officers perform on unconscious victims to check their level of consciousness: do the eyes open in response to pain? Is there a pupillary response to light stimulation? Mentally translate this into software language: Does the Bug repeat itself when I enter this and that parameter? And if I insert this new argument, does it happen too?</p>\n<p>To decide on a Bug, data is needed. If you don't receive this data, you must extract it.</p>\n<h2>Trust your tools</h2>\n<h3>When less is more</h3>\n<p>Police work is very complex and your day-to-day job involves the fight for life and protection of the most important assets for society. There would be no point in having a modern and efficient selection and training process for the development of police officers if they were not given the appropriate tools for their work.</p>\n<p>And when I say appropriate, I don't mean they are minimally capable of performing the task, as the result delivered is directly related to the quality and safety of the tools. For an excellent result, not only the police officers but their tools must also be excellent.</p>\n<p>In the police agency I worked for, one of the work tools used was an Austrian pistol known worldwide for its safety and efficiency. The Glock 17 pistol was developed to meet the strict criteria of the Austrian army who wanted to replace the model used during the Second World War.</p>\n<p>In addition to requirements on durability, efficiency, and reliability, safety against accidental firing was one of the pivots of the new model.</p>\n<p>But how did a knife factory manage to deliver the pistol model that would prove to be the safest? Well, it can be said that, in addition to intense testing, the secret to reliability was simplicity and maintainability. The person responsible for the project designed the equipment with as few parts as possible, minimizing complexity. Today the Glock pistol is made with an average of 35 parts, which is a much smaller quantity than other pistols on the market.</p>\n<p>With a simpler design, so-called first-level maintenance is much easier. Equipment users can easily perform maintenance procedures, making the equipment's efficiency last for decades.</p>\n<p>Of course, you must already remember the principle with the acronym KISS, which stands for Keep it Simple (omitting the last word because I don't want to offend the reader). The secret of that renowned pistol model was not in the used materials, but in being simple. The secret of maintaining your software in good shape is to keep it simple, having a specific component for each task. When a problem arises, you will know exactly which component to look for, as each component has a responsibility, this is the principle of Single Responsibility.</p>\n<p>Because of all this, when you are at a shooting range practicing your aim, the police officer knows that if he is not hitting the target, most likely, the error is in his procedure and not in the equipment.</p>\n<p>Allan Kelly, in one of his contributions to the Book \"97 Things Every Programmer Should Know\", corroborates this idea that we should trust our tools when dealing with Bugs. Considering our tools are widely used, mature, and in use by multiple companies, we have little reason to doubt their quality.</p>\n<p>Of course, a prototype, version 0.1, is much more fragile than a library that has been on the market for several years. We are also not saying that it is impossible to have bugs in compilers.</p>\n<p>But considering how rare compiler bugs are, what we are saying is that in the search for a Bug solution, we should trust our tools and look at our piece of code first.</p>\n<h3>Efficient distribution of efforts</h3>\n<p>Imagine it as an efficient distribution of efforts. The library or compiler code has already been reviewed and tested for many years by countless people. As for that little piece of code of yours, only God knows who besides you has looked at it on your team.</p>\n<p>On one of those days, I managed to solve an unpleasant bug that occurred in one of our services. It was a non-deterministic error in our test suite, that is, the Bug only pushed out its head when no one was looking.</p>\n<p>This type of non-deterministic error generally involves three types of origin: multithread, time sensitivity, and dissynchrony between construction and cleaning of test data.</p>\n<p>Analyzing the body of the failed test, after several hours, of course, I began to strongly distrust the way we created timestamps with a native Java library. After all, the test seemed perfect to me, if there was an error it must have been somewhere else.</p>\n<h3>Confidence that avoids shame</h3>\n<p>But, aware of this lesson that we should trust our tools, especially since the component in question had been in Java for 9 years, I ignored this distrust. This saved me a lot of time and embarrassment, as I'm going to share that the error was elsewhere.</p>\n<p>Our test suite ran all tests in a single thread, so it wouldn't be a concurrency issue. But it also ran each test in a deterministic but unpredictable order. Therefore, the source of the intermittent problem must be in some specific order in which the tests were performed.</p>\n<p>After several hours of analyzing the logs, I was able to identify two tests that failed when run in sequence. It turned out that, although our tests performed a test data cleaning routine after each execution, a simple typo had silently overwritten the cleaning method in one of the tests, which generated data inconsistency if such tests were executed in a specific order.</p>\n<h2>Communication via Radio</h2>\n<h3>Q-code</h3>\n<p>I remember that one of the first systems I developed when I worked in the police was one to replace the sending of statistics through radio communication. Many years ago, each police station was supposed to send daily work statistics via radio. Data such as number of people and vehicles inspected, car accidents, etc.</p>\n<p>This took a lot of time as the audio quality of radio communications was not always ideal. The interlocutors spent a lot of time transmitting and confirming each piece of information. All this using Q-Code</p>\n<p>The Q-Code is a standardized collection of three-letter codes used in radio communication. The use of Q-code even in voice transmissions makes communication safer and more efficient. Each set of three letters, always starting with the letter Q, has a specific meaning that is known to the interlocutors.</p>\n<p>I could see that two of those codes were widely used among police officers in radio communication: QAP and QSL.</p>\n<p>QAP was used when you wanted to transmit a message. Let's say someone wanted to talk to me, so he said on the radio: \"Bruno, QAP?\". This could be translated as \"Bruno, are you ready to hear a message?\"</p>\n<p>My answer could be QAP or for example QRM. If I responded with QAP, I was signaling that yes, I was ready to receive the message. If you responded with QRM, I would be informing you that at that moment the communication is being interfered with.</p>\n<p>During communication, the second most used Q-Code appears, QSL. At the end of each message, the sender ends it with the QSL. This means that he is asking the receiver to confirm whether they received the message. It would be something like: \"There is a suspicious vehicle heading towards your team, QSL?\" If I had understood the entire message, I should respond with a simple \"QSL\". In the absence of a response, the sender repeats the message until receiving confirmation from the receiver.</p>\n<p>Each message is concluded with a QSL, and its reception is confirmed with a QSL. Does this remind you of any transmission control protocol out here, used on the internet?</p>\n<p>The main thing here is that a large amount of knowledge is condensed into three letters, in a language that provides interlocutors with efficiency and security. Eric Evans, the author of \"Domain-Driven Design: Tackling Complexity in the Heart of Software\", would agree with me that there is a principle here that should also be adopted in software development.</p>\n<h3>The Language and the Domain</h3>\n<p>A language specific to the domain, which considers possible interference from the environment, a ubiquitous language that is known by all interlocutors, can be the difference between life and death, between success and damnation of a project.</p>\n<p>When a project does not have its own language for that domain, this creates the need for numerous translations between interlocutors. Developers have to translate for domain experts. These, in turn, have to translate their demands to developers. Even among developers, there may be a need for translations.</p>\n<p>All these translations make concepts confusing and the consequences of a lack of mutual understanding can be disastrous.</p>\n<p>The importance of this in trying to arrest a Bug goes much further than compilation errors or exceptions. It goes through the swampy characteristic that the software may be logically working almost perfectly, but it is not delivering what was expected by the stakeholder.</p>\n<p>Let's look at the following pieces of code:</p>\n<div class=\"gatsby-highlight\" data-language=\"java\"><pre class=\"language-java\"><code class=\"language-java\"><span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span>order<span class=\"token punctuation\">.</span>status <span class=\"token operator\">!=</span> completed<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n     <span class=\"token comment\">// (...)</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<div class=\"gatsby-highlight\" data-language=\"java\"><pre class=\"language-java\"><code class=\"language-java\"><span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span>order<span class=\"token punctuation\">.</span><span class=\"token function\">isFullyRefunded</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n     <span class=\"token comment\">// (...)</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>Here we have a small example of two ways of expressing the same situation. In the second piece of code, a common language between developers and domain experts was used. Here, possible logical errors can be easily noticed, without the need to keep in mind what the possible states of the Order are and what the expected state is for that case.</p>\n<p>See the following code:</p>\n<div class=\"gatsby-highlight\" data-language=\"java\"><pre class=\"language-java\"><code class=\"language-java\"><span class=\"token class-name\">Payment</span> newPayment <span class=\"token operator\">=</span> payment<span class=\"token punctuation\">.</span><span class=\"token function\">add</span><span class=\"token punctuation\">(</span><span class=\"token number\">10</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div>\n<p>What do you expect from this code? Does it add ten dollars to the payment? Ten transactions, inbound or outbound? Will the method create a new payment object or will it just change the value of the payment variable and create a new reference called newPayment?</p>\n<p>A lesson also taken from the book \"Clean Code: A HandBook of Agile Software Craftsmanship\" shows that clear communication must also take place between software developers. Functions must communicate exactly what they are doing.</p>\n<p>If you have to look at the function's implementation to know what it's actually doing, that's a big sign that you need to rename it.</p>\n<h2>Bug is in jail</h2>\n<p>I'm very grateful that you've made it this far, you've certainly learned a lot about police activity and how some of its principles can be applied to combat and remove Bugs from your software.</p>\n<p>By applying this knowledge you will be able to work more confidently in your code, giving you peace in your daily life and also allowing you to experiment and discover new things.</p>\n<p>Your aim will be improved and you will get straight to the point, avoiding damage instead of first looking for the culprits. You become bolder and go further because you know you can trust your tools. This leaves you more time to refine your code and seek excellence.</p>\n<p>Finally, not only will your code be safe and efficient, but its result will also be in line with what your customers expect because through effective communication you can accurately deliver what was demanded from you.</p>\n<p>The result of all this is that Bug is in jail, lives are no longer in danger, and you can go home at the end of your shift to enjoy the rest of the day with your family which is looking forward to welcoming their hero back.</p>","fields":{"slug":"/bug-police-officer/","tags":["debugging","testing","TDD","DDD"]}}}]},"authorArticles":null},"pageContext":{"slug":"/IntelliJ IDEA plugin development - focus on UI based plugins/","tags":["auto1","engineering","android","mobile","intellij","idea"],"category":"Engineering","author":"Vadim Shchenev","date":"2021-07-10"}}