E
evanevankan2
Lets say I have an example structure like this
struct day {
/* Data goes here */
};
and in my program I work with an array of structs of that type.
Example:
int main(void)
{
struct day *day;
int count;
/* Allocation of struct goes here. */
do_stuff_on_array(day, count);
do_stuff_on_day(day);
return 0;
}
void do_stuff_on_array(struct day *day, int count)
{
int i;
for (i = 0; i < count; i++) {
/* Do stuff on day */
}
}
void do_stuff_on_array(struct day *day)
{
/* Do stuff on *day */
}
Now my question is just about the names. I always try to make up
the best describing names for my variables and structs, but I have
problems on which conventions I should use. I ended up with
the same name for both the variable and struct, that is 'day'.
So I write: struct day *day;
I know it works perfectly well because of different namespaces.
But is that a bad idea, would you say it is confusing to have the
same name?
Then the next question. The first parameter of the two functions
are idential, and I choose the same naming here too:
struct day *day
But the functions work on different things, the first one on
a whole array while the second only on an element.
Should I use some different naming here for the two functions
to indicate that they don't handle the argument in the same way,
in order to not confuse people who read my code?
Grateful for any opinions!
Thanks
struct day {
/* Data goes here */
};
and in my program I work with an array of structs of that type.
Example:
int main(void)
{
struct day *day;
int count;
/* Allocation of struct goes here. */
do_stuff_on_array(day, count);
do_stuff_on_day(day);
return 0;
}
void do_stuff_on_array(struct day *day, int count)
{
int i;
for (i = 0; i < count; i++) {
/* Do stuff on day */
}
}
void do_stuff_on_array(struct day *day)
{
/* Do stuff on *day */
}
Now my question is just about the names. I always try to make up
the best describing names for my variables and structs, but I have
problems on which conventions I should use. I ended up with
the same name for both the variable and struct, that is 'day'.
So I write: struct day *day;
I know it works perfectly well because of different namespaces.
But is that a bad idea, would you say it is confusing to have the
same name?
Then the next question. The first parameter of the two functions
are idential, and I choose the same naming here too:
struct day *day
But the functions work on different things, the first one on
a whole array while the second only on an element.
Should I use some different naming here for the two functions
to indicate that they don't handle the argument in the same way,
in order to not confuse people who read my code?
Grateful for any opinions!
Thanks