<?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>Forem: Vitaliy Emeliyantsev</title>
    <description>The latest articles on Forem by Vitaliy Emeliyantsev (@gambala).</description>
    <link>https://forem.com/gambala</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%2F231846%2F944a7511-8240-4f8f-a2d7-131f6a2ff5eb.jpg</url>
      <title>Forem: Vitaliy Emeliyantsev</title>
      <link>https://forem.com/gambala</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/gambala"/>
    <language>en</language>
    <item>
      <title>Semantic naming in Stimulus.js</title>
      <dc:creator>Vitaliy Emeliyantsev</dc:creator>
      <pubDate>Sat, 02 May 2020 13:28:50 +0000</pubDate>
      <link>https://forem.com/gambala/semantic-naming-in-stimulus-js-23ac</link>
      <guid>https://forem.com/gambala/semantic-naming-in-stimulus-js-23ac</guid>
      <description>&lt;p&gt;TL;DR: You can use &lt;code&gt;data-controller="PascalCase"&lt;/code&gt; and &lt;code&gt;controllers/PascalCase_controller.js&lt;/code&gt; naming in Stimulus. It's not a hack, Stimulus supports it out of the box.&lt;/p&gt;




&lt;p&gt;Stimulus Reflex is the topic #1 this week. &lt;a href="https://rubyweekly.com/issues/499"&gt;Link #1 in Ruby Weekly&lt;/a&gt;, multiple mentions in ruby chats, &lt;a href="https://dev.to/codefund/build-a-real-time-twitter-clone-10-mins-with-rails-and-stimulusreflex-5h5c"&gt;publication on DEV&lt;/a&gt;. I'm so happy for the guys, who created it. And there is one thing I want to talk about. Stimulus Reflex not only gave us effective way for creating reactive apps, but the way to properly name Stimulus controllers.&lt;/p&gt;

&lt;p&gt;I love Stimulus concept, and have been using it since its very first public release. But one thing I truly hate and despise the way Stimulus developers and docs writers are naming their controllers.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In ruby world we have snake_case filenames and PascalCase classnames. It's a general convention and very clear to the community.&lt;/li&gt;
&lt;li&gt;In ES6 world (React.js for example) we have both PascalCase filenames and classnames. It is even better and more semantically clear.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now, what do we see in &lt;a href="https://stimulusjs.org/handbook/hello-stimulus"&gt;Stimulus docs&lt;/a&gt;? &lt;code&gt;data-controller="hello"&lt;/code&gt; and &lt;code&gt;hello_controller.js&lt;/code&gt; – "Ok, I see, lowercase controller name plus _controller suffix for filename – Easy Peasy."&lt;/p&gt;

&lt;p&gt;But what about multiple words controllers? Should I write &lt;code&gt;my_custom_input&lt;/code&gt; like in ruby, or &lt;code&gt;MyCustomInput&lt;/code&gt; like in ES6? &lt;a href="https://stimulusjs.org/handbook/working-with-external-resources#asynchronously-loading-html"&gt;Let's read the docs and see&lt;/a&gt;: &lt;code&gt;data-controller="content-loader"&lt;/code&gt; and &lt;code&gt;content_loader_controller.js&lt;/code&gt;. Wait, WHAT??? How am I supposed to remember it? kebab-case for naming controllers in views and snake_case for filenames? Why??? 🤔&lt;/p&gt;

&lt;p&gt;And every time I needed to add a new Stimulus controller (sometimes in a new project), I had to look for my previously added controllers and even google proper naming in internet. And that made me cry every time. 😢&lt;/p&gt;

&lt;p&gt;But this week I met Stimulus Reflex. And guess what, Reflex author writes all reflex names with PascalCase. Whaat? 😲 But there is a Stimulus under the hood, right? And you tell me, I can use PascalCase in Stimulus?&lt;/p&gt;

&lt;p&gt;Let's visit Stimulus github repo, search for code that scanning controllers folder. &lt;a href="https://github.com/stimulusjs/stimulus/blob/master/packages/%40stimulus/webpack-helpers/src/index.ts#L29"&gt;Here it is&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export function identifierForContextKey(key: string): string | undefined {
  const logicalName = (key.match(/^(?:\.\/)?(.+)(?:[_-]controller\..+?)$/) || [])[1]
  if (logicalName) {
    return logicalName.replace(/_/g, "-").replace(/\//g, "--")
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;So what does Stimulus do. It scans folder for filenames including ANY cased string plus &lt;code&gt;-controller&lt;/code&gt; or &lt;code&gt;_controller&lt;/code&gt; suffix. After that Stimulus replaces all dashes with hyphens. That's the reason why you must use kebab-case in controller names inside views.&lt;/p&gt;

&lt;p&gt;But the most important insight is that you can write &lt;code&gt;PascalCase_controller.js&lt;/code&gt; filename, and use &lt;code&gt;PascalCase&lt;/code&gt; controller name inside views. Or &lt;code&gt;camelCase_controller.js&lt;/code&gt; and &lt;code&gt;camelCase&lt;/code&gt; in views. Or even &lt;code&gt;kebab-case-controller.js&lt;/code&gt; and &lt;code&gt;kebab-case&lt;/code&gt; in views. It's up to you. I prefer PascalCase.&lt;/p&gt;

&lt;p&gt;After so many years I finally have found a way to semantically name Stimulus controllers. It works out of the box, and it worked like this all the time. But because of official docs, every new developer is faced a naming problem. Now it's finally over. And thanks to Stimulus Reflex for that.&lt;/p&gt;

</description>
      <category>stimulus</category>
      <category>rails</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
