<?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: Esteban A. Maringolo</title>
    <description>The latest articles on DEV Community by Esteban A. Maringolo (@emaringolo).</description>
    <link>https://dev.to/emaringolo</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%2F186557%2Fb36574bf-9947-4c9d-8d5e-fe6d1179c065.jpg</url>
      <title>DEV Community: Esteban A. Maringolo</title>
      <link>https://dev.to/emaringolo</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/emaringolo"/>
    <language>en</language>
    <item>
      <title>Naming Smalltalk method parameters</title>
      <dc:creator>Esteban A. Maringolo</dc:creator>
      <pubDate>Tue, 06 Feb 2024 15:30:38 +0000</pubDate>
      <link>https://dev.to/emaringolo/naming-smalltalk-method-parameters-3ci0</link>
      <guid>https://dev.to/emaringolo/naming-smalltalk-method-parameters-3ci0</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;I like to name things. A lot. And I appreciate unambiguity like a compiler. In some of my previous jobs, both as an employee and as a contractor, I was responsible for naming (and renaming) a good part of the codebase wherever possible.&lt;/p&gt;

&lt;p&gt;I've wanted to write about naming classes and methods for a long time. Remembering some rules and recommendations from &lt;a href="https://www.goodreads.com/book/show/781561.Smalltalk_Best_Practice_Patterns"&gt;Kent Beck's &lt;em&gt;Smalltalk Best Practice Patterns&lt;/em&gt;&lt;/a&gt; (which are as valid as when they were published almost three decades ago) and some rules and conventions of my own I've been following since I started programming in Smalltalk at the beginning of this century.&lt;/p&gt;

&lt;p&gt;But what finally moved me to write about this was reading &lt;a href="https://www.amazon.com/Naming-Things-Hardest-Software-Engineering-ebook/dp/B0BT15GFZG"&gt;&lt;em&gt;Naming Things: The Hardest Problem in Software Engineering&lt;/em&gt;&lt;/a&gt;, because I thought that I hadn't seen much, if anything, written about this topic lately in the context of Smalltalk.&lt;/p&gt;

&lt;h2&gt;
  
  
  Parameter Naming Rule 0: anObject
&lt;/h2&gt;

&lt;p&gt;In Smalltalk it is very common to name method parameters by using the most general expected class in the name of the parameter, in the form of &lt;em&gt;indefinite article + class name&lt;/em&gt;, i.e. &lt;code&gt;aString&lt;/code&gt; or &lt;code&gt;aDictionary&lt;/code&gt;, the error-prone &lt;code&gt;aStringOrNil&lt;/code&gt; or the &lt;em&gt;catch-all&lt;/em&gt; &lt;code&gt;anObject&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;This is a community convention almost as old as the language itself that is characteristic of Smalltalk codebases and there's nothing wrong with it. It's useful and describes the class of the expected argument, given Smalltalk's late binding.&lt;/p&gt;

&lt;p&gt;Some examples of selectors with that convention are &lt;code&gt;printOn: aStream&lt;/code&gt;, &lt;code&gt;at: anObject ifAbsent: aBlock&lt;/code&gt;, &lt;code&gt;isKindOf: aClass&lt;/code&gt;, where the paramenter name tells you what object you should pass as an argument when sending a message.&lt;/p&gt;

&lt;h2&gt;
  
  
  Parameter Naming Rule 1: Only name parameters after existing classes
&lt;/h2&gt;

&lt;p&gt;There is a common practice that breaks the rule described in the introduction: naming a parameter after a class that doesn't exist.&lt;/p&gt;

&lt;p&gt;E.g.&lt;code&gt;accessor: aSelector&lt;/code&gt;, where there is no &lt;code&gt;Selector&lt;/code&gt; class.&lt;/p&gt;

&lt;p&gt;My rule here, which I think I read somewhere over ten years ago, is to avoid doing that, using one of the alternatives:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use the right class name: &lt;code&gt;accessor: aSymbol&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Use the semantic name, without article nor class name: &lt;code&gt;accessor: selector&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Parameter Naming Rule 2: Clearly distinguish between multiple parameters of the same class
&lt;/h2&gt;

&lt;p&gt;Sometimes a method has two or more parameters of the same class, which can look like &lt;code&gt;from: anOrigin to: anExtent&lt;/code&gt; or &lt;code&gt;copyFrom: aSource to: aTarget&lt;/code&gt;, &lt;code&gt;start: objectA end: objectB&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;All these examples have one or more issues, the obvious now is violating the PNR1, but also not having any semantic meaning. These could be rewritten as follows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;from: originPoint to: extentPoint&lt;/code&gt; or &lt;code&gt;from: origin to: extent&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;copyFrom: source to: target&lt;/code&gt; or, if applicable, &lt;code&gt;copyFrom: sourceRepository to: targetRepository&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;&lt;code&gt;start: startObject end: endObject&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Naming dynamics
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;“What you must learn is that these rules are no different than the rules of a computer system. Some of them can be bent. Others can be broken.”&lt;/em&gt; - Morpheus, The Matrix&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;There are often times when naming a parameter without following PNR0 will cause a clash between the parameter name and some instance variable name (e.g. &lt;code&gt;from: origin to: extent&lt;/code&gt; will likely clash with the &lt;code&gt;origin&lt;/code&gt; and &lt;code&gt;extent&lt;/code&gt; instVar names). In such cases, you will have to get creative and add a prefix or suffix to the parameter name if necessary.&lt;/p&gt;

&lt;h2&gt;
  
  
  Follow up
&lt;/h2&gt;

&lt;p&gt;I have some other rules and conventions I'd like to share with the development community. Including adding new ones to those originally published in this article. Stay tuned.&lt;/p&gt;

</description>
      <category>naming</category>
      <category>smalltalk</category>
      <category>programming</category>
      <category>style</category>
    </item>
  </channel>
</rss>
