Data Dictionary 
Author Message
 Data Dictionary

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 <string.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);

Quote:
}

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

    Test(id, name, 99, 145);

Quote:
}

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{*filter*}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. :-)

--Kamilche



Mon, 04 Apr 2005 07:37:31 GMT  
 Data Dictionary
"Kamilche" wrote

Quote:
> 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 <string.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);
> }

> 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:

I fail to see what way this code is doing for what you seek for, so I must be
missing something. Supposedly the defines make up your 'data dictionary'. Do you
claim you use the items from the data dictionary without having to know the
actual type within the function Test? Then how did you now what format string to
provide to printf? You still have to know the actual type.

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

Using typedefs is superior to using defines for the purpose of introducing type
aliases. If you have shot yourself in the foot with typedefs (I am curious as
how that happened), I guess using defines will make you blow your head instead.

Quote:
> 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.

I have never had any problems coming up with a good variable name. Your method
does not guarantee useful names, especially when more than one variable of the
same item in the data dictionary have overlapping scope.

Quote:
> 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.

Can you eleborate?

Quote:
> 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.

--- aDs


Mon, 04 Apr 2005 13:36:01 GMT  
 Data Dictionary

Quote:

> 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.

Well, C is a typed language, so I think you're onto a loser here.

Quote:

> 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 <string.h>

> #define ID     __int64

Well, I don't need to tell you __int64 is not a standard type, do I? No?
Thought not. :-)

Quote:
> #define AGE    int
> #define HEIGHT int
> #define NAME   char *

Hmmmm. See below.

Quote:

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

> 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{*filter*}hole for a few minutes, and decided I was never
> gonna use them if there was an alternative. This method doesn't use
> typedefs.

Big problem with your NAME define. As you've used it here, it's fine,
but consider this:

NAME name = "Sandy", othername;

What type is othername? Yup, char. Not char *, but char.

Quote:

> 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.

But what if you want more than one variable of the same type?

Quote:

> 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.

You might want to consider getting a better editor, or having a look at
the grep utility.

<snip>

--

"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.*-*-*.com/ ~scs/C-faq/top.html
K&R answers, C books, etc: http://www.*-*-*.com/



Mon, 04 Apr 2005 17:13:19 GMT  
 Data Dictionary

Quote:

> #include <stdio.h>
> #include <string.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);
> }

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

>     Test(id, name, 99, 145);
> }

> Does anyone have any thoughts about using this method?

Yes. It's gonna hurt.

First of all, in a project of decent size, you're going to have oodles
of different variables, and therefore oodles of different #defines. This
may look nice and tidy for Hello, World, but for rewriting NetHack it's
going to be a great pain.
Second, sometimes you need to make several variables of one type. For
example, you want length1, height1, length2, and height2 all to be
unsigned ints. Now either you're going to make new typedefs for each
object, so you end up with LENGTH1 length1, HEIGHT1 height1, etc., and
lose the information that these are all the same kind of object; or
you're going to make LENGTH1 length1, height1, etc., and lose all
advantage of your system; or something in between, and confuse things
even more.
Third, you are going to want different objects called by the same name.
Never mind simple things like i and x; sometimes you just want
input_value to be a long int in one function, and an array of 20 chars
in another. And are you really going to refer to
DBASE_RELATIONS_CUSTOMER_ID dbase_relations_customer_id (seven{*filter*}
times!) in one function and DBASE_ITEMS_SOLD_ID dbase_items_sold_id
(which occurs no less than 39 times) in another, or do you want to be
able to use unsigned Id in one case, and char Id[10] in the other?

IOW, this is typically the kind of idea that sounds good in theory, but
turns out not to be practical. Stick with typedef's for different
_kinds_ of objects, not one for each separate object.

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

And is the worse for it. This is exactly what typedefs are for. See
Richard's post.

Richard



Mon, 04 Apr 2005 18:51:29 GMT  
 Data Dictionary

Quote:

> ... You might want to consider getting a better editor, or having a look at
> the grep utility.

Not too useful, eh? Hm. I'm almost tempted to write a pseudocode-to-C
translator then.

I must confess, the overhead of maintaining similar types for a datum
in the function prototype, member, function arguments, etc., is
getting tedious. This would give me 'change once' sort of behavior
when I change the types.

I wish C had named parameters, as well. Not many languages implement
that, but I miss it. You'd think they would have caught on in more
languages by now, considering how many programmers create bugs by
passing parameters in the wrong order. However, my first attempt at
recreating that with C, showed (possibly) why they're not popular yet.
Mein Gott! The overhead, the humanity! >:-D

--Kamilche



Mon, 04 Apr 2005 23:04:38 GMT  
 Data Dictionary

Quote:

> I wish C had named parameters, as well. Not many languages implement
> that, but I miss it. You'd think they would have caught on in more
> languages by now, considering how many programmers create bugs by
> passing parameters in the wrong order. However, my first attempt at
> recreating that with C, showed (possibly) why they're not popular yet.

And many IDEs can be very helpful in providing online help with the
function prototypes.  VC++, for instance, has Intellisense.

--

|_ http://www.Sonnack.com/ ___________________| Call: 1-800-DEV-NULL  |
|_____________________________________________|_______________________|

Opinions expressed herein are my own and may not represent those of my employer.



Mon, 04 Apr 2005 23:37:35 GMT  
 Data Dictionary

Quote:
> I must confess, the overhead of maintaining similar types for a datum
> in the function prototype, member, function arguments, etc., is
> getting tedious. This would give me 'change once' sort of behavior
> when I change the types.

> I wish C had named parameters, as well. Not many languages implement
> that, but I miss it. You'd think they would have caught on in more
> languages by now, considering how many programmers create bugs by
> passing parameters in the wrong order. However, my first attempt at
> recreating that with C, showed (possibly) why they're not popular yet.
> Mein Gott! The overhead, the humanity! >:-D

Simple typedefs are, well, simple..
typedef long int ID; /*that's all there is to it*/

But, I see you yearning for more organization. The struct typdef is the
solution you seek. Try out this sample program based on your original
effort:

#include <stdio.h>

typedef struct
{
    int id;
    char name[20];
    int age;
    int height;

Quote:
} Record;

void test(Record *record)
{
    printf("ID %d, Name %s, Age %d, Height %d\n", record->id, record->name,
record->age, record->height);

Quote:
}

int main(void)
{
    Record my_record[5];
    int i;

    printf("Enter 5 records: ID name age height\n");

    for (i = 0; i < 5; i++)
    {
        scanf("%d %s %d %d", &my_record[i].id, &my_record[i].name,
&my_record[i].age, &my_record[i].height);
    }

    for (i = 0; i < 5; i++)
    {
        test(&my_record[i]);
    }

    return 0;

Quote:
}

And here is some sample output:
Enter 5 records: ID name age height
45326 Bob 40 170
52367 Joe 32 165
21346 Sue 53 160
13453 Rex 60 180
15234 Jon 29 190
ID 45326, Name Bob, Age 40, Height 170
ID 52367, Name Joe, Age 32, Height 165
ID 21346, Name Sue, Age 53, Height 160
ID 13453, Name Rex, Age 60, Height 180
ID 15234, Name Jon, Age 29, Height 190

If used correctly, structs can be an extremely powerful organizational tool.

Based on you comments, you might be seeking even more organization power. At
the risk of being {*filter*}ed by the ansi c group, I'd highly recommend you look
into C++ instead.

Webb



Tue, 05 Apr 2005 01:17:49 GMT  
 Data Dictionary

Quote:

> I wish C had named parameters, as well. Not many languages implement
> that, but I miss it. You'd think they would have caught on in more
> languages by now, considering how many programmers create bugs by
> passing parameters in the wrong order. However, my first attempt at
> recreating that with C, showed (possibly) why they're not popular yet.
> Mein Gott! The overhead, the humanity! >:-D

For a compiled language, I fail to see how named parameter arguments
would add any overhead to the executable.  That it adds overhead to
a C program to implement such a thing, doesn't mean it would for a
language that supported named arguments (for instance, Ada95).  The
capability seems more useful when function names can be overloaded
and arguments can have default values.


Tue, 05 Apr 2005 12:06:16 GMT  
 Data Dictionary

Quote:

> 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.

[snip]

Quote:
> #define ID     __int64
> #define AGE    int
> #define HEIGHT int
> #define NAME   char *

> void Test(ID id, NAME name, AGE age, HEIGHT height)

[snip]

Quote:
> Does anyone have any thoughts about using this method?

This is a TERRIBLE idea. The only reason *you* are able to make any
sense out of ID, AGE, HEIGHT, etc without needing to "remember or look
up" the (true) types of the variables is because you *know* what the
types are. If I give you a prototype like this:

    int download (LCID, GDF, MODE, HOST, USER, PASSWD);

how can you make any sense of it? If I write this instead,

    int download (ulong lcid, char* gdf, int mode, ulong ipaddr, char
*usr, char *passwd);

then things start to make sense. The above example is actually based on
some code in a system I am working with, and I *always* have to look up
the actual types hiding behind the typedefs and #defines because I
actually do need to know them in order to use the function.

Quote:
> Here's a couple of my thoughts:

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

In this case, there is no difference between using #define and using
typedef except that typedef better expresses your intention and has some
"lexical substance"; #define merely does string replacement.

Quote:
> 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.

This might seem easy until you try to assign a value to a variable like

    SSN    ssn;    /* social security number */

and realize that you don't know whether it's an integer or a string.

Quote:
> 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.

This also seems like a good idea, but I don't think you can justify (2)
with (3). Good software architecture skills, forsight, and sed should
overcome most of these types of problems.

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

You can avoid this by using the convention

    Type type;

rather than

    TYPE type;

reserving all upper-case identifiers for macros.

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

You have only briefly mentioned table-lookup, but have not really made a
proposal for how such a thing would work. AFAICT, the issue is not
identifying a variable with its type alias as in (2), but knowing what
values to put into a variable for which the type is opaque.

        david

--
...the use of keys+commands required a higher level of mental planning
to organise the interaction, which apparently obscures the perception
of the passage of time...
        -- forsyth on Tognazzini's "Tog on Interface"



Tue, 05 Apr 2005 23:08:57 GMT  
 Data Dictionary

Quote:

> Simple typedefs are, well, simple..
> typedef long int ID; /*that's all there is to it*/

They are simple, I grant you that, but name space collisions were
happening for me. If I declare a typedef for use of one module only,
it's visible to all modules, whether I need it to be or not. So I've
learned to just use 'struct whatever' instead, so I can keep it
private.

If typedef's could be limited to a single module, I'd use them 20 ways
here from Sunday.

--Kamilche



Tue, 05 Apr 2005 23:21:30 GMT  
 Data Dictionary

Quote:

>> Simple typedefs are, well, simple..
>> typedef long int ID; /*that's all there is to it*/

> They are simple, I grant you that, but name space collisions were
> happening for me. If I declare a typedef for use of one module only,
> it's visible to all modules, whether I need it to be or not. So I've
> learned to just use 'struct whatever' instead, so I can keep it
> private.

I don't get you there. You mean you had to introduce the typedef
into the whole program not just the source files it was needed
in? I don't know about such an requirement and the I really
thought that typedefs had the same scope conditions as normal
identifiers have.

Quote:

> If typedef's could be limited to a single module, I'd use them 20 ways
> here from Sunday.

What is a module in your context?

--

"LISP  is worth learning for  the profound enlightenment  experience
you will have when you finally get it; that experience will make you
a better programmer for the rest of your days."   -- Eric S. Raymond



Tue, 05 Apr 2005 23:42:51 GMT  
 Data Dictionary

Quote:

> If typedef's could be limited to a single module, I'd use them 20 ways
> here from Sunday.

Can't they?  Why can't you define them at the top of the only module
that uses them?  If you're like Richard, and "module" means more than
one file, you can always make private "module" header files.

--

|_ http://www.Sonnack.com/ ___________________| Call: 1-800-DEV-NULL  |
|_____________________________________________|_______________________|

Opinions expressed herein are my own and may not represent those of my employer.



Tue, 05 Apr 2005 23:55:28 GMT  
 Data Dictionary

Quote:


> > Simple typedefs are, well, simple..
> > typedef long int ID; /*that's all there is to it*/

> They are simple, I grant you that, but name space collisions were
> happening for me. If I declare a typedef for use of one module
> only, it's visible to all modules, whether I need it to be or not.
> So I've learned to just use 'struct whatever' instead, so I can
> keep it private.

> If typedef's could be limited to a single module, I'd use them 20
> ways here from Sunday.

Of course they can.  You only put things in the .h file that you
want to publish to other modules, and you only include .h files
when you need to know about that module.

--

   Available for consulting/temporary embedded and systems.
   <http://cbfalconer.home.att.net>  USE worldnet address!



Wed, 06 Apr 2005 01:46:31 GMT  
 Data Dictionary

Quote:


> > If typedef's could be limited to a single module, I'd use them 20 ways
> > here from Sunday.

> Can't they?  Why can't you define them at the top of the only module
> that uses them?  If you're like Richard, and "module" means more than
> one file, you can always make private "module" header files.

And that's precisely what I do. At least, that's the model I'm currently
using in the set of comms routines I'm writing - a sort of
medium-thickness wrapper around a bunch of Internet protocols.

The private information in the module resides in a struct, to which the
user of the module can define a pointer. That pointer type is defined in
a public header, but the struct definition itself is in a private
header; thus, it can contain any information it likes without that
information being directly accessible by the user-programmer. The module
returns a pointer to a new instance of the struct in much the same way
that fopen returns a FILE *. Unlike the FILE * situation, however, the
user can't see the struct definition by looking in the header, because
he can't see the private header - because I won't be shipping it. :-)
All that will ship is the library file and the public header.

It seems to work rather nicely.

--

"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton



Wed, 06 Apr 2005 00:42:21 GMT  
 Data Dictionary

Quote:

> Can't they?  Why can't you define them at the top of the only module
> that uses them?  If you're like Richard, and "module" means more than
> one file, you can always make private "module" header files.

Huh. After reading Richard's comment, I did a test, and discovered
they DID have module scope. Leftover n00b misconception, I guess.
Thanks for clearing that up!

It looks like namespace collision is still possible, though, unless
I'm misunderstanding the documentation, which says:

"Names declared using typedef occupy the same name space as other
identifiers (except statement labels). Therefore, they cannot use the
same identifier as a previously declared name, except in a class-type
declaration. "



Wed, 06 Apr 2005 07:42:52 GMT  
 
 [ 25 post ]  Go to page: [1] [2]

 Relevant Pages 

1. Data Dictionary and Code Generator

2. Data dictionary+DFD

3. Data dictionaries

4. Data dictionary (symbol xref) generator, results

5. looking for a data dictionary

6. Query: "Data Dictionary" Facilities?

7. FREE CODE!: Dictionary template (works like VBS Dictionary)

8. dictionary routines and/or dictionary wanted.

9. FREE CODE!: Dictionary template (works like VBS Dictionary)

10. Accessing Dictionary Data type exposed in COM object

11. Creating string from dictionary information

12. Custom dictionary usage via automation

 

 
Powered by phpBB® Forum Software