<?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: MN Mark</title>
    <description>The latest articles on DEV Community by MN Mark (@mjb2kmn).</description>
    <link>https://dev.to/mjb2kmn</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%2F61254%2F1238e831-45a4-4b46-8634-8a76a9928b82.jpg</url>
      <title>DEV Community: MN Mark</title>
      <link>https://dev.to/mjb2kmn</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mjb2kmn"/>
    <language>en</language>
    <item>
      <title>Terminology and structure of defining record relationships</title>
      <dc:creator>MN Mark</dc:creator>
      <pubDate>Mon, 05 Aug 2019 18:24:52 +0000</pubDate>
      <link>https://dev.to/mjb2kmn/terminology-and-structure-of-defining-record-relationships-217d</link>
      <guid>https://dev.to/mjb2kmn/terminology-and-structure-of-defining-record-relationships-217d</guid>
      <description>&lt;p&gt;Looking for some opinions on naming and structure when matching a data entry to another data entry.&lt;br&gt;
Specifically the use case is matching a file to a host, but this same methodology will be applied to other relationships.&lt;/p&gt;

&lt;p&gt;A file on disk contains yaml front matter with a separator after which is the contents of the file, very similar to posts here on dev.to. In the front matter I need to list attributes of a Host to match and attributes that should not match as well as define greater/less-than tests. It is important that the naming and structure be understandable and approachable by systems administrators.&lt;/p&gt;
&lt;h2&gt;
  
  
  Base context
&lt;/h2&gt;

&lt;p&gt;Simplified example without matching criteria, this will be the static context used in all examples:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;path: /etc/hosts
mode: 0644
user: root
group: root
---
127.0.0.1 localhost
::1 ip6-localhost
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  First Option
&lt;/h2&gt;

&lt;p&gt;Now to add criteria to match only &lt;code&gt;Hosts&lt;/code&gt; which have a &lt;code&gt;OS.Type&lt;/code&gt; of "Linux", and &lt;code&gt;OS.Version&lt;/code&gt; greater than &lt;code&gt;18.00&lt;/code&gt; but not matching a &lt;code&gt;Platform&lt;/code&gt; of "prlvm":&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;path: /etc/hosts
mode: 0644
user: root
group: root
assert:
  - os.type: linux
  - os.version: 18.00+
refute:
  - platform: prlvm
---
127.0.0.1 localhost
::1 ip6-localhost
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here the &lt;code&gt;assert&lt;/code&gt; key contains a list of Host attributes and values that must match. The &lt;code&gt;os.version&lt;/code&gt; would need special treatment to be compared as a floating-point value even thought it's stored and transmit as a string and the operators &lt;code&gt;+&lt;/code&gt; and &lt;code&gt;-&lt;/code&gt; would need to be parsed out and evaluated accordingly. I think this is mostly clear and concise from the users' end, but special treatment of values such as &lt;code&gt;os.version&lt;/code&gt; could make this nightmarishly complex later down the road.&lt;/p&gt;

&lt;p&gt;I also am considering &lt;code&gt;match&lt;/code&gt; and &lt;code&gt;except&lt;/code&gt; terms in place of &lt;code&gt;assert&lt;/code&gt; and &lt;code&gt;refute&lt;/code&gt;, respectively, because they might be more intuitive to sysadmins.&lt;/p&gt;

&lt;h2&gt;
  
  
  Second Option
&lt;/h2&gt;

&lt;p&gt;Another approach is to thin out the abstraction and use a more direct representation of the matches happening in the program. I think this is more accurate at the cost of being more verbose.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;path: /etc/hosts
mode: 0644
user: root
group: root
match:
  equal:
    - os.type: linux
  greaterthan:
    - os.version: 18.00
  not:
    - platform: prlvm
---
127.0.0.1 localhost
::1 ip6-locahost
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;That would produce the same end result but have simpler logic in the application processing it. We could reasonably assume that values submitted under the &lt;code&gt;greaterthan&lt;/code&gt; key could be treated as numbers without special parsing. We could more clearly differentiate between &lt;code&gt;equal&lt;/code&gt; and &lt;code&gt;contains&lt;/code&gt; and even add operators such as &lt;code&gt;startswith&lt;/code&gt;. I think this is starting to look like elasticsearch filters which I was never a big fan of, but it does make the backend coding safer and easier and I think more clearly set expectations for the user.&lt;/p&gt;

&lt;p&gt;In the last example, &lt;code&gt;match&lt;/code&gt; could instead be &lt;code&gt;select&lt;/code&gt; or other term.&lt;/p&gt;

&lt;p&gt;Ultimately I am trying to make it the most friendly to the sysadmin end user while still being reasonably safe, testable, and not overly complex for the developer.&lt;/p&gt;

&lt;h2&gt;
  
  
  Update
&lt;/h2&gt;

&lt;p&gt;I am moving forward with the Second Option. I think the benefits of clarity are worth the increased verbosity. While the baseline complexity is more than other options I believe the complexity will not significantly increase with the number and size of records. It also requires more logic in the application but not more complex logic, this should make the program easier to maintain and test.&lt;/p&gt;

</description>
      <category>discuss</category>
    </item>
    <item>
      <title>Pointers in practice</title>
      <dc:creator>MN Mark</dc:creator>
      <pubDate>Thu, 04 Apr 2019 21:58:41 +0000</pubDate>
      <link>https://dev.to/mjb2kmn/pointers-in-practice-4fn4</link>
      <guid>https://dev.to/mjb2kmn/pointers-in-practice-4fn4</guid>
      <description>&lt;p&gt;I generally understand what pointers are and how they work. Where I run into trouble is when to use them in my own code.&lt;/p&gt;

&lt;p&gt;When I have a struct that I want to create a new instance of, when should I return the instance and when should I return it as a pointer? Say I have a function to get a &lt;code&gt;Session&lt;/code&gt; instance in a web app. It either looks up the session from a cookie or creates a new one. In any situation it will always return a valid Session. Should this return &lt;code&gt;Session&lt;/code&gt; or &lt;code&gt;*Session&lt;/code&gt;? Why?&lt;/p&gt;

&lt;p&gt;Now consider a card game that has &lt;code&gt;Card&lt;/code&gt;, &lt;code&gt;Deck&lt;/code&gt;, and &lt;code&gt;Hand&lt;/code&gt; structs. When creating a &lt;code&gt;Hand&lt;/code&gt;, a &lt;code&gt;Card&lt;/code&gt; is removed from the &lt;code&gt;Deck&lt;/code&gt; and put in a &lt;code&gt;Hand&lt;/code&gt;. There can only ever be 1 instance of a particular &lt;code&gt;Card&lt;/code&gt;, such as 8 of hearts, should &lt;code&gt;Card&lt;/code&gt; be passed around as a pointer? Would this be more memory efficient? Is there a concern about garbage collection here? Or can the GC still keep track of memory only being passed as a pointer?&lt;/p&gt;

&lt;p&gt;My experience is in languages like Java, Ruby, Lua, Php, if that helps know where I'm coming from.&lt;/p&gt;

</description>
      <category>go</category>
      <category>help</category>
      <category>discuss</category>
    </item>
    <item>
      <title>Failing First / Failing Fast</title>
      <dc:creator>MN Mark</dc:creator>
      <pubDate>Wed, 06 Mar 2019 18:29:44 +0000</pubDate>
      <link>https://dev.to/mjb2kmn/failing-first--failing-fast-4gik</link>
      <guid>https://dev.to/mjb2kmn/failing-first--failing-fast-4gik</guid>
      <description>&lt;h1&gt;
  
  
  Failing First and Failing Fast
&lt;/h1&gt;

&lt;p&gt;When writing new code I typically focus on the successful path first, and this seems to almost always result in terrible code. Terrible code on the first pass isn't really a problem, a brain dump into code usually produces bad code, at least for me. The important thing is that the brain dump code gets followed up on one or more times to simplify the logic. Too often those passes are months or even years later.&lt;/p&gt;

&lt;p&gt;Example case is validating a Single-Sign-On (SSO) token in a cookie with &lt;a href="https://github.com/openresty/lua-nginx-module"&gt;Lua and Nginx&lt;/a&gt;.&lt;br&gt;&lt;br&gt;
My first pass included something like this:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;code is simplified for illustration&lt;/em&gt;&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function check_sso()
  local cookies = ngx.req.get_headers()['Cookie']
  if cookies then
    local token = cookies:match('sso_token=([^; ]+)')
    if token then
      local username = get_username_from_token(token)
      if username then
        if is_member_of(username, GROUP_NAME) then
          return true
        else
          ngx.log(ngx.WARN, username .. ' is not in group ' .. GROUP_NAME)
          return false
        end
      end
    end
  end
end
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;That follows a simple logic when you read through it, but it's hideous to look at and easy to lose your place in the nesting. The actual code with debug logging and token caching is worse due to the length of the nested blocks.&lt;/p&gt;

&lt;p&gt;Consider what it looks like when we handle the failing side of the checks first:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function check_sso()
  local cookies = ngx.req.get_headers()['Cookie']
  if not cookies then
    return false
  end
  local token = cookies:match('sso_token=([^; ]+)')
  if not token then
    return false
  end
  local username = get_username_from_token(token)
  if not username then
    return false
  end
  if is_member_of(username, GROUP_NAME) then
    return true
  else
    ngx.log(ngx.WARN, username .. ' is not in group ' .. GROUP_NAME)
    return false
  end
end
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Despite being more lines I find this version simpler and visually easier to read. When I come back to code written like this I find it much easier to modify and troubleshoot. Yet I still struggle to modify my thinking to use this type logic in the first place.&lt;/p&gt;

&lt;p&gt;Anyone else have similar trouble with thought process differing from optimal code logic?&lt;/p&gt;

&lt;p&gt;Does anyone disagree and think the first example is better or at least no worse than the second?&lt;/p&gt;

</description>
      <category>discuss</category>
      <category>coding</category>
    </item>
    <item>
      <title>Returning type-safe &amp; non-nil failure in Crystal?</title>
      <dc:creator>MN Mark</dc:creator>
      <pubDate>Mon, 18 Feb 2019 18:37:07 +0000</pubDate>
      <link>https://dev.to/mjb2kmn/returning-type-safe--non-nil-failure-in-crystal-n01</link>
      <guid>https://dev.to/mjb2kmn/returning-type-safe--non-nil-failure-in-crystal-n01</guid>
      <description>&lt;p&gt;I am having design doubts in Crystal and would like to hear how others have implemented similar functionality.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;User&lt;/code&gt; class has a method to fetch data based on some attribute value and return an instance.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class User
  def self.get_by_login(login : String)
    obj = DB.irrelevant_method(login)
    return obj
  end
end
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;em&gt;(I am using superfluous return statements to be more illustrative of intent)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The issue comes in when the database does not find a record to return. I do not want to return a union type such as &lt;code&gt;(User|Nil)&lt;/code&gt; or &lt;code&gt;(User|Bool)&lt;/code&gt;. My idea is to return a User instance to satisfy the return type with the specific error added to an existing &lt;code&gt;errors&lt;/code&gt; array I'm already using for validation on the creation side.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class User
  def self.get_by_login(login : String)
    user = User.new
    begin
      user = DB.irrelevant_method(login)
    rescue ex
      user.errors &amp;lt;&amp;lt; ex.to_s
    end
    return user
  end
end
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This leaves it up to the caller to check if the returned object is valid or has any errors before knowing what to do with it.&lt;/p&gt;

&lt;p&gt;Any thoughts or concerns about this approach in Crystal or in general? I've used this pattern before in other languages and it does work, but I've never really felt great about it. I'm curious to hear how others have solved this or similar issues in Crystal or other strongly typed languages.&lt;/p&gt;

</description>
      <category>crystal</category>
      <category>discuss</category>
    </item>
    <item>
      <title>Go zero values finally hit me</title>
      <dc:creator>MN Mark</dc:creator>
      <pubDate>Tue, 12 Feb 2019 20:44:41 +0000</pubDate>
      <link>https://dev.to/mjb2kmn/go-zero-values-finally-hit-me-58ec</link>
      <guid>https://dev.to/mjb2kmn/go-zero-values-finally-hit-me-58ec</guid>
      <description>&lt;p&gt;I decided to learn Go (not my first language) by opening the docs and an editor and just Going for it. After 2 weeks and a few hundred lines, I had a functional and useful tool and am mostly enjoying working with Go. &lt;/p&gt;

&lt;p&gt;I had seen references to "zero values" here and there but didn't pay much attention. My variable definitions, assignments, flow control, and such were all working as expected so I didn't feel I was missing anything.&lt;br&gt;
Then, suddenly, the idea of a Boolean variable's &lt;em&gt;zero value&lt;/em&gt; being &lt;code&gt;false&lt;/code&gt; penetrated my willful ignorance and I re-examined my code. What I found were lots of familiar patterns like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type Test struct {
    Pass    bool
}


if len(test.Passes) &amp;gt; 0 &amp;amp;&amp;amp; len(test.Fails) == 0 {
    test.Pass = true
} else {
    test.Pass = false
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;But if a "zero value" means that simply declaring a variable name and type guarantees it gets initialized with a known value, then &lt;code&gt;test.Pass&lt;/code&gt; was already &lt;code&gt;false&lt;/code&gt; as soon as it existed. Meaning that all those &lt;code&gt;else&lt;/code&gt; branches were redundant.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type Test struct {
    Pass    bool
}


if len(test.Passes) &amp;gt; 0 &amp;amp;&amp;amp; len(test.Fails) == 0 {
    test.Pass = true
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;I was able to cut out a large portion of lines and simplify logic just by knowing and using the zero values of all the basic types.&lt;/p&gt;

&lt;p&gt;After this I decided to not learn solely by the language specification and bought a book. The Go Programming Language book covers "zero values" within about the first dozen pages. (&lt;code&gt;palm.Smack(&amp;amp;forehead)&lt;/code&gt;)&lt;/p&gt;

&lt;p&gt;The lesson here isn't only that Go's zero values make Go code more succinct, safe, and readable, but... well, I'm pretty sure there is another lesson in here. Maybe someone can spot it and explain it to me? ;)&lt;/p&gt;

</description>
      <category>go</category>
      <category>learning</category>
    </item>
    <item>
      <title>A better way to handle magic values and constants?</title>
      <dc:creator>MN Mark</dc:creator>
      <pubDate>Tue, 19 Jun 2018 17:15:30 +0000</pubDate>
      <link>https://dev.to/mjb2kmn/a-better-way-to-handle-magic-values-and-constants-2o4g</link>
      <guid>https://dev.to/mjb2kmn/a-better-way-to-handle-magic-values-and-constants-2o4g</guid>
      <description>&lt;p&gt;It seems pretty well known that "magic values" in code are a bad idea and should be given descriptive names instead. Instead of this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;throw new Exception("invalid state: 0x02")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;it's preferred to define "magic values" as constants:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Integer STATE_OK = 0x01;
Integer STATE_BAD = 0x02;
...
throw new Exception ("invalid state: {STATE_BAD}")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This works well thus far. However, what the user sees in either case is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ invalid state: 02
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I am thinking of frameworks and tools designed for technical users here, not non-tech end users.&lt;/p&gt;

&lt;p&gt;In my actual use case I was able to find the source code and search out the constant name of the value reported to discover the actual problem, but this is rather inconvenient and not always possible like when using closed source tools or the user isn't familiar with the given language.&lt;/p&gt;

&lt;p&gt;As a developer I am interested in how this experience can be improved. On the surface it seems string values would be an improvement but that clearly doesn't scale well. Is this just a documentation failure and no better way to deal with it in the code? Ever see a drastically different approach?&lt;/p&gt;

</description>
      <category>discuss</category>
    </item>
    <item>
      <title>Is bubbling scroll events terrible?</title>
      <dc:creator>MN Mark</dc:creator>
      <pubDate>Wed, 23 May 2018 15:10:19 +0000</pubDate>
      <link>https://dev.to/mjb2kmn/is-bubbling-scroll-events-terrible-24g</link>
      <guid>https://dev.to/mjb2kmn/is-bubbling-scroll-events-terrible-24g</guid>
      <description>&lt;p&gt;First, as a user, when on a web page that scrolls and contains elements which scroll, I find it terrible behavior to scroll the parent of an element when it has reached it's end. To be clear: a page that is 1000px high with a window that is 700px high will scroll. This page then has an element that is sized to 200px high with 400px of content scrolling inside it. When you place your mouse cursor over the 200px element and scroll, the content in that element will scroll. When you reach the end of that content, and the mouse cursor is still hovering that same element, the page will then begin to scroll.&lt;/p&gt;

&lt;p&gt;I find this behavior insane and terrible, am I the odd-ball or outlier on this? Or do others also find this at least irritating or problematic?&lt;/p&gt;

&lt;p&gt;Related but not addressed here is the issue of scrolling down a long page and a scrollable element becoming focused by moving under the stationary cursor and then acting on the scrolling event?&lt;/p&gt;

&lt;p&gt;Is there just a bigger problem of bad user experience from nested scrolling?&lt;/p&gt;

&lt;p&gt;Second; is there a way, maybe with injected javascript from a browser plugin, to change this behavior? Is it possible to find all scrolling elements and prevent them from bubbling the scroll event? (I'm ok with JS, but I'm years out of practice with DOM)&lt;/p&gt;

</description>
      <category>discuss</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
