Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Style question

Reply
Thread Tools

Style question

 
 
Nick
Guest
Posts: n/a
 
      10-19-2010
I'm not after a definitive answer here - I know there aren't any. But
I'm curious to know how people feel about something.

A few days ago I found myself writing something like this:

find_item(name)->identifier

Where find_item looks up a name in a data structure and returns a
pointer to the node (a struct). The "identifier" is an integer.

I was, in fact, using this as a paramater to another function, that took
an integer identifier and did some more stuff with it.

There's a bit of me that thinks this is really neat, and another bit of
me that found the resulting three-line statement horrible.

In the end I realised I needed different items at different times, so
introduced a temporary variable. But how do others feel about this
idea of getting a struct pointer back from a function and extracting a
member directly from it?
--
Online waterways route planner | http://canalplan.eu
Plan trips, see photos, check facilities | http://canalplan.org.uk
 
Reply With Quote
 
 
 
 
Felix Palmen
Guest
Posts: n/a
 
      10-19-2010
* Nick <(E-Mail Removed)>:
> I'm not after a definitive answer here - I know there aren't any. But
> I'm curious to know how people feel about something.
>
> A few days ago I found myself writing something like this:
>
> find_item(name)->identifier
>
> Where find_item looks up a name in a data structure and returns a
> pointer to the node (a struct). The "identifier" is an integer.
>
> I was, in fact, using this as a paramater to another function, that took
> an integer identifier and did some more stuff with it.
>
> There's a bit of me that thinks this is really neat, and another bit of
> me that found the resulting three-line statement horrible.
>
> In the end I realised I needed different items at different times, so
> introduced a temporary variable. But how do others feel about this
> idea of getting a struct pointer back from a function and extracting a
> member directly from it?


I'd put it a little more generally and discuss chained and nested
function calls. And I know this tradeoff from my own experience:

Given proper names and logical nesting/chaining that really describes
the intent of the statement, it just looks quite elegant.

The downside is IMO twofold:

- As your program evolves, you may need to add some functionality in
your already complex statement, missing the point where it turns from
being just obvious, nice and clean into some incomprehensible monster.
- When debugging, you're missing the opportunity to easily single-step
over each involved function, checking the intermediate results.

So, most of the time I'd vote against to much chaining/nesting of
function calls.

Regards,
Felix

--
Felix Palmen (Zirias) + [PGP] Felix Palmen <(E-Mail Removed)>
web: http://palmen-it.de/ | http://palmen-it.de/pub.txt
my open source projects: | Fingerprint: ED9B 62D0 BE39 32F9 2488
http://palmen-it.de/?pg=pro + 5D0C 8177 9D80 5ECF F683
 
Reply With Quote
 
 
 
 
Seebs
Guest
Posts: n/a
 
      10-19-2010
On 2010-10-19, Nick <(E-Mail Removed)> wrote:
> In the end I realised I needed different items at different times, so
> introduced a temporary variable. But how do others feel about this
> idea of getting a struct pointer back from a function and extracting a
> member directly from it?


Is it remotely, ever, conceivable that the function will return NULL?

-s
--
Copyright 2010, all wrongs reversed. Peter Seebach / http://www.velocityreviews.com/forums/(E-Mail Removed)
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.
 
Reply With Quote
 
James Dow Allen
Guest
Posts: n/a
 
      10-19-2010
Nick <(E-Mail Removed)> might have writ, in
news:(E-Mail Removed):

> find_item(name)->identifier


Perhaps even more problematic is
copy_item(name).identifier

There are some restrictions on such constructions;
perhaps someone can post an "executive summary."

James
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      10-19-2010
On 10/19/10 09:30 PM, James Dow Allen wrote:
> Nick<(E-Mail Removed)> might have writ, in
> news:(E-Mail Removed):
>
>> find_item(name)->identifier

>
> Perhaps even more problematic is
> copy_item(name).identifier


Probably less problematic, copy_item can't return null.

--
Ian Collins
 
Reply With Quote
 
Tom St Denis
Guest
Posts: n/a
 
      10-19-2010
On Oct 19, 2:28*am, Nick <(E-Mail Removed)> wrote:
> In the end I realised I needed different items at different times, so
> introduced a temporary variable. *But how do others feel about this
> idea of getting a struct pointer back from a function and extracting a
> member directly from it?


I'd avoid that solely on the premise that function could potentially
fail [return NULL] and thus would segfault your application.

The typical way I would do that is

item = find_item(name);
if (item == NULL) { return error_code_of_some_sort; }
// ... use item

If it's a short usage you might get away with

if ((item = find_item(name)) != NULL) {
// use item
} else {
// return error
}

but if you subsequently "find" another item the code can get nested
crazy deep and weird.

Tom
 
Reply With Quote
 
ImpalerCore
Guest
Posts: n/a
 
      10-19-2010
On Oct 19, 2:28*am, Nick <(E-Mail Removed)> wrote:
> I'm not after a definitive answer here - I know there aren't any. *But
> I'm curious to know how people feel about something.
>
> A few days ago I found myself writing something like this:
>
> find_item(name)->identifier
>
> Where find_item looks up a name in a data structure and returns a
> pointer to the node (a struct). *The "identifier" is an integer.
>
> I was, in fact, using this as a paramater to another function, that took
> an integer identifier and did some more stuff with it.
>
> There's a bit of me that thinks this is really neat, and another bit of
> me that found the resulting three-line statement horrible.
>
> In the end I realised I needed different items at different times, so
> introduced a temporary variable. *But how do others feel about this
> idea of getting a struct pointer back from a function and extracting a
> member directly from it?


In general, any function that returns a struct pointer with a 'find'
or 'search' in the function name is likely to return NULL eventually,
as NULL typically represents the 'not found' return value semantic of
the lookup. To me, this is a bomb with a relatively short fuse. If
the maintenance programmer that follows you finds a bug as a result of
'find_item(name)->identifier', I doubt your cleverness will be
appreciated.

Best regards,
John D.
 
Reply With Quote
 
Tom St Denis
Guest
Posts: n/a
 
      10-19-2010
On Oct 19, 4:46*am, Ian Collins <(E-Mail Removed)> wrote:
> On 10/19/10 09:30 PM, James Dow Allen wrote:
>
> > Nick<(E-Mail Removed)> *might have writ, in
> >news:(E-Mail Removed):

>
> >> find_item(name)->identifier

>
> > Perhaps even more problematic is
> > * * *copy_item(name).identifier

>
> Probably less problematic, copy_item can't return null.


Nah, it's just a performance hazard and what you're referencing could
be NULL e.g.

printf("name is %s\n", find_item(ID).name);

Brilliant! Combining two problems into one!
 
Reply With Quote
 
Willem
Guest
Posts: n/a
 
      10-19-2010
Kenneth Brody wrote:
) On the other hand, sometimes the resulting code of directly using the
) returned pointer just looks awkward.
)
) foo(bar(plugh)->item[n].addr)->name = "foo";

This is only awkward if you're not used to functional programming.


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
Reply With Quote
 
Nick
Guest
Posts: n/a
 
      10-19-2010
Seebs <(E-Mail Removed)> writes:

> On 2010-10-19, Nick <(E-Mail Removed)> wrote:
>> In the end I realised I needed different items at different times, so
>> introduced a temporary variable. But how do others feel about this
>> idea of getting a struct pointer back from a function and extracting a
>> member directly from it?

>
> Is it remotely, ever, conceivable that the function will return NULL?


Not in this case. But I agree that that is a perfectly natural thing
for a search function to do, and completely breaks this approach.
--
Online waterways route planner | http://canalplan.eu
Plan trips, see photos, check facilities | http://canalplan.org.uk
 
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
DataGrid header style inconsistent with sortable column style cedoucette@alum.rpi.edu ASP .Net 0 10-14-2005 12:13 AM
All style tags after the first 30 style tags on an HTML page are not applied in Internet Explorer Rob Nicholson ASP .Net 3 05-28-2005 03:11 PM
Need help with Style conversion from Style object to Style key/value collection. Ken Varn ASP .Net Building Controls 0 04-26-2004 07:06 PM
Javascript Style Switcher that remebers current site style in use Hardeep Rakhra HTML 8 01-15-2004 08:00 PM
Style sheets, include one style within another (not inheritance) foldface@yahoo.co.uk HTML 1 11-24-2003 01:37 PM



Advertisments