Channels ▼

Christopher Diggins

Dr. Dobb's Bloggers

Subtyping Primitives (or Abuse of the Integer)

November 21, 2009

A common source of bugs in the code bases I am working with lately (3d graphics in C# and C++) are related to the overloading of the notion of an integer.

 In many 3d graphics libraries, like XNA, a common data representation of mesh data is an array of vertexes combined with an array of indexes into the original array.

In building an application in C# using the XNA game library requires that I keep straight whether I am dealing with indexes into the vertex array, or indexes into the index array. This gets confusing because in C# I had to represent everything as an integer. 

I tried writing a struct to represent Vertex identifiers, instead of using raw integers, but the amount of extra code and complexity didn't really justify it. Anyone reading my code, would have just been wondering why the heck I wasn't using integers. 

This isn't the first time that integers have given me a problem. In the C++ SDK for 3ds Max which I document, for example, there are lots of gotchas related to a distinction between identifier and index. For example parameter identifiers (ParamID which is typedef'd as a short int) are not the same as the parameter indexes. Because the parameter id's and indexes are often the same, this means that lots of code has latent bugs that only rarely occur. 

A lot of bugs can be avoided if a programming language makes it easy and convenient to define new ordinal types. Effectively I want to write:

 

 class VertexID : Int { } 

 

And be done with it.  I don't really see why this isn't yet possible in most languages. 

Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 


Video