4
\$\begingroup\$

So in my current application, I was thinking to make a extended class for all the datatypes. For int, it will be something like ExtendedInt, for bool it will be something like ExtendedBool.

These individual extended classes will expose some special functonality bound to that type. For example, this is how i think my ExtendedInt will look like:

public interface IExtended
{
   bool IsModified { get; set; }
}

Public class ExtendedInt: IExtended
{
   private bool m_IsModified;
   private int m_MaxValue = int.MaxValue ;
   private int m_MinValue = int.MinValue;
   private int m_Value=int.MinValue;
   private int m_MaxValue = int.MaxValue ;
   private int m_MinValue = int.MinValue;

   public bool IsModified
      {
    get { return m_IsModified; }
    set { m_IsModified = value; }
      }


    public int Value
    {
        get { return m_Value; }
        set
    {
    // Only change the value if it is different.

             if (m_Value != value)
       {
          // Only set the value if it is in the valid range, if any max/min exists.
          if ((m_MaxValue > -1) && (value > m_MaxValue))
          {
              throw new ArgumentException("Value is above maximum value permitted.");
           }
           else if ((m_MinValue > -1) && (value < m_MinValue))
           {
               throw new ArgumentException("Value is below minimum value permitted.");
           }
           else
           {
              m_Value = value;
              this.IsChanged = true;
              if (m_Parent != null) m_Parent.DirtyState();
            }
               }
           }
       }

Similarly, I will provide ExtendedBool, ExtendedDouble, ExtendedDate, ExtendedString, etc..

Is this considered the best practice or it is overhead for my application?

\$\endgroup\$
1
  • \$\begingroup\$ You do realize that basic types are already an abstraction, right? Why complicate things unnecessarily? What value do your abstractions provide and what potential problems could they possibly introduce? Is the time you are spending on this actually adding value or is it simply giving you something to toy around with? \$\endgroup\$ Commented Feb 8, 2012 at 23:25

2 Answers 2

5
\$\begingroup\$

Too much overhead - nullable types and extension methods seem to give you what you need. The checking you do on your setters will never be executed because an int will never be less than int.MinValue or greater than int.MaxValue to begin with.

Only create wrappers over types where you're adding intrinsic business (or technical) value that your consumers will benefit from. I'm afraid the ExtendedInt example you provide doesn't seem to meet that criteria over int? myInt;

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Agreed. You are absolutely right abt Extension methods and i think i shud use them if i need to give additional flexibility on my type. Thanks for the help. \$\endgroup\$
    – Asdfg
    Commented Feb 9, 2012 at 0:50
2
\$\begingroup\$

@Asdfg - Keep it simple!

As written, I would say this is not a best practice at all, and should be avoided. It will be a (small) performance drag and a (large) addition of non-useful complexity if your plan is sweeping - any int will instead be ExtendedInt.

[I say "small" performance drag since I don't think that will be a problem compared to the far larger penalty in the complexity you will be introducting. One example: will you be supporting all int primatives in ExtendedInt like +, /, %, ++, casting, bitmasks, and so forth? Other programmers working on your code will be confused. Very messy.]

On the other hand, there is nothing wrong with writing a class that encapsulates needed behavior - in fact, that is a big part of object oriented design. Just that you would only use it when you have a good reason.

In your particular example, my read of your code suggests perhaps you are attempting to create a BoundedInt abstraction rather than the (too) generic ExtendedInt which doesn't tell us anything about the intent of the class. What does "Extended" mean? Doesn't reveal much.

Your comment // Only set the value if it is in the valid range, if any max/min exists. further suggests you intend to allow objects of this class to have settable ranges (though these methods are not shown). As Jesse points out, as written, the defaults you give are not useful, but with customizable ranges this might be a useful class. A plausible use case would be for a data object to associate with a UI element for which only a given range of values is allowed (disallowing negative age values, or rental dates in the past, and so forth).

Similarly, the IsModified method suggests tracking data updates. Only useful if you have a scenario where knowing whether an int has changed value. And realize these are two separate concerns - dirty tracking & restricting its range.

\$\endgroup\$
2
  • 2
    \$\begingroup\$ But if he is aiming for that "range" idea, I would suggest a generic class Range<T> or something similar, maybe with readonly properties T? MinValue and T? MaxValue. \$\endgroup\$
    – ANeves
    Commented Feb 9, 2012 at 9:25
  • \$\begingroup\$ I agree @ANeves that would be a good idea for implementing such a class. \$\endgroup\$ Commented Feb 9, 2012 at 14:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.