Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Data dictionaries

Reply
Thread Tools

Data dictionaries

 
 
Kami
Guest
Posts: n/a
 
      03-05-2008
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. > 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
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      03-05-2008
Kami wrote:
> 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. > 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.

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)lid
 
Reply With Quote
 
 
 
 
John Bode
Guest
Posts: n/a
 
      03-05-2008
On Mar 5, 5:47 am, Kami <(E-Mail Removed)> wrote:
> 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. > 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Constructing JSON data structures from non-string key python dictionaries saikari78 Python 10 11-22-2012 05:05 PM
Re: pickle.load() on an dictionary of dictionaries doesn't loadfull data structure on first call Albert Hopkins Python 0 02-23-2009 01:10 AM
updating dictionaries from/to dictionaries Brandon Python 12 08-15-2008 12:35 AM
Pickling dictionaries containing dictionaries: failing,recursion-style! lysdexia Python 6 12-02-2007 12:03 AM
TB1.5: Dictionaries =?windows-1252?Q?won=92t_install?= Hendrik Maryns Firefox 10 01-19-2006 04:36 PM



Advertisments