Data dictionaries

K

Kami

I spent ALL day playing with named parameters instead of positional
parameters to functions, and ended up throwing it all away. C just
doesn't make that easy or clean.

But the basic gist of what I want, is this: I would like to create a
'data dictionary' that stores the data name and type. Whenever I need
a new function parameter or local variable, and it's manipulating an
item that is described in the data dictionary, I want to be able to
declare and use it without thought... I don't want to have to remember
or look up what type it is.

I have some sample code to show you. It's pretty short, which is
funny, considering all the complicated stuff I threw away earlier
today. >:-D But simple is good - maybe that means it will be easier to
use.

#include<stdio.h>
#include<memory.h>

#define ID __int64
#define AGE int
#define HEIGHT int
#define NAME char*

void Test(ID id, NAME name, AGE age, HEIGHT height)
{
printf("ID %I64d, Name %s, Age %d, Height %d\n", id, name, age,
height);

}

void main()
{
ID id = 64;
NAME name = "Sandy";

Test(id, name, 99, 145);

}

Does anyone have any thoughts about using this method?
Here's a couple of my thoughts:

1. I hate typedefs. I've shot myself in the foot once with them, stared
at the gory hole for a few minutes, and decided I was never gonna use
them if there was an alternative. This method doesn't use typedefs.

2. The decision on what to call a variable becomes easy. Look up the
variable in the data dictionary, use the uppercase version for the type,
the lowercase version for the variable.

3. It will help in the declaration of structures, as well... no more
hunting around for all references to a member (parameters, members,
etc.) when you change the type.

4. There is a possibility of collision with other uppercase constants
and such, but it doesn't look to be too difficult.

5. Being a #define instead of a table lookup, it doesn't slow down the
code or add to the memory requirements.

If there's something wrong with it, let me know!
If it's all good, yay. :)

--Kami
 
E

Eric Sosman

Kami said:
I spent ALL day playing with named parameters instead of positional
parameters to functions, and ended up throwing it all away. C just
doesn't make that easy or clean.

No. (Shrug.)
But the basic gist of what I want, is this: I would like to create a
'data dictionary' that stores the data name and type. Whenever I need
a new function parameter or local variable, and it's manipulating an
item that is described in the data dictionary, I want to be able to
declare and use it without thought... I don't want to have to remember
or look up what type it is.

I have some sample code to show you. It's pretty short, which is
funny, considering all the complicated stuff I threw away earlier
today. >:-D But simple is good - maybe that means it will be easier to
use.

#include<stdio.h>
#include<memory.h>

What's this? Never mind; it doesn't seem to be used.
#define ID __int64
#define AGE int
#define HEIGHT int
#define NAME char*

void Test(ID id, NAME name, AGE age, HEIGHT height)
{
printf("ID %I64d, Name %s, Age %d, Height %d\n", id, name, age,
height);

I don't see any evidence here of anything I'd call a
"data dictionary." In fact, you haven't even been relieved
of the burden of remembering things about the types: How
did you know to use "%s" for name and "%d" for age and height?
(And what's that "I" modifier about? Some non-standard thing
related to the non-standard __int64, I'd guess -- and if
so, yet another thing the "data dictionary" didn't supply.)
}

void main()

When you threw away all that "complicated stuff," you
forgot to throw away this line.
{
ID id = 64;
NAME name = "Sandy";

Test(id, name, 99, 145);

}

Does anyone have any thoughts about using this method?
Here's a couple of my thoughts:

1. I hate typedefs. I've shot myself in the foot once with them, stared
at the gory hole for a few minutes, and decided I was never gonna use
them if there was an alternative. This method doesn't use typedefs.

This isn't a "thought," it's a statement about your
personal preferences. It has about as much relevance as
would "I hate parsnips."
2. The decision on what to call a variable becomes easy. Look up the
variable in the data dictionary, use the uppercase version for the type,
the lowercase version for the variable.

And if you need two such variables ...? Well, the obvious
thing would be to add a "decoration," as in

NAME myname, yourname;

.... which accomplishes two things: It shows how to modify your
naming convention in a clear and useful way, and it introduces
an error that may make you think again about typedef.
3. It will help in the declaration of structures, as well... no more
hunting around for all references to a member (parameters, members,
etc.) when you change the type.

I don't understand how using macros will help or hinder
the task of finding occurrences. Also, "when you change the
type" has always struck me as letting the tail wag the dog,
in this and in other contexts. C doesn't support a programming
style in which you suddenly say "I think I'll change NAME from
a char* to a long double, hey presto! it's done!" These much-
posited type changes are, in my experience, infrequent.
4. There is a possibility of collision with other uppercase constants
and such, but it doesn't look to be too difficult.

C offers very little to help avoid name collisions in
any case (pun intended).
5. Being a #define instead of a table lookup, it doesn't slow down the
code or add to the memory requirements.

I guess this is another reference to the "data dictionary,"
but I still don't see how any such thing enters the picture.
If there's something wrong with it, let me know!
If it's all good, yay. :)

My guess is that you're trying to do something but that
I'm unable to make sense of your explanation of it.
 
J

John Bode

I spent ALL day playing with named parameters instead of positional
parameters to functions, and ended up throwing it all away. C just
doesn't make that easy or clean.

But the basic gist of what I want, is this: I would like to create a
'data dictionary' that stores the data name and type. Whenever I need
a new function parameter or local variable, and it's manipulating an
item that is described in the data dictionary, I want to be able to
declare and use it without thought... I don't want to have to remember
or look up what type it is.

I have some sample code to show you. It's pretty short, which is
funny, considering all the complicated stuff I threw away earlier
today. >:-D But simple is good - maybe that means it will be easier to
use.

#include<stdio.h>
#include<memory.h>

#define ID __int64
#define AGE int
#define HEIGHT int

Okay so far.
#define NAME char*

This will bite you in the ass if you do something like this:

NAME name1, name2;

You will have the same problem with any other pointer type. Also,
char* is not necessarily synonymous with "string"; abstracting away
details for string types is going to require a bit more than just a
typedef or a macro.
void Test(ID id, NAME name, AGE age, HEIGHT height)
{
printf("ID %I64d, Name %s, Age %d, Height %d\n", id, name, age,
height);

If you really want to abstract away the details of a data type,
perhaps you should define a toString() function for each one, instead
of having to remember which conversion specifier to use.
}

void main()

You mean

int main(void)
{
ID id = 64;
NAME name = "Sandy";

Test(id, name, 99, 145);

}

Does anyone have any thoughts about using this method?
Here's a couple of my thoughts:

1. I hate typedefs. I've shot myself in the foot once with them, stared
at the gory hole for a few minutes, and decided I was never gonna use
them if there was an alternative. This method doesn't use typedefs.

I pointed out one case above where using a macro will fail. Macros
also fail for relatively complex types (such as pointers to arrays of
pointers to functions). In general, typedefs really are the best way
to go.
2. The decision on what to call a variable becomes easy. Look up the
variable in the data dictionary, use the uppercase version for the type,
the lowercase version for the variable.

Surely you're not going to create a separate type for each variable?
3. It will help in the declaration of structures, as well... no more
hunting around for all references to a member (parameters, members,
etc.) when you change the type.

I can see cases where the underlying representation may have to change
if moving between different architectures, but you shouldn't need to
change data types that often.
4. There is a possibility of collision with other uppercase constants
and such, but it doesn't look to be too difficult.

5. Being a #define instead of a table lookup, it doesn't slow down the
code or add to the memory requirements.

Macros are not robust, however.
If there's something wrong with it, let me know!
If it's all good, yay. :)

--Kami

If I were going to try to abstract away data type details, I wouldn't
stop with simply aliasing the data types, especially for text types.
I'd at least provide a library to do things like assignments,
conversions (including string representations), etc. The thing is,
there's no good way to do this that's robust *and* simple *and* fast.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,252
Latest member
MeredithPl

Latest Threads

Top