I\'m experimenting with a naming convention I\'ve devised and am seeking opinion
ID: 642343 • Letter: I
Question
I'm experimenting with a naming convention I've devised and am seeking opinion. It involves the use of an "As" infix, and at this point I am envisioning it in the context of JavaScript, C# and C++ coding to assist with clarity of data format/contract when dealing with conversions or multiple representations of the same inherent data or object.
Examples:
// ex 1. receive and convert user input.
string userInput = "123";
float userInputAsFloat = float.Parse(userInput);
int userInputAsInt = (int)Math.Round(userInputAsFloat);
// ex 2. prepare an object for json transport.
Dictionary<string,object> packet = new Dictionary<string,object>();
string packetAsJson = packet.ToJson();
// ex 3. interface with a component from various angles.
IUnknown foo = MyObjectFactory.CreateFoo();
IVisualizer fooAsVisualizer = (IVisualizer)foo;
ILocator fooAsLocator = (ILocator)foo;
Thoughts for/against? Does this convention have a name?
Explanation / Answer
While generally it looks nice, and definitely better than the awful Hungarian style, where you put prefixes everywhere, even in obvious and self-descriptive cases, nevertheless I'd say write your programs so that there is no need to have the same piece of data in different formats within the same context.
For example:
function buildXmlRequest(name, email)
{
return toXml({'request':
{'name': name, 'email': email}});
}
function registerUser(name, email)
{
if (!sendRequest('http://allusers.com/xml',
buildXmlRequest(name, email)))
throw "User registration has failed";
}
There is a slight bend toward functional programming here: as you can see there are no variables at all. The benefit of this is that small stateless pieces of code are usually less error prone, especially in dynamic languages, where a mistyped variable name can go unnoticed for a long time.
Another advantage of this approach is data is not kept in memory when it is no longer needed. In your example you have packet AND packetAsJson at the same time in memory (and they can be huge!) whereas most likely you need only one at a time. In reference-counted environments (PHP) or languages with scope-based garbage collection (C++) bringing some functional style into your program may save you a lot of resources.
But again, yes, I'd use this style whereever the functional approach doesn't help much.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.