<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Matt McKenna</title>
    <description>The latest articles on DEV Community by Matt McKenna (@mattm).</description>
    <link>https://dev.to/mattm</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F421815%2Fb7f0a4d2-3874-41be-a72b-cc3ff39378c4.jpeg</url>
      <title>DEV Community: Matt McKenna</title>
      <link>https://dev.to/mattm</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mattm"/>
    <language>en</language>
    <item>
      <title>Learning Live Templates for Jetpack Compose</title>
      <dc:creator>Matt McKenna</dc:creator>
      <pubDate>Wed, 21 Apr 2021 22:11:40 +0000</pubDate>
      <link>https://dev.to/mattm/learning-live-templates-for-jetpack-compose-5g44</link>
      <guid>https://dev.to/mattm/learning-live-templates-for-jetpack-compose-5g44</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;What are Live Templates?&lt;br&gt;
How can they speed up your development?&lt;br&gt;
And how to create your own for Jetpack Compose!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  Intro to Live Templates
&lt;/h1&gt;

&lt;p&gt;If you are like me you like to optimize some of the most common situations in your day to day tasks. Finding little ways to make your machine work more for you is satisfying and when it all adds up can lead to some serious time saved! &lt;/p&gt;

&lt;p&gt;Android Studio's live templates are a great and fun way to increase your productivity.&lt;/p&gt;

&lt;p&gt;One way to think about live templates are like acronyms. Much like in english "IDK" expands to "I don't know" a live template can expand from &lt;code&gt;main&lt;/code&gt; to:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Array&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="nc"&gt;END&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h6&gt;
  
  
  In the follow parts of this post you'll learn:
&lt;/h6&gt;

&lt;ul&gt;
&lt;li&gt;To use Live Templates in Android Studio&lt;/li&gt;
&lt;li&gt;How to create your own Live Templates&lt;/li&gt;
&lt;li&gt;About the many variables available for Live Templates&lt;/li&gt;
&lt;li&gt;How to use Live Templates with Jetpack Compose&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's dive in!&lt;/p&gt;

&lt;h2&gt;
  
  
  How to use Live Templates
&lt;/h2&gt;

&lt;p&gt;Android Studio comes with a bunch of live templates out of the box. These can be accessed by going to: &lt;code&gt;Preferences &amp;gt; Editor &amp;gt; Live Templates&lt;/code&gt;. Lets see what kind of options we have in this settings screen.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6yfkuig3gzz7073aqj9c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6yfkuig3gzz7073aqj9c.png" alt="live_templates_01_annotated"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The list of available categories and live template .&lt;/li&gt;
&lt;li&gt;Abbreviation: what is typed to trigger the live template.&lt;/li&gt;
&lt;li&gt;Template Text: Where the live template is constructed.&lt;/li&gt;
&lt;li&gt;Description: A description for what this live template does.&lt;/li&gt;
&lt;li&gt;Additional options for this live template.&lt;/li&gt;
&lt;li&gt;The "Edit variables" button for this live template.&lt;/li&gt;
&lt;li&gt;A context selector to choose where a live template can be invoked.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;To use one of these live templates lets make a new kotlin file, type &lt;code&gt;main&lt;/code&gt;, and then wait a second. A tool-tip window will show up enabling the insertion of a live template! Press &lt;code&gt;enter&lt;/code&gt; or &lt;code&gt;tab&lt;/code&gt; to insert.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwxcmhfl8h8pnqqb1ys64.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwxcmhfl8h8pnqqb1ys64.png" alt="live_templates_02"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And that's it! You have now used a Live Template. &lt;/p&gt;

&lt;p&gt;Explore some of the example live templates for Kotlin and Android in the settings window to see what else is predefined. In the next section we will learn how to customize one of the preexisting templates to be a bit more useful.&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating a Custom Live Template
&lt;/h3&gt;

&lt;p&gt;Let's start by creating a simple live template for a custom &lt;code&gt;TODO&lt;/code&gt; comment. I usually put my initials in my &lt;code&gt;TODO&lt;/code&gt;'s so that I can easily search them. They look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// TODO:mcm
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To start making this live template follow these steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Go to &lt;code&gt;Preferences &amp;gt; Editor &amp;gt; Live Templates&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Select the Kotlin group. This is the group the live template will be added to. &lt;/li&gt;
&lt;li&gt;Click the &lt;code&gt;+&lt;/code&gt; button in the upper right corner.&lt;/li&gt;
&lt;li&gt;Select Live Template.&lt;/li&gt;
&lt;li&gt;Type the abbreviation to use to trigger the live template.&lt;/li&gt;
&lt;li&gt;Give the live template a description.&lt;/li&gt;
&lt;li&gt;In the code block, add the code for the live template. In our case it will be the above &lt;code&gt;TODO&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Choose the contexts where this template should be usable.

&lt;ol&gt;
&lt;li&gt;For this template we want to use it in Kotlin.&lt;/li&gt;
&lt;li&gt;And it should be usable in all contexts &lt;strong&gt;except&lt;/strong&gt; comments (since we already add the comment slashes).&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;

&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fstoicallytyped.com%2F6c86002ad679e6b345ec9761333fa2bf%2Flive_templates_10.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fstoicallytyped.com%2F6c86002ad679e6b345ec9761333fa2bf%2Flive_templates_10.gif" alt="Here's the gif link , it's too big for DEV.to"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And that's it! Here it is in action!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh1cj7suhl9dbc7ugfvia.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh1cj7suhl9dbc7ugfvia.gif" alt="live_templates_11"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Live Template Variables
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Defining Variables
&lt;/h3&gt;

&lt;p&gt;To define a variable in a live template simply surround a string with dollar signs. Let's look at the provided Kotlin &lt;code&gt;interface&lt;/code&gt; live template:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="nc"&gt;NAME&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="nc"&gt;END&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h6&gt;
  
  
  There are two variables here:
&lt;/h6&gt;

&lt;ol&gt;
&lt;li&gt;&lt;code&gt;$NAME$&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;$END$&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;After invoking the live template with &lt;code&gt;tab&lt;/code&gt; the editor places your cursor to define a name for the interface. After entering a value for &lt;code&gt;$NAME$&lt;/code&gt; pressing &lt;code&gt;tab&lt;/code&gt; again will take you to the next variable, until you get to the variable &lt;code&gt;$END$&lt;/code&gt;. &lt;code&gt;$END$&lt;/code&gt;, and a few other variables, are predefined for specific use.&lt;/p&gt;

&lt;h2&gt;
  
  
  Predefined Variables
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;$DOLLAR$&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;$END$&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;$SELECTION$&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;code&gt;$DOLLAR$&lt;/code&gt; variable is used for inserting a dollar sign (&lt;code&gt;$&lt;/code&gt;) into a live template. Think of it like an escape sequence in a &lt;code&gt;String&lt;/code&gt;. It will most likely be used in those sorts of situations. To see an example look at the Kotlin live template, &lt;code&gt;soutv&lt;/code&gt; (Prints a value to System.out).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"$EXPR_COPY$ = $DOLLAR${$EXPR$}"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;$END$&lt;/code&gt; is a special live template variable that just signifies the template is done being populated. When pressing &lt;code&gt;tab&lt;/code&gt; leads to this variable your cursor will be placed in the &lt;code&gt;$END$&lt;/code&gt;'s position and the live template will no longer be active.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$SELECTION$&lt;/code&gt; is a special variable type as it represents text that is highlighted in the editor. For instance to surround a piece of code with parenthesis we could define a live template like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="nc"&gt;SELECTION&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This live template is already defined as &lt;code&gt;P&lt;/code&gt; under the &lt;code&gt;Live Templates &amp;gt; surround&lt;/code&gt; section, along with a few others.&lt;/p&gt;

&lt;p&gt;To trigger a selection live template, select the text you wish to use and then, if you are on mac, press &lt;code&gt;⌥+⌘+J&lt;/code&gt; (&lt;code&gt;Ctrl+Alt+J&lt;/code&gt; for windows/linux) to prompt the dialog. Then the applicable live templates will appear! Now you can type the name or select which one to use.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn74w5sc9gyob18lteqtz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn74w5sc9gyob18lteqtz.png" alt="live_templates_03"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Variable Functions
&lt;/h2&gt;

&lt;p&gt;Alright, this is where we get to the true magic of live templates.&lt;/p&gt;

&lt;p&gt;In the first section we called out the edit variable button in the live template window. This is where variables gain extra power.&lt;/p&gt;

&lt;p&gt;Lets jump to &lt;code&gt;Live Templates &amp;gt; Kotlin &amp;gt; iter&lt;/code&gt; and see how it is defined:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="nc"&gt;VAR&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="nc"&gt;ITERABLE&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="nc"&gt;END&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To break this down, we have the structure for a Kotlin for loop. Two variables are defined, &lt;code&gt;$VAR$&lt;/code&gt; for the variable name  and &lt;code&gt;$ITERABLE$&lt;/code&gt; for the iterable collection. And finally &lt;code&gt;$END$&lt;/code&gt; to place the cursor in the for loop when the live template finishes executing.&lt;/p&gt;

&lt;p&gt;Up until now this is exactly how we would define such a structure, but lets super charge it.&lt;/p&gt;

&lt;p&gt;Click on the "Edit Variables" button that we saw above in location 6 to bring up this window table containing each of the defined variables.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcd38286y791gnna7cg2l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcd38286y791gnna7cg2l.png" alt="live_templates_04"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The Expression column has a drop down menu that provides access to many functions that can make the live template variables smarter. Let's take a look at the two variables in this tempate.&lt;/p&gt;

&lt;p&gt;For the variable &lt;code&gt;ITERABLE&lt;/code&gt; we see that there is an expression of &lt;code&gt;kotlinVariable()&lt;/code&gt;. This function will make a suggestion for something that can populate this variable. If nothing is found then a Default value can be provided as a suggestion.&lt;/p&gt;

&lt;p&gt;For &lt;code&gt;VAR&lt;/code&gt; we see it has &lt;code&gt;kotlinSuggestVariableName()&lt;/code&gt;. This function is tells the live template to put something like &lt;code&gt;i&lt;/code&gt;, &lt;code&gt;s&lt;/code&gt;, etc if a type can be inferred or loops are being nested. Here the Default value to suggest is &lt;code&gt;i&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The last column in this table is "Skip if defined". Checking this box means that if the function executes and returns a value then this variable is considered defined and the live template will automatically move your the cursor to the next variable.&lt;/p&gt;

&lt;p&gt;Lets see these functions in action! I will make a function to print out elements of a &lt;code&gt;List&amp;lt;String&amp;gt;&lt;/code&gt;. Since Android Studio knows the list is only of type string it will suggest a variable name of &lt;code&gt;s&lt;/code&gt; for &lt;code&gt;VAR&lt;/code&gt;. And since we have a variable of type Iterable in our scope the variable &lt;code&gt;ITERABLE&lt;/code&gt; will populate with the available &lt;code&gt;val list&lt;/code&gt;! Finally, since the check boxes for "Skip if defined" are not enabled, the cursor still goes to each of these variable in case they need to be edited.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1619040102660%2FkPbv5Eemn.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1619040102660%2FkPbv5Eemn.gif" alt="live_templates_05_iter.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There are LOTS of these functions available for live template variables. The function names are pretty descriptive and I recommend looking through the drop down menus to see the options. There is a comprehensive list on the &lt;a href="https://www.jetbrains.com/help/idea/template-variables.html#predefined_functions" rel="noopener noreferrer"&gt;Jetbrains website&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Live Template Pro tips
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Options
&lt;/h3&gt;

&lt;p&gt;There are a few options in the bottom right of the live template window which can add additional convenience.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Expand with &lt;code&gt;tab&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;Allows for choosing which key will expand a live template
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Reformat according to code style

&lt;ul&gt;
&lt;li&gt;a checkbox to use the project's code style when inserting a live template&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Shorten FQ names

&lt;ul&gt;
&lt;li&gt;FQ stands for Fully Qualified&lt;/li&gt;
&lt;li&gt;This is a way to automatically import objects or annotations that a live template uses&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  Automatic Imports with Live Templates
&lt;/h3&gt;

&lt;p&gt;To automatically import with a live template the "Shorten FQ names" checkbox needs to be checked. Then include the fully qualified name in the live template definition.&lt;/p&gt;

&lt;p&gt;Here is an example for making a quick test function and not needing to import the &lt;code&gt;@Test&lt;/code&gt; annotation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="n"&gt;org&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;junit&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Test&lt;/span&gt;
&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="nf"&gt;TEST_FUNCTION&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="nc"&gt;END&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Applicable Contexts
&lt;/h3&gt;

&lt;p&gt;Not all live templates should be accesible at all times. For instance, Kotlin live templates shouldn't be available in a Java context, but we can have even more control than that! Let's look at the live template to create a function with no return type (&lt;code&gt;void&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6err4lxivg8sxw0lmpxg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6err4lxivg8sxw0lmpxg.png" alt="live_templates_08"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This live template is useful at the levels of:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;top-level&lt;/li&gt;
&lt;li&gt;statement&lt;/li&gt;
&lt;li&gt;class&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;but not at the other levels of:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;comment&lt;/li&gt;
&lt;li&gt;expression&lt;/li&gt;
&lt;li&gt;object declaration&lt;/li&gt;
&lt;li&gt;other&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This means is we can granularly configure when a live template could be applicable. Typing &lt;code&gt;void&lt;/code&gt; while in a class will for the live template, but typing &lt;code&gt;void&lt;/code&gt; in a comment will not!&lt;/p&gt;

&lt;h6&gt;
  
  
  In a class:
&lt;/h6&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1xys6qocmehefe7q6j7d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1xys6qocmehefe7q6j7d.png" alt="live_templates_06"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h6&gt;
  
  
  In a comment:
&lt;/h6&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk3ngc8tlr88qwxqtkv5v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk3ngc8tlr88qwxqtkv5v.png" alt="live_templates_07"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Live Templates for Jetpack Compose
&lt;/h2&gt;

&lt;p&gt;Many of the code structures, syntax, and best practices in Jetpack Compose UI are very repeatable which means this is a great opportunity to create live templates! Android Studio Arctic Fox comes with some Compose live templates, but lets add some common uses that aren't included.&lt;/p&gt;

&lt;h4&gt;
  
  
  Private Composable
&lt;/h4&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
  &lt;tr&gt;
    &lt;th&gt;&lt;b&gt;Abbreviation&lt;/b&gt;&lt;/th&gt;
    &lt;td&gt;pcomp&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;th&gt;&lt;b&gt;Description&lt;/b&gt;&lt;/th&gt;
    &lt;td&gt;Create a private composable function&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;th&gt;&lt;b&gt;Shorten FQ Names&lt;/b&gt;&lt;/th&gt;
    &lt;td&gt;✅&lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="n"&gt;androidx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;compose&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;runtime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Composable&lt;/span&gt;
&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="nf"&gt;NAME&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="nc"&gt;END&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Remember mutableStateOf delegate
&lt;/h4&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
  &lt;tr&gt;
    &lt;th&gt;&lt;b&gt;Abbreviation&lt;/b&gt;&lt;/th&gt;
    &lt;td&gt;remstate&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;th&gt;&lt;b&gt;Description&lt;/b&gt;&lt;/th&gt;
    &lt;td&gt;Create a remember mutableStateOf Delegate&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;th&gt;&lt;b&gt;Shorten FQ Names&lt;/b&gt;&lt;/th&gt;
    &lt;td&gt;✅&lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="py"&gt;VAR&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt; &lt;span class="k"&gt;by&lt;/span&gt; &lt;span class="n"&gt;androidx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;compose&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;runtime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;remember&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
  &lt;span class="n"&gt;androidx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;compose&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;runtime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;mutableStateOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="nc"&gt;END&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Make a Spacer
&lt;/h4&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
  &lt;tr&gt;
    &lt;th&gt;&lt;b&gt;Abbreviation&lt;/b&gt;&lt;/th&gt;
    &lt;td&gt;spacer&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;th&gt;&lt;b&gt;Description&lt;/b&gt;&lt;/th&gt;
    &lt;td&gt;Makes a spacer of set width&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;th&gt;&lt;b&gt;Shorten FQ Names&lt;/b&gt;&lt;/th&gt;
    &lt;td&gt;✅&lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="n"&gt;androidx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;compose&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;foundation&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;layout&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Spacer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Modifier&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;width&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="nc"&gt;NUM&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dp&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="nc"&gt;END&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  LazyColumn
&lt;/h4&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
  &lt;tr&gt;
    &lt;th&gt;&lt;b&gt;Abbreviation&lt;/b&gt;&lt;/th&gt;
    &lt;td&gt;lazycol&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;th&gt;&lt;b&gt;Description&lt;/b&gt;&lt;/th&gt;
    &lt;td&gt;Makes a LazyColumn Composable&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;th&gt;&lt;b&gt;Shorten FQ Names&lt;/b&gt;&lt;/th&gt;
    &lt;td&gt;✅&lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h6&gt;
  
  
  Edit Variables Values
&lt;/h6&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
  &lt;tr&gt;
    &lt;th&gt;&lt;b&gt;Name&lt;/b&gt;&lt;/th&gt;
    &lt;td&gt;MOD&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;th&gt;&lt;b&gt;Expression&lt;/b&gt;&lt;/th&gt;
    &lt;td&gt;variableOfType(androidx.compose.ui.Modifier)&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;th&gt;&lt;b&gt;Default&lt;/b&gt;&lt;/th&gt;
    &lt;td&gt;"androidx.compose.ui.Modifier"&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;th&gt;&lt;b&gt;Skip if defined&lt;/b&gt;&lt;/th&gt;
    &lt;td&gt;Unchecked&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;th&gt; &lt;/th&gt;
    &lt;td&gt; &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;th&gt;&lt;b&gt;Name&lt;/b&gt;&lt;/th&gt;
    &lt;td&gt;LIST&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;th&gt;&lt;b&gt;Expression&lt;/b&gt;&lt;/th&gt;
    &lt;td&gt;kotlinVariable()&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;th&gt;&lt;b&gt;Default&lt;/b&gt;&lt;/th&gt;
    &lt;td&gt; &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;th&gt;&lt;b&gt;Skip if defined&lt;/b&gt;&lt;/th&gt;
    &lt;td&gt;Unchecked&lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="n"&gt;androidx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;compose&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;foundation&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;lazy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;LazyColumn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;modifier&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="nc"&gt;MODIFIER&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;items&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;items&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="nc"&gt;LIST&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt;
    &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="nc"&gt;END&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;          
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Day / Night Preview Composable
&lt;/h4&gt;

&lt;p&gt;&lt;em&gt;This live template idea is from &lt;a href="https://twitter.com/AdamMc331" rel="noopener noreferrer"&gt;Adam McNeilly&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
  &lt;tr&gt;
    &lt;th&gt;&lt;b&gt;Abbreviation&lt;/b&gt;&lt;/th&gt;
    &lt;td&gt;dnprev&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;th&gt;&lt;b&gt;Description&lt;/b&gt;&lt;/th&gt;
    &lt;td&gt;Create a day/night &lt;code&gt;@Preview&lt;/code&gt; composable function&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;th&gt;&lt;b&gt;Shorten FQ Names&lt;/b&gt;&lt;/th&gt;
    &lt;td&gt;✅&lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="n"&gt;androidx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;compose&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ui&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tooling&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;preview&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Preview&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Night Mode"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;uiMode&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;android&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Configuration&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;UI_MODE_NIGHT_YES&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="n"&gt;androidx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;compose&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ui&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tooling&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;preview&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Preview&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Day Mode"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;uiMode&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;android&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Configuration&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;UI_MODE_NIGHT_NO&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="n"&gt;androidx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;compose&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;runtime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Composable&lt;/span&gt;
&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="nf"&gt;NAME&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="nc"&gt;Preview&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="nc"&gt;NAME&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="nc"&gt;END&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  Phone Preview Annotation
&lt;/h5&gt;

&lt;p&gt;&lt;em&gt;This series of &lt;code&gt;@Preview&lt;/code&gt; annotation live templates is inspired by &lt;a href="https://twitter.com/heytherewills/status/1387746132147806214/photo/1" rel="noopener noreferrer"&gt;William Barbosa&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
  &lt;tr&gt;
    &lt;th&gt;&lt;b&gt;Abbreviation&lt;/b&gt;&lt;/th&gt;
    &lt;td&gt;prevphone&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;th&gt;&lt;b&gt;Description&lt;/b&gt;&lt;/th&gt;
    &lt;td&gt;Create an &lt;code&gt;@Preview&lt;/code&gt; annotation for a phone in light/dark mode&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;th&gt;&lt;b&gt;Shorten FQ Names&lt;/b&gt;&lt;/th&gt;
    &lt;td&gt;✅&lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="n"&gt;androidx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;compose&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ui&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tooling&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;preview&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Preview&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Phone Night Mode"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;uiMode&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;android&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Configuration&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;UI_MODE_NIGHT_YES&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="n"&gt;androidx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;compose&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ui&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tooling&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;preview&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Preview&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Phone Day Mode"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;uiMode&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;android&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Configuration&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;UI_MODE_NIGHT_NO&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  Tablet Preview Annotation
&lt;/h5&gt;

&lt;p&gt;&lt;em&gt;This series of &lt;code&gt;@Preview&lt;/code&gt; annotation live templates is inspired by &lt;a href="https://twitter.com/heytherewills/status/1387746132147806214/photo/1" rel="noopener noreferrer"&gt;William Barbosa&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
  &lt;tr&gt;
    &lt;th&gt;&lt;b&gt;Abbreviation&lt;/b&gt;&lt;/th&gt;
    &lt;td&gt;prevtablet&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;th&gt;&lt;b&gt;Description&lt;/b&gt;&lt;/th&gt;
    &lt;td&gt;Create an &lt;code&gt;@Preview&lt;/code&gt; annotation for a tablet in light/dark mode&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;th&gt;&lt;b&gt;Shorten FQ Names&lt;/b&gt;&lt;/th&gt;
    &lt;td&gt;✅&lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="n"&gt;androidx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;compose&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ui&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tooling&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;preview&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Preview&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Tablet Night Mode"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;uiMode&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;android&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Configuration&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;UI_MODE_NIGHT_YES&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;heightDp&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1080&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;widthDp&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;760&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="n"&gt;androidx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;compose&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ui&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tooling&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;preview&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Preview&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Tablet Day Mode"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;uiMode&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;android&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Configuration&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;UI_MODE_NIGHT_NO&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;heightDp&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1080&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;widthDp&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;760&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  Desktop Preview Annotation
&lt;/h5&gt;

&lt;p&gt;&lt;em&gt;This series of &lt;code&gt;@Preview&lt;/code&gt; annotation live templates is inspired by &lt;a href="https://twitter.com/heytherewills/status/1387746132147806214/photo/1" rel="noopener noreferrer"&gt;William Barbosa&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
  &lt;tr&gt;
    &lt;th&gt;&lt;b&gt;Abbreviation&lt;/b&gt;&lt;/th&gt;
    &lt;td&gt;prevdesktop&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;th&gt;&lt;b&gt;Description&lt;/b&gt;&lt;/th&gt;
    &lt;td&gt;Create an &lt;code&gt;@Preview&lt;/code&gt; annotation for a desktop in light/dark mode&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;th&gt;&lt;b&gt;Shorten FQ Names&lt;/b&gt;&lt;/th&gt;
    &lt;td&gt;✅&lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="n"&gt;androidx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;compose&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ui&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tooling&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;preview&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Preview&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Desktop Night Mode"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;uiMode&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;android&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Configuration&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;UI_MODE_NIGHT_YES&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;heightDp&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;760&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;widthDp&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1080&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="err"&gt;@&lt;/span&gt;&lt;span class="n"&gt;androidx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;compose&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ui&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tooling&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;preview&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Preview&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Desktop Day Mode"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;uiMode&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;android&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Configuration&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;UI_MODE_NIGHT_NO&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;heightDp&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;760&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;widthDp&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1080&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  What's next?
&lt;/h3&gt;

&lt;p&gt;Go play with and create some live templates! They are really fun to make and use in day to day development. If you need help with them or want to share what you've made reach out on &lt;a href="https://twitter.com/intent/tweet?text=.@himattm" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;! I'd love to see them (and add to my collection 😉)!&lt;/p&gt;




&lt;p&gt;Header Photo by &lt;a href="https://unsplash.com/@tim_arterbury?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noopener noreferrer"&gt;Tim Arterbury&lt;/a&gt; on &lt;a href="https://unsplash.com/s/photos/template?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noopener noreferrer"&gt;Unsplash&lt;/a&gt; and modified by me.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Special thanks to &lt;a href="https://twitter.com/AdamMc331" rel="noopener noreferrer"&gt;Adam McNeilly&lt;/a&gt; for reviewing and proof reading this article.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>android</category>
      <category>kotlin</category>
      <category>compose</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Magically turn your phone into a tablet with adb for testing tablet views!</title>
      <dc:creator>Matt McKenna</dc:creator>
      <pubDate>Fri, 02 Apr 2021 13:37:57 +0000</pubDate>
      <link>https://dev.to/mattm/magically-adb-your-phone-into-a-tablet-for-testing-tablet-views-5c5m</link>
      <guid>https://dev.to/mattm/magically-adb-your-phone-into-a-tablet-for-testing-tablet-views-5c5m</guid>
      <description>&lt;h3&gt;
  
  
  &lt;a href="https://youtu.be/LV-cXixgrho" rel="noopener noreferrer"&gt;The Pledge&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;So you forgot your tablet at the office and now you are working from home for the foreseeable future. It's still important to test those tablet views! Here’s a quick and painless way to turn an ordinary phone into a tablet and back again. This isn't one of those one way disappearing acts!&lt;/p&gt;

&lt;h4&gt;
  
  
  Tools of the Trick
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;a href="https://developer.android.com/studio/command-line/adb" rel="noopener noreferrer"&gt;Android Debug Bridge&lt;/a&gt; - Our good friend the Android Debug Bridge (adb). We will be using some commands here to perform our magic trick.&lt;/li&gt;
&lt;li&gt;Search Engine - One of your choice, I recommend &lt;a href="https://duckduckgo.com/" rel="noopener noreferrer"&gt;DuckDuckGo&lt;/a&gt; &lt;a href="https://duckduckgo.com/privacy" rel="noopener noreferrer"&gt;(go privacy!)&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Phone - Can be any phone you want to use. I promise we will put it back to the way it was by the end of this article&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/Genymobile/scrcpy" rel="noopener noreferrer"&gt;scrcpy&lt;/a&gt; - An application to view and control your Android device from your computer&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  &lt;a href="https://youtu.be/LV-cXixgrho" rel="noopener noreferrer"&gt;The Turn&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;The first step you want to take is to find the resolution and pixel density of the tablet you want to emulate on your device. For example, if we want to emulate a &lt;a href="https://www.gsmarena.com/google_pixel_c-7826.php" rel="noopener noreferrer"&gt;Pixel C device&lt;/a&gt; we can search up its specs on a site like GSMArena. We find that this device has a &lt;strong&gt;2560x1800&lt;/strong&gt; resolution display with a pixel density of &lt;strong&gt;308&lt;/strong&gt; ppi (pixels per inch).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F93qlexiypx43m7po58c9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F93qlexiypx43m7po58c9.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For the next step I'm going to assume that &lt;code&gt;adb&lt;/code&gt; is installed and set up on your system. If it is not, check out this write up from &lt;a href="https://www.xda-developers.com/install-adb-windows-macos-linux/" rel="noopener noreferrer"&gt;xda developers&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The Android Debug Bridge has a slew of great commands. The one we will be using today is the &lt;code&gt;shell&lt;/code&gt; command with the &lt;code&gt;wm&lt;/code&gt; command.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;shell&lt;/code&gt; - allows us to access the linux shell on an Android device&lt;/p&gt;

&lt;p&gt;&lt;code&gt;wm&lt;/code&gt; - accesses the devices window manager&lt;/p&gt;

&lt;p&gt;We also need to know two parameters for the &lt;code&gt;wm&lt;/code&gt; command.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;size&lt;/code&gt; - which will allow us to see our current and original screen resolution, as well as set a desired screen resolution&lt;/p&gt;

&lt;p&gt;&lt;code&gt;density&lt;/code&gt; - which allows us to see our current and original pixel densities as well as set a desired pixel density.&lt;br&gt;
Lets try this out in our terminal.&lt;/p&gt;

&lt;p&gt;Now let's try this command! Make sure you have your device connected and USB Debugging enabled! I am running this on a Google Pixel 4XL. Here's what is shown when running these commands&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; adb shell wm size
Physical size: 1440x3040

&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; adb shell wm density
Physical density: 560
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see we are given the original resolution with &lt;code&gt;size&lt;/code&gt; and the original pixel density with &lt;code&gt;density&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvwm3ie5b4gi62pxv1qlx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvwm3ie5b4gi62pxv1qlx.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Time to become a tablet! Remember this is easily reversible if something goes awry.&lt;br&gt;
Let's grab the resolution of a Pixel C tablet and set our phone resolution to it using the command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; adb shell wm size 2560x1800
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You should see your device make this change immediately.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd6c35s14fpkqcrud4msz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd6c35s14fpkqcrud4msz.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Running the &lt;code&gt;size&lt;/code&gt; command again will show that we now have another line of output for the &lt;strong&gt;Override size&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; adb shell wm size
Physical size: 1440x3040
Override size: 2560x1800
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the screen will look a little funny but this is because the pixel density of the device most likely isn't calibrated correctly for this resolution.  Lets see what it looks like when we change &lt;strong&gt;only&lt;/strong&gt; the density.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; adb shell wm density 308
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpiqokpd021bxui1j7tvz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpiqokpd021bxui1j7tvz.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And again our change can be verified by running the &lt;code&gt;density&lt;/code&gt; command once more.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; adb shell wm density
Physical density: 560
Override density: 308
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now it's time to bring it all together changing both size and density to match what the Pixel C specs have!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; adb shell wm size 2560x1800
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; adb shell wm density 308
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Alakazam!&lt;/em&gt; &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8yedbeohdii27blvyzdb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8yedbeohdii27blvyzdb.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A tablet! This is similar to changing the resolution or scaling on your laptop or monitor! Now when you run your app the tablet specific views will be rendered!&lt;/p&gt;

&lt;p&gt;Hmm, it still looks funny on the device though. It's probably pretty small. It might not even respond to touch inputs. That's okay! This is expected. There are a few ways we can address this. Firstly, a Pixel C is a tablet device with a very high resolution screen and pixel density. Choosing a resolution and pixel density from a tablet that isn't quite as large as the Pixel C's will provide a bit of a better experience. However, there is a &lt;em&gt;cooler&lt;/em&gt; option.&lt;/p&gt;

&lt;h4&gt;
  
  
  Introducing &lt;a href="https://github.com/Genymobile/scrcpy" rel="noopener noreferrer"&gt;&lt;code&gt;scrcpy&lt;/code&gt;&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;If you have never used &lt;code&gt;scrcpy&lt;/code&gt; before you will love it. It's a &lt;a href="https://blog.rom1v.com/2018/03/introducing-scrcpy/" rel="noopener noreferrer"&gt;magic trick all in its own&lt;/a&gt; by some developers at &lt;a href="https://github.com/Genymobile" rel="noopener noreferrer"&gt;Genymobile&lt;/a&gt; to control and view your Android devices from your computer. It can easily be installed for all platforms following the instructions on their &lt;a href="https://github.com/Genymobile/scrcpy/blob/master/README.md" rel="noopener noreferrer"&gt;Github README&lt;/a&gt;.&lt;br&gt;
Now that it's installed all we need to do is open up a terminal and run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; scrcpy
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you only have one device connected you will be able to see it right away! If you have more than one you will need to get your device's serial number and then use that to invoke &lt;code&gt;scrcpy&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; adb devices
List of devices attached
0123456789abcdef    device

&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; scrcpy &lt;span class="nt"&gt;-s&lt;/span&gt; 0123456789abcdef
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F07rr1puraka4173gxqdl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F07rr1puraka4173gxqdl.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The reason we are using &lt;code&gt;scrcpy&lt;/code&gt; here is the window is resizable so you can now enjoy that full size "tablet experience" being rendered on your phone and use your mouse and keyboard for inputs!&lt;br&gt;
And voila! We now have a solution for seamless tablet development. &lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;a href="https://youtu.be/LV-cXixgrho" rel="noopener noreferrer"&gt;The Prestige&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;When done working on tablet views it is very easy to undo our tablet transformation trick! All that is needed is a simple reset with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; adb shell wm size reset
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; adb shell wm density reset
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Boom!&lt;/strong&gt; Back in the phone business.&lt;br&gt;
But wait! We can go even further. We can look up the screen settings of some smaller phones to see how our views look on smaller screens. I'll leave this to you to try on your own!&lt;br&gt;
I hope this has helped you if you need to check things out on a tablet but happen to find yourself without one!&lt;/p&gt;




&lt;p&gt;Header photo by &lt;a href="https://unsplash.com/@frostroomhead?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noopener noreferrer"&gt;Rodion Kutsaev&lt;/a&gt; on &lt;a href="https://unsplash.com/collections/9036135/for-posts/0666e9ede519149a0d119be4362f103b?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noopener noreferrer"&gt;Unsplash&lt;/a&gt;&lt;/p&gt;

</description>
      <category>android</category>
      <category>beginners</category>
      <category>adb</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Effective Naming of Booleans</title>
      <dc:creator>Matt McKenna</dc:creator>
      <pubDate>Mon, 23 Nov 2020 00:56:41 +0000</pubDate>
      <link>https://dev.to/mattm/effective-naming-of-booleans-4gl1</link>
      <guid>https://dev.to/mattm/effective-naming-of-booleans-4gl1</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;"Do you have bananas?"&lt;/p&gt;

&lt;p&gt;"Yes! We have no bananas."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is the title of &lt;a href="https://en.wikipedia.org/wiki/Yes!_We_Have_No_Bananas"&gt;a song by Frank Silver and Irvine Cohn&lt;/a&gt; that my great grandfather used to play for me on his ukulele, but today we will be using it to better understand how to name boolean values.&lt;/p&gt;

&lt;p&gt;This line is comedic in its confusion. Upon asking a yes or no question the inquisitor is met with both in response. How are they supposed to interpret this answer? After a moment of thought it can be fairly safe to determine the shopkeeper has no bananas. That extra moment of thought leading to this conclusion is what we will be discussing. &lt;/p&gt;

&lt;p&gt;Lets translate this question into some Kotlin code. We can turn "Yes! We have no bananas into the below boolean declaration.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;hasNoBananas&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and "No! We have no bananas" into the false equivalent&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;hasNoBananas&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code is perfectly valid and won't cause any problems in its implementation.&lt;br&gt;
&lt;br&gt;
  &lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;hasNoBananas&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"We have no bananas"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"We have bananas"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Let's take a step away from the bananas for a moment and look at a more practical example. Let's consider a feature flag that will determine whether or not our users have access to a new part of our app and name it with the same intention we have been looking at. We can do this in two ways depending on the words we are using. With the inclusion of 'not' or using a negated version of the word itself.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;isNotEnabled&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;isDisabled&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This looks just like the example with the bananas, but when applied to our context, it has the same effect as the reply from the shopkeeper. The 'not' in the boolean name creates a moment of mental processing needed to understand that &lt;code&gt;true&lt;/code&gt; is actually the negative case and &lt;code&gt;false&lt;/code&gt; is actually the positive case. Much like a double negative in grammar, the reader of this code needs to apply more thought to what will happen when presented with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;setFeatureEnabled&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;isEnabled&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Boolean&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;isEnabled&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Enable Feature&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Disable Feature&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;span class="nf"&gt;setFeatureEnabled&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;isNotEnabled&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// setFeatureEnabled(true)&lt;/span&gt;
&lt;span class="c1"&gt;// or&lt;/span&gt;
&lt;span class="nf"&gt;setFeatureEnabled&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;isDisabled&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// setFeatureEnabled(true)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We have added a negation into the name of our boolean. We are asking the reader of our code to care about the name as well as the current value to understand what we are intending. In the above example we are passing &lt;code&gt;true&lt;/code&gt; to our function with the names &lt;code&gt;isNotEnabled&lt;/code&gt; and &lt;code&gt;isDisabled&lt;/code&gt;. This will lead to confusion as the function is expecting &lt;code&gt;true&lt;/code&gt; to be the enabled case, but reading this code it looks like we want to disable our feature.&lt;/p&gt;

&lt;p&gt;When naming a boolean it is best to omit any modifiers to the intention of the value in the name. We wouldn't write something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;isNotNOTEnabled&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;so we should also not write something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;isNotEnabled&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This also applies to words that can have negated meaning as we could easily name a boolean like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;isDisabled&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But this has the same problem of needing to align the positive &lt;code&gt;true&lt;/code&gt; with the negated naming and vice-versa. And just like above we wouldn't write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;isNotDisabled&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This would be clearer and logically equivalent if it was named &lt;code&gt;isEnabled&lt;/code&gt;. Therefore, we should rely on the positive naming scheme to keep the &lt;code&gt;true&lt;/code&gt; / &lt;code&gt;false&lt;/code&gt; values aligned with the boolean that they represent.&lt;/p&gt;

&lt;h1&gt;
  
  
  Applying this understanding to our case
&lt;/h1&gt;

&lt;p&gt;Making the changes to the above we can see how effective this is, both for the shopkeeper and our app!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;hasBananas&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;

&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;answerFruit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fruit&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;hasInventory&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Boolean&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;hasInventory&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"We have $fruit"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"We have no $fruits"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;answerFruit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"bananas"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;hasBananas&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// We have bananas&lt;/span&gt;

&lt;span class="n"&gt;hasBananas&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;false&lt;/span&gt;

&lt;span class="nf"&gt;answerFruit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"bananas"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;hasBananas&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// We have no bananas&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;isEnabled&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;false&lt;/span&gt;
&lt;span class="nf"&gt;setFeatureEnabled&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;isEnabled&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// setFeatureEnabled(false)&lt;/span&gt;

&lt;span class="n"&gt;isEnabled&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;
&lt;span class="nf"&gt;setFeatureEnabled&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;isEnabled&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// setFeatureEnabled(true)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By removing the negation in the name, we now understand and define the desired intent. Applying this practice prevents needing to make any mental jumps when working with the humble boolean.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>programming</category>
      <category>codequality</category>
      <category>kotlin</category>
    </item>
  </channel>
</rss>
