Unoffical empeg BBS

Quick Links: Empeg FAQ | RioCar.Org | Hijack | BigDisk Builder | jEmplode | emphatic
Repairs: Repairs

Topic Options
#113577 - 28/08/2002 19:31 I hate C and function pointers.
mschrag
pooh-bah

Registered: 09/09/2000
Posts: 2303
Loc: Richmond, VA
Give me interfaces any day.

Top
#113578 - 28/08/2002 21:02 Re: I hate C and function pointers. [Re: mschrag]
wfaulk
carpal tunnel

Registered: 25/12/2000
Posts: 16706
Loc: Raleigh, NC US
Ehh. Yer jes' lazy.
_________________________
Bitt Faulk

Top
#113579 - 28/08/2002 21:39 Re: I hate C and function pointers. [Re: wfaulk]
mschrag
pooh-bah

Registered: 09/09/2000
Posts: 2303
Loc: Richmond, VA
I look at this:

sp_rv
sp_generate_signature (sp_audio_info_t * song_audio_format,
sp_signature_t * signature,
guint (*get_pcm_data) (guint sample_offset,
guint num_samples,
guchar * output_buf,
void *user_data),
void *user_data)

and it makes my heart hurt. I'm so glad I only visit C world long enough to port it to Java world.

Then again, in the source to javazoom, a pure java MP3 decoder, there exists the line:

fs = (fs>32767.0f ? 32767.0f : (fs < -32767.0f ? -32767.0f : fs));

the ?: operator is the devil.

Top
#113580 - 28/08/2002 23:02 Re: I hate C and function pointers. [Re: mschrag]
Shonky
pooh-bah

Registered: 12/01/2002
Posts: 2009
Loc: Brisbane, Australia
In reply to:

the ?: operator is the devil




Don't say things like that . I love the ?: operator. Just make sure it's commented properly and there's no problem. I even have ?: nested 4 and 5 deep.
_________________________
Christian
#40104192 120Gb (no longer in my E36 M3, won't fit the E46 M3)

Top
#113581 - 28/08/2002 23:06 Re: I hate C and function pointers. [Re: mschrag]
wfaulk
carpal tunnel

Registered: 25/12/2000
Posts: 16706
Loc: Raleigh, NC US
I disagree about ?:. What is the devil is random magic numbers. He just couldn't make 32767 a constant, could he?

Of course, some indentation could be useful in that function declaration.
_________________________
Bitt Faulk

Top
#113582 - 29/08/2002 02:29 Re: I hate C and function pointers. [Re: mschrag]
peter
carpal tunnel

Registered: 13/07/2000
Posts: 4174
Loc: Cambridge, England
sp_rv
sp_generate_signature (sp_audio_info_t * song_audio_format,
sp_signature_t * signature,
guint (*get_pcm_data) (guint sample_offset,
guint num_samples,
guchar * output_buf,
void *user_data),
void *user_data)


C's insane type declarator syntax can usually be sanitised by introducing typedefs everywhere it gets hairy:

typedef guint (*get_pcm_data_fn) (guint sample_offset,
guint num_samples,
guchar * output_buf,
void *user_data);

sp_rv
sp_generate_signature (sp_audio_info_t * song_audio_format,
sp_signature_t * signature,
get_pcm_data_fn fn,
void *user_data)

but even so the typedef itself remains a bit icky.

Peter

Top
#113583 - 29/08/2002 05:25 Re: I hate C and function pointers. [Re: wfaulk]
mschrag
pooh-bah

Registered: 09/09/2000
Posts: 2303
Loc: Richmond, VA
I definitely agree about magic numbers ...

Top
#113584 - 29/08/2002 05:32 Re: I hate C and function pointers. [Re: Shonky]
mschrag
pooh-bah

Registered: 09/09/2000
Posts: 2303
Loc: Richmond, VA
I just always prefer clarity over brevity ... I suppose if properly commented, ?: can be OK (certainly a SINGLE ?: does not return true from my devil function -- which, curiously enough, is: boolean isDevil = (hasQuestionColon) ? ((hasMoreThanOne) ? : true : false) : false, I guess I just find if/else's much easier to follow, and much easier to go back and modify later, whereas the syntax of nested ?:'s is so fragile that you have to really work at changing it.... But that's just me

Top
#113585 - 29/08/2002 05:37 Re: I hate C and function pointers. [Re: peter]
mschrag
pooh-bah

Registered: 09/09/2000
Posts: 2303
Loc: Richmond, VA
Yeah -- It's definitely not fair to condemn a feature based on one of the more confusing examples of its use

Top
#113586 - 29/08/2002 05:58 Re: I hate C and function pointers. [Re: mschrag]
tms13
old hand

Registered: 30/07/2001
Posts: 1115
Loc: Lochcarron and Edinburgh
In reply to:

boolean isDevil = (hasQuestionColon) ? ((hasMoreThanOne) ? true : false) : false


Using conditional operator with only true or false as results is just gratuitous obfuscation

More than one on the same line is bad, but I have no problem with multiple ?: operators if they are laid out as if-elseif-elseif-else (which is great because you don't need loads of parens), or as a binary chop or some other recognisable structure, with a line for each test and each result, and indented sensibly.
_________________________
Toby Speight
030103016 (80GB Mk2a, blue)
030102806 (0GB Mk2a, blue)

Top
#113587 - 29/08/2002 06:08 Re: I hate C and function pointers. [Re: tms13]
mschrag
pooh-bah

Registered: 09/09/2000
Posts: 2303
Loc: Richmond, VA
In reply to:

Using conditional operator with only true or false as results is just gratuitous obfuscation




I couldn't think of anything funny to put there that wasn't just true/false I should have made it a recursive infinite loop, so instead of true, i had isThisADevil()... Hindsight.

Top
#113588 - 29/08/2002 06:28 Re: I hate C and function pointers. [Re: mschrag]
tms13
old hand

Registered: 30/07/2001
Posts: 1115
Loc: Lochcarron and Edinburgh
Sorry if I wasn't clear - I know you put true and false there as a joke, but I have seen (too much) real code that does that sort of thing.
_________________________
Toby Speight
030103016 (80GB Mk2a, blue)
030102806 (0GB Mk2a, blue)

Top
#113589 - 29/08/2002 07:50 Re: I hate C and function pointers. [Re: mschrag]
tman
carpal tunnel

Registered: 24/12/2001
Posts: 5528
Nah. You just have to wonder about some programmers when they do things like this:

(xyz ? func_1 : func_2)(arg1, xyz ? arg2 : arg3);

I've got a library for a expansion board that is full of things like that for some reason. They're actually a little more complicated but you get the general idea. Must have had an aversion to using if/else

- Trevor

Top
#113590 - 29/08/2002 08:20 Re: I hate C and function pointers. [Re: mschrag]
grgcombs
addict

Registered: 03/07/2001
Posts: 663
Loc: Dallas, TX
You and me both buddy. There's got to be a better way.

Then again, I've been known to use a goto here and there.

Greg
_________________________

Top
#113591 - 29/08/2002 11:29 Re: I hate C and function pointers. [Re: tman]
wfaulk
carpal tunnel

Registered: 25/12/2000
Posts: 16706
Loc: Raleigh, NC US
    (xyz ? func_1 : func_2)(arg1, xyz ? arg2 : arg3);
Hah! Took me a while to parse that. Pretty damn funny. I'm going to have to start using that all over the place, just to annoy folks.
_________________________
Bitt Faulk

Top