I responded that he slept 4 nights in the street outside before leaving. Incorrect...
Actually, the man came and left on a horse named Friday. This is an illustration of my proposal that, through the magic of syntax and grammar, almost anything is possible. This is true even in C++!
If you use C#, you've probably learned to use and like the way you define property accessors in a class. Even better, you drop the () when you set or query the property, hence the properties are easily identified and distinguished in code from other class members. Properties are nouns and other methods are generally verbs. It's nice to have visual cues in your code to illustrate the difference. Here's a sample of some C# code:
string _filePath = Path.GetTempFileName();
public string FilePath
{
get
{
return _filePath;
}
set
{
if( string.IsNullOrEmpty(value) )
throw new ApplicationException("Why would you use a null file path, sport?");
_filePath = value;
}
}
The magic variable value is kind of like a this pointer for a class. Inside a set accessor, it carries the proposed new value. Beautiful. Now what about C++? C++ does not have accessors. If you make your data members public, you don't give your class any opportunity to validate a property change.
Remember the horse named Friday? Syntax and grammar makes the seemingly impossible possible in both English and C++. The C++ language supports operator overloading. You can overload numerous operations on a class, including +, -, =, ++, *, <<, etc. The haters would say that this is bad for the language. Why on earth would you allow someone to overload a + operator that can theoretically perform a subtraction instead? Because it's cool and thousands of people are using operator overloading to be more productive! Deal with it, you killjoys! It makes your code readable to have a string class with + and += operations; it's really intuitive.
One special kind of operator is a cast. Check out this example:
protected:
//Constructor fills this in!
wchar_t _chars[100];
public:
operator const wchar_t * () const
{
return _chars;
}
This creates an implicit cast to type
const wchar_t*. This is very powerful syntax! You can pass a stack allocated instance of your object around as if it was a unicode string. This gives us a basis for an accessor class. Say you want to have an accessor for an int. You can create a class with a cast operator to type int, and you can overload operators on it like =, +, ++, etc. Further, you can pass the validation off to an "outer" class that owns the accessor. It's kind of like the "validation" events you see in C#. Here's some code:With the magic of wordplay, we created a C++ class with properties that look like "nouns". The properties support a subset of normal C++ operators, and they allow the outer class to validate them. Looking at the class and three inner classes in Accessors.cpp, it would appear that there are encapsulation and coupling rules broken. However, you can look at it from a different perspective, that all the code is part of the Trigonometry class. The inner classes are like the accessors in C# -- just another member of the outer class.
Again, visual cues in your code that separate nouns and verbs make your code more readable. They make your code easier for others to review. In a fast paced project, making aesthetically pleasing classes may not be worthwhile. But when creating reusable frameworks, using accessors may add a touch of usability and intuitiveness to your APIs.
Time for me to ride off on Friday. Cheers,
Chris

No comments:
Post a Comment
Please keep it short, respectful, and clean!