Live data from Hacker News

The Worst API Ever Made?

mollyrocket.com

61–70 of 82 posts

Re: The Worst API Ever Made?

#61

Earlier quoted context omitted.

Dude worked at RAD, so I'm willing to bet he's got some experience. In your LPSTR example, that could've been to silence compiler warnings about doing pointer arithmetic. EDIT: From bio: The most significant project I’ve created to date has been The Granny Animation SDK, a complete animation pipeline system that I first shipped in 1999 and which, 15 years later, still is in active use at many top-tier game studios. "…

You don't ever need to cast from char * to char * . Period. In 16 bit land the "L" meant something but that hasn't meant anything since long before the API he is calling existed.

Right, but you're talking past me. Look at the code:

  (LPSTR)((char*)pSessionProperties + pSessionProperties->LoggerNameOffset)
He's casting pSessionProperties to a char* so that he can do the arithmetic on it--otherwise, the compiler would assume "Oh, golly, I should increment the pSessionProperties pointer by LoggerNameOffset times sizeof(SessionProperties)".

He has to make that conversion in order to do byte offsetting correctly. He then casts that back to what it wants (an LPSTR), to match the required argument type on the function.

It's completely reasonable code, so stop complaining about it as though it weren't.

Re: The Worst API Ever Made?

#62

Earlier quoted context omitted.

You don't ever need to cast from char * to char * . Period. In 16 bit land the "L" meant something but that hasn't meant anything since long before the API he is calling existed.

Right, but you're talking past me. Look at the code: (LPSTR)((char*)pSessionProperties + pSessionProperties->LoggerNameOffset) He's casting pSessionProperties to a char* so that he can do the arithmetic on it--otherwise, the compiler would assume "Oh, golly, I should increment the pSessionProperties pointer by LoggerNameOffset times sizeof(SessionProperties)". He has to make that conversion in order to do byte offset…

I think the complaint was about the cast to LPSTR at the end.

    typedef char* PSTR, *LPSTR;
After the addition, you've already got a char * - casting to a char * again by another name is unnecessary.

Re: The Worst API Ever Made?

#63

Earlier quoted context omitted.

Right, but you're talking past me. Look at the code: (LPSTR)((char*)pSessionProperties + pSessionProperties->LoggerNameOffset) He's casting pSessionProperties to a char* so that he can do the arithmetic on it--otherwise, the compiler would assume "Oh, golly, I should increment the pSessionProperties pointer by LoggerNameOffset times sizeof(SessionProperties)". He has to make that conversion in order to do byte offset…

I think the complaint was about the cast to LPSTR at the end. typedef char* PSTR, *LPSTR; After the addition, you've already got a char * - casting to a char * again by another name is unnecessary.

So, at that point, it gets to be a little more philosophical, right?

I err on the side of "Oh, the API requests this type (which I'll pretend I don't know is actually just a char*), so I will explicitly cast to that". At least that way it's clear in the future that there is some explicit changing of types going on if, say, LPSTR ever changes.

Silent "Oh, well, we all know that it's really going to be a pointer here anyways" is a good way to get subtle bugs.

Re: The Worst API Ever Made?

#64

Earlier quoted context omitted.

I think the complaint was about the cast to LPSTR at the end. typedef char* PSTR, *LPSTR; After the addition, you've already got a char * - casting to a char * again by another name is unnecessary.

So, at that point, it gets to be a little more philosophical, right? I err on the side of "Oh, the API requests this type (which I'll pretend I don't know is actually just a char*), so I will explicitly cast to that". At least that way it's clear in the future that there is some explicit changing of types going on if, say, LPSTR ever changes. Silent "Oh, well, we all know that it's really going to be a pointer here a…

Right.

For the record, I wasn't criticizing - I mostly agree with the side you're erring on here - I was just trying to clarify the criticism.

Re: The Worst API Ever Made?

#65
I was half expecting the rant to be about TAPI. From memory, I remember at least one instance where it returns a void* pointer, with a few integers telling the offset of the information you need to get.

But at least the function calls made sense. This is much worse.

Re: The Worst API Ever Made?

#66

Poster has not worked very deeply with Win32, is unaware of its conventions. Film at 11. For example: > Yes, that’s right, every user of the Event Tracing for Windows API has to do the arithmetic and layout of the packed structure format themselves. These represent very common idioms in Windows. One common idiom is about binary compatibility. Microsoft can change the length of the structure in a future rev of the SDK…

> Why on earth would you say (LPSTR)(char * ) ? That is literally saying (char * ) (char * ).

Possibly because it's not obvious that LPSTR is the same thing as char *. Sure, if you've done a non-trivial amount of windows programming I'm sure it's one of those things you just get use to. But as someone coming from the Unix world, I can't imagine why you wouldn't just use the native types (same with DWORD and friends).

Re: The Worst API Ever Made?

#67
post #53

Earlier quoted context omitted.

And that article, having the same tone, would be about it being bad. But not understanding the conventions is not the same as being bad. A lot of this coding style is hard fought and battle tested.

No question about it. My answer would actually be a lot more about the history of both Win32 and Win16, both of which I programmed in C++ for years. Looking at something like that now and proclaiming it bad is about the same thing as deploring the Mongol invasions.

The difference is that we no longer have to put up with Mongol invasions.

Re: The Worst API Ever Made?

#68

Earlier quoted context omitted.

Dude worked at RAD, so I'm willing to bet he's got some experience. In your LPSTR example, that could've been to silence compiler warnings about doing pointer arithmetic. EDIT: From bio: The most significant project I’ve created to date has been The Granny Animation SDK, a complete animation pipeline system that I first shipped in 1999 and which, 15 years later, still is in active use at many top-tier game studios. "…

You don't ever need to cast from char * to char * . Period. In 16 bit land the "L" meant something but that hasn't meant anything since long before the API he is calling existed.

The docs remain silent on the subject but since ControlTrace takes a TCHAR * I guess the logger name in the struct could be a TCHAR[] too. So perhaps LPTSTR was intended.
Post reply on HN