<?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: Octoxalis</title>
    <description>The latest articles on DEV Community by Octoxalis (@octoxalis).</description>
    <link>https://dev.to/octoxalis</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%2F275203%2F70309b40-07be-4787-90a2-c11bcdff4e97.png</url>
      <title>DEV Community: Octoxalis</title>
      <link>https://dev.to/octoxalis</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/octoxalis"/>
    <language>en</language>
    <item>
      <title>JavaScript naming scheme: TypexJS</title>
      <dc:creator>Octoxalis</dc:creator>
      <pubDate>Wed, 20 Nov 2019 06:42:17 +0000</pubDate>
      <link>https://dev.to/octoxalis/javascript-naming-scheme-typexjs-pjc</link>
      <guid>https://dev.to/octoxalis/javascript-naming-scheme-typexjs-pjc</guid>
      <description>&lt;p&gt;One of the most useful principle of software development which should be permanently kept in mind is not a new one. Actually, it's an almost millenial assertion: &lt;strong&gt;&lt;q&gt;Entities must not be multiplied without necessity&lt;/q&gt;&lt;/strong&gt;, a sentence frequently associated with the &lt;a href="http://www.irishphilosophy.com/2014/05/27/who-sharpened-occams-razor/"&gt;Occam's razor&lt;/a&gt; concept. Despite his canonical age, the validity of the sentence remains intact in the sofware engineering field and it has been adaptated in many ways (the &lt;a href="https://en.wikipedia.org/wiki/KISS_principle"&gt;KISS&lt;/a&gt; principle is one among many &lt;a href="https://effectivesoftwaredesign.com/2013/08/05/simplicity-in-software-design-kiss-yagni-and-occams-razor/"&gt;others&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;Therefore, what are the implications of such a &lt;em&gt;law&lt;/em&gt; for a JavaScript programmer? And why is a computer language typing concept related to it?&lt;/p&gt;

&lt;h2&gt;
  
  
  A dispute about names and types
&lt;/h2&gt;

&lt;p&gt;Every developer knows that JavaScript is not a static typed language, a useful feature eliminating lots of bugs (a language like Typescript has been created as a remedy to that important lack of safety). Even for code modules counting less than a few tens of lines, it's easy to forget what exactly is the type of a variable declared at the begining of the file and then make a mistake when assining a wrong type to that variable.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href="https://typexjs.netlify.com"&gt;TypexJS&lt;/a&gt;&lt;/strong&gt; is a simple convention designed to avoid such mistakes: by only adding a mnemonic letter at the end of each variable identifier to specify its type (of course, this scheme also applies to constants).&lt;/p&gt;

&lt;p&gt;This simple adjonction has two main benefices:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;it gathers tightly related variables in a consistent semantic field;&lt;/li&gt;
&lt;li&gt;it simplifies identifier derivations.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Just an example, taken from &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split"&gt;MDN String documentation&lt;/a&gt;. The JavaScript &lt;code&gt;String.prototype.split&lt;/code&gt; method returns an array of Strings:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var str = 'The quick brown fox jumps over the lazy dog.';
var words = str.split(' ');
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Two different words for two tightly related entities! Isn't it semantically more meaningful to use the same identifier with different specifiers?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const sentence_s = 'The quick brown fox jumps over the lazy dog.';
const sentence_a = sentence_s.split(' ');
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;A more tricky example (with a smart inline type coercion tricks!):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let number_s = '123'
let number_n = +number_s    //: cast to Number
number_s = '' + ++number_n  //: cast to String
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Types specifiers
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;TypexJS&lt;/strong&gt; naming scheme applies to all primitive immutable types:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Null&lt;/li&gt;
&lt;li&gt;Undefined&lt;/li&gt;
&lt;li&gt;Boolean&lt;/li&gt;
&lt;li&gt;Number&lt;/li&gt;
&lt;li&gt;BigInt&lt;/li&gt;
&lt;li&gt;String&lt;/li&gt;
&lt;li&gt;Array&lt;/li&gt;
&lt;li&gt;Object&lt;/li&gt;
&lt;li&gt;Symbol&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It extends to specific Object types:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Class&lt;/li&gt;
&lt;li&gt;RegExp&lt;/li&gt;
&lt;li&gt;Function&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The suffix specifying the type is a single letter preceded by an underscore character:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;lowercase letters&lt;/strong&gt;
for "wrapped" types: &lt;em&gt;Boolean&lt;/em&gt;, &lt;em&gt;Number&lt;/em&gt;, &lt;em&gt;BigInt&lt;/em&gt;, &lt;em&gt;String&lt;/em&gt;, &lt;em&gt;Array&lt;/em&gt;, &lt;em&gt;Object&lt;/em&gt;, &lt;em&gt;Symbol&lt;/em&gt;, &lt;em&gt;Function&lt;/em&gt;, &lt;em&gt;Class&lt;/em&gt;, &lt;em&gt;RegExp&lt;/em&gt; 
(see &lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/Primitive"&gt; MDN Primitive description&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;uppercase letters&lt;/strong&gt; for "unwrapped" types: &lt;em&gt;Null&lt;/em&gt;, &lt;em&gt;Undefined&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Type&lt;/th&gt;
&lt;th&gt;Suffix&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Null&lt;/td&gt;
&lt;td&gt;_&lt;em&gt;N&lt;/em&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Undefined&lt;/td&gt;
&lt;td&gt;_&lt;em&gt;U&lt;/em&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Boolean&lt;/td&gt;
&lt;td&gt;_&lt;em&gt;b&lt;/em&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Number&lt;/td&gt;
&lt;td&gt;_&lt;em&gt;n&lt;/em&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;BigInt&lt;/td&gt;
&lt;td&gt;_&lt;em&gt;i&lt;/em&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;String&lt;/td&gt;
&lt;td&gt;_&lt;em&gt;s&lt;/em&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Array&lt;/td&gt;
&lt;td&gt;_&lt;em&gt;a&lt;/em&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Object&lt;/td&gt;
&lt;td&gt;_&lt;em&gt;o&lt;/em&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Symbol&lt;/td&gt;
&lt;td&gt;_&lt;em&gt;y&lt;/em&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Class&lt;/td&gt;
&lt;td&gt;_&lt;em&gt;c&lt;/em&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;RegExp&lt;/td&gt;
&lt;td&gt;_&lt;em&gt;r&lt;/em&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;To convey even more meaningful information, function identifiers follow a slightly different scheme: &lt;strong&gt;two underscore&lt;/strong&gt; characters before the type specifier &lt;strong&gt;of the returned value&lt;/strong&gt;. Consequently, if a function doesn't return anything (&lt;code&gt;void&lt;/code&gt; function), the &lt;em&gt;v&lt;/em&gt; letter is used for its suffix!&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Function return is&lt;/th&gt;
&lt;th&gt;Suffix&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Null&lt;/td&gt;
&lt;td&gt;__&lt;em&gt;N&lt;/em&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Undefined&lt;/td&gt;
&lt;td&gt;__&lt;em&gt;U&lt;/em&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Boolean&lt;/td&gt;
&lt;td&gt;__&lt;em&gt;b&lt;/em&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Number&lt;/td&gt;
&lt;td&gt;__&lt;em&gt;n&lt;/em&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;BigInt&lt;/td&gt;
&lt;td&gt;__&lt;em&gt;i&lt;/em&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;String&lt;/td&gt;
&lt;td&gt;__&lt;em&gt;s&lt;/em&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Array&lt;/td&gt;
&lt;td&gt;__&lt;em&gt;a&lt;/em&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Object&lt;/td&gt;
&lt;td&gt;__&lt;em&gt;o&lt;/em&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Symbol&lt;/td&gt;
&lt;td&gt;__&lt;em&gt;y&lt;/em&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Class&lt;/td&gt;
&lt;td&gt;__&lt;em&gt;c&lt;/em&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;RegExp&lt;/td&gt;
&lt;td&gt;__&lt;em&gt;r&lt;/em&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Function&lt;/td&gt;
&lt;td&gt;__&lt;em&gt;f&lt;/em&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Void&lt;/td&gt;
&lt;td&gt;__&lt;em&gt;v&lt;/em&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const awesome__s = () =&amp;gt; 'An awesome String'
const clone__s = awesome__s
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In case of a function returning different types of value, we just omit the type character:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const silly__ = string_b =&amp;gt; string_b ? 'A weird String' : 1234
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We could do the same for a variable accepting different kinds of type, but is it really a good practice?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let hybrid_ = 'A String'
hybrid_ = 1234  //: What a mess!
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Full example
&lt;/h2&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const DOM_siblings__a = selector_s =&amp;gt;
{
  const node_e = document.querySelector( selector_s )
  return !node_e ?
    null :
    Array.prototype.filter
      .call( node_e.parentNode.children, sibling_e =&amp;gt; sibling_e !== node_e )
}

const DOM_listReverse = selector_s =&amp;gt;
{
  const node_a = Array.prototype.slice.call( document.querySelectorAll( `${selector_s} li` ))
  node_a.forEach( node_e =&amp;gt; node_e.parentNode.insertBefore( node_e, node_e.parentNode.firstChild ) )
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The previous code shows an &lt;strong&gt;exception&lt;/strong&gt; to the specifiers convention used by &lt;strong&gt;TypexJS&lt;/strong&gt;: for &lt;code&gt;DOM&lt;/code&gt; elements, it seems more appropriate to use the &lt;code&gt;_e&lt;/code&gt; suffix (for &lt;em&gt;element&lt;/em&gt;, because the &lt;code&gt;_N&lt;/code&gt; suffix is already used for a &lt;code&gt;Null&lt;/code&gt; value) than the &lt;code&gt;_o&lt;/code&gt; suffix used for &lt;code&gt;Object&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The example code shows another singularity: we can instantly differenciate JavaScript and browser specific functions or methods from our own code functions and methods. Our identifiers have a letter suffix where JS and browser identifiers have nothing. Therefore, we can safely use exactly the same names:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const myWindow = window  // usual way
const window_o = window  // smarter isn't it?
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Hence, everytime we want to override some JavaScript or DOM function&lt;br&gt;
(or any browser specific, third-party library function, etc.), we can safely do it while keeping a semantic coherency with the original function.&lt;/p&gt;

&lt;p&gt;This very simple and easy to memorize convention can help you to write a better code while conveying more information about it as well as shortening its documentation. You can find more details about &lt;strong&gt;&lt;a href="https://typexjs.netlify.com"&gt;TypexJS&lt;/a&gt;&lt;/strong&gt; visiting its dedicated site and put comments or questions if you see fit.&lt;/p&gt;

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