<commit_before>---
layout: post
tags: [Software Design]
date: 2011-05-27 13:21:06 UTC
title: "Design Smell: Redundant Required Attribute"
---
{% include JB/setup %}

<div id="post">
	<p>
		This post is the fourth in a series about <a href="http://blog.ploeh.dk/2011/05/24/Poka-yokeDesignFromSmelltoFragrance">Poka-yoke Design</a>  -  also known as <em>encapsulation</em>.
	</p>
	<p>
		Recently I saw this apparently enthusiastic tweet reporting from some Microsoft technology event:
	</p>
	<blockquote>
		<p>
			<a href="https://twitter.com/#!/jennifermarsman/status/57850114424848385">[Required] attribute in code automatically creates a non-nullable entry in DB and validation in the webpage  -  nice […]</a>
		</p>
	</blockquote>
	<p>
		I imagine that it must look something like this:
	</p>
	<p>
		<pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">class</font></span> </font><span style="color: "><font style="font-size: 10pt" color="#2b91af">Smell</font></span>
<font style="font-size: 10pt">{</font>
<font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; [<span style="color: "><font color="#2b91af">Required</font></span>]</font>
<font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">public</font></span> <span style="color: "><font color="#0000ff">int</font></span> Id { <span style="color: "><font color="#0000ff">get</font></span>; <span style="color: "><font color="#0000ff">set</font></span>; }</font>
<font style="font-size: 10pt">}</font></pre>
	</p>
	<p>
		Every time I see something like this I die a little inside. If you already read my previous posts it should by now be painfully clear why this breaks encapsulation. Despite the [Required] attribute there's <a href="http://blog.ploeh.dk/2011/05/24/DesignSmellTemporalCoupling">no guarantee that the Id property will ever be assigned a value</a>. The attribute is just a piece of garbage making a claim it can't back up.
	</p>
	<p>
		Code like that is not fail-safe.
	</p>
	<p>
		I understand that the attribute mentioned in the above tweet is intended to signal to some tool (probably EF) that the property must be mapped to a database schema as non-nullable, but it's still redundant. Attributes are not the correct way to make a statement about invariants.
	</p>
	<p>
		<strong>Improved Design</strong>
	</p>
	<p>
		The [Required] attribute is redundant because there's a much better way to state that a piece of data is required. This has been possible since .NET 1.0. Here's the Poka-yoke version of that same statement:
	</p>
	<p>
		<pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">class</font></span> </font><span style="color: "><font style="font-size: 10pt" color="#2b91af">Fragrance</font></span>
<font style="font-size: 10pt">{</font>
<font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">private</font></span> <span style="color: "><font color="#0000ff">readonly</font></span> <span style="color: "><font color="#0000ff">int</font></span> id;</font>
<font style="font-size: 10pt">&nbsp;</font>
<font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">public</font></span> Fragrance(<span style="color: "><font color="#0000ff">int</font></span> id)</font>
<font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font>
<font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">this</font></span>.id = id;</font><commit_msg>Converted to real paragraph headings<commit_after>---
layout: post
tags: [Software Design]
date: 2011-05-27 13:21:06 UTC
title: "Design Smell: Redundant Required Attribute"
---
{% include JB/setup %}

<div id="post">
	<p>
		This post is the fourth in a series about <a href="http://blog.ploeh.dk/2011/05/24/Poka-yokeDesignFromSmelltoFragrance">Poka-yoke Design</a>  -  also known as <em>encapsulation</em>.
	</p>
	<p>
		Recently I saw this apparently enthusiastic tweet reporting from some Microsoft technology event:
	</p>
	<blockquote>
		<p>
			<a href="https://twitter.com/#!/jennifermarsman/status/57850114424848385">[Required] attribute in code automatically creates a non-nullable entry in DB and validation in the webpage  -  nice […]</a>
		</p>
	</blockquote>
	<p>
		I imagine that it must look something like this:
	</p>
	<p>
		<pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">class</font></span> </font><span style="color: "><font style="font-size: 10pt" color="#2b91af">Smell</font></span>
<font style="font-size: 10pt">{</font>
<font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; [<span style="color: "><font color="#2b91af">Required</font></span>]</font>
<font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">public</font></span> <span style="color: "><font color="#0000ff">int</font></span> Id { <span style="color: "><font color="#0000ff">get</font></span>; <span style="color: "><font color="#0000ff">set</font></span>; }</font>
<font style="font-size: 10pt">}</font></pre>
	</p>
	<p>
		Every time I see something like this I die a little inside. If you already read my previous posts it should by now be painfully clear why this breaks encapsulation. Despite the [Required] attribute there's <a href="http://blog.ploeh.dk/2011/05/24/DesignSmellTemporalCoupling">no guarantee that the Id property will ever be assigned a value</a>. The attribute is just a piece of garbage making a claim it can't back up.
	</p>
	<p>
		Code like that is not fail-safe.
	</p>
	<p>
		I understand that the attribute mentioned in the above tweet is intended to signal to some tool (probably EF) that the property must be mapped to a database schema as non-nullable, but it's still redundant. Attributes are not the correct way to make a statement about invariants.
	</p>
	<h3 id="19c9811ac7ea433c9d327d04b9845952">
		Improved Design <a href="#19c9811ac7ea433c9d327d04b9845952" title="permalink">#</a>
	</h3>
	<p>
		The [Required] attribute is redundant because there's a much better way to state that a piece of data is required. This has been possible since .NET 1.0. Here's the Poka-yoke version of that same statement:
	</p>
	<p>
		<pre style="margin: 0px"><span style="color: "><font color="#0000ff"><font style="font-size: 10pt">public</font></font></span><font style="font-size: 10pt"> <span style="color: "><font color="#0000ff">class</font></span> </font><span style="color: "><font style="font-size: 10pt" color="#2b91af">Fragrance</font></span>
<font style="font-size: 10pt">{</font>
<font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">private</font></span> <span style="color: "><font color="#0000ff">readonly</font></span> <span style="color: "><font color="#0000ff">int</font></span> id;</font>
<font style="font-size: 10pt">&nbsp;</font>
<font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">public</font></span> Fragrance(<span style="color: "><font color="#0000ff">int</font></span> id)</font>
<font style="font-size: 10pt">&nbsp;&nbsp;&nbsp; {</font>
<font style="font-size: 10pt">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color: "><font color="#0000ff">this</font></span>.id = id;</font>
{'fraction_non_alphanumeric': 0.1906691061160481, 'fraction_numerical': 0.04495556717198118, 'mean_word_length': 5.707274320771253, 'pattern_counts': {'":': 0, '<': 269, '<?xml version=': 0, '>': 269, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2494461', 'n_tokens_mistral': 2825, 'n_tokens_neox': 2496, 'n_words': 753}