Coding style survey

  • Thread starter Papadopoulos Giannis
  • Start date
C

CBFalconer

Arthur J. O'Dwyer said:
To what is this a "counter-example"? I see allusions to "private"
data, user-defined types, and encapsulation, but the part of my
post that you quoted wasn't talking about such high-level stuff.
I just said that if you wanted to make a const struct, then initializing
it with the value returned by a function would be an excellent idea.

So, if you're disagreeing with something I wrote, could you say
so? Or if you're agreeing with something I wrote, could you say
that too? Or if you're just describing hashlib, could you start a
new thread? ;-)

I was disagreeing with the first sentence of the portion I quoted,
which I read as denigrating returning structures from functions.
 
A

Arthur J. O'Dwyer

I was disagreeing with the first sentence of the portion I quoted,
which I read as denigrating returning structures from functions.

Okay. Well, I shall continue to religiously avoid returning structs
by value from functions. In the case you quote, if I were not being
utterly paranoid, I would simply make 'hshstatus' return a pointer
to the "real" record, const-qualified. :)

const struct hshstats status;
status = hshstatus(h);

If I were being paranoid, I might make the user pass in his buffer
explicitly [despite NRV optimization's being common, as E.R. Tisdale
pointed out a while back]. I just like being explicit about these
things, I guess:

struct hshstats status;
hshstatus(h, &status);

Or I might see your allusion to "FILE *" above, and make an exact
analogue of that scheme, although that would be overkill for most
applications:

struct hshstats *status;
status = hshgetstatus(h);
[...]
hshkillstatus(status);

Anyway, there are plenty of ways to do it. Your hashlib is an
excellent example of how somebody can use return-struct-by-value to
do something useful, but it's not gonna convert me. ;-D
[Why don't I like returning structs? Mostly because it's not as
transparent as most of the C language that I do use -- it's not
clear whether it's going to be fast or slow unless you know what
sorts of optimization your compiler does with returning structs.]

-Arthur
 
C

CBFalconer

Arthur J. O'Dwyer said:
.... snip ...

Okay. Well, I shall continue to religiously avoid returning structs
by value from functions. In the case you quote, if I were not being
utterly paranoid, I would simply make 'hshstatus' return a pointer
to the "real" record, const-qualified. :)

const struct hshstats status;
status = hshstatus(h);
.... snip ...

Anyway, there are plenty of ways to do it. Your hashlib is an
excellent example of how somebody can use return-struct-by-value to
do something useful, but it's not gonna convert me. ;-D

Most such techniques are not idiot or enemy proof. Once the user
can lay his hands on a pointer to internal data he can muck with
it. The const qualifier is easily overridden.
 
E

Eirik WS

Papadopoulos said:
Which do you think is best?

1.
a) type* p;
b) type *p;
b

2.
a) return (var);
b) return(var);
c) return var;
c

3.
a) return (ptr->var);
b) return(ptr->var);
c) return ptr->var;

c
4.
a) return (foo(ptr->var));
b) return(foo(ptr->var));
c) return foo(ptr->var);

c
5.
a) a = (b+c);
b) a=(b+c);
c) a = b+c;
d) a=b+c;

a = b + c;
6.
a)
type foo(type arg1, type arg2, ...) {
declarations;
code;
return;
}
b)
type foo(type arg1, type arg2, ...) {
declarations;

code;

return;
}
c)
type foo(type arg1, type arg2, ...)
{
declarations;
code;
return;
}
d)
type foo(type arg1, type arg2, ...)
{
declarations;

code;

return;
}
e)
type foo(type arg1, type arg2, ...)
{
declarations;
code;
return;
}
f)
type foo(type arg1, type arg2, ...)
{
declarations;

code;

return;
}

a
 

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

No members online now.

Forum statistics

Threads
474,139
Messages
2,570,806
Members
47,353
Latest member
TamiPutnam

Latest Threads

Top