SDL 1.3 proposed conventions

David MacCormack wrote:

True, Olivier, it is “Microsoft-ish” :slight_smile: and HRESULTs are
definately what led me to try this out in my own applications.

[…]

SDL_LAST_RESULT_DECL(SDL_ESOMETHING, 0xFFFFFFFF)
SDL_END_RESULT_DECL

That’s just a guess; there’s probably a better way.

What attracted me to SDL was its simplicity and the fact that it takes
almost no time to start using it. Adding all kinds of "interesting"
constructs – using creative #ifdefs and whatnot – for something like error
handling will only complicate the SDL API and make it – in my opinion – a
worse product. Please don’t get all carried away, but keep the API as simple
as possible.

Matthijs

Without starting the holy war ( :slight_smile: ), I’d personally beg for not using
tab characters at all, and just spaces (at indentions of four, if you
prefer). If nothing else, it verifies that the source will look consistent
in everyone’s editor, no matter what his individual tabstops are set to.
That’s just me, and that’s as far as I’ll defend it.

I won’t bother explain here how, I’ll just mention that it is possible
to use tabs for indentation so that variable tab setting look good. The
only problem which you can not avoid is that maximum line length can not
be easily enforced.

My coding style is a bit different (see below) - but I am happy with
anything which is consistent. BTW, I suggest checking out doxygen for
documentation tool.

if(  (bar < baz) &&
     (foo > bar)    )
{
	funky_poker(
		bar + baz - (foo * foo),
		baz + foo - (bar * bar)
	)
	return 0;
}else if( truth == 42 ){
	return 1;
}else{
	return -1;
}

– Timo Suoranta – @Timo_K_Suoranta

Sam Lantinga wrote:

I’m looking for general feedback here from experienced developers.
As always, please do not turn this into a flame war, or I will stop
soliciting feedback.

Thanks!

Here are some proposed conventions for the SDL 1.3 -> 2.0 rewrite:

API conventions:

SDL functions return a boolean value or a pointer value
SDL functions are prefixed with “SDL2_” … “SDL_” ?
SDL functions take a status pointer as the first argument
This status pointer can be 0, but no error information will be saved.

e.g.

bool SDL2_Init(SDL_status *status, Uint32 flags);
SDL_VideoMode *SDL2_FirstVideoMode(SDL_status *status, Uint32 flags);

If a function returns 0 or a NULL pointer, an error code or string
can be retrieved from the status object:

SDL_string *string;

switch (status->code) {
case SDL_OUTOFMEMORY:
/* Perform garbage collection and try again? */
break;
default:
string = SDL2_GetStatusString(status);
ShowMessage(“Operation Failed”, string);
break;
}

SDL_statuscode is an enumerated type that contains as many error
conditions as possible. These are organized into extendable blocks
so that individual drivers can add their own error codes.

Each function prototype will include a documentation section which
contains return value, parameters, and non-driver-specific error
conditions. It will also contain a brief note on thread-safety.
The documentation section is set off by /** so documentation tools
can parse it easily.

Coding conventions:

The SDL code is written in ANSI C using 4 space tabstops and spaces
liberally for code readability:

/**
SDL2_function - this is a useful function to do something

@param status
Standard SDL status information
@param param
A parameter of some type
@return a new object of some type

This function should be used when there is something special to be done.
You should watch out for X, Y, and Z, as they can surprise you.
How much useless documentation can I write for a fictional function?

Thread-safety: This function is completely thread-safe

@see SDL_status
**/
object *
SDL2_function(SDL_status status, int param)
{
/
This comment adds clarity to the code */
if ( internal_function(status) == failure )
handle_failure(status);
else
handle_success();

/*
 * Note that C++ style comments are not allowed by some compilers.
 * Also, keep the code formatted to 78 characters if possible.
 */
if ( (condition_one() == failure) ||
     (condition_two() == failure) ) {
    handle_failure(status);
} else {
    handle_success();
}
return new_object;

}

What do people think? Good? Bad? Ugly?
Again, this is for constructive feedback, so if you don’t have anything
nice to say about someone’s opinions, don’t say it!

See ya!
-Sam Lantinga, Lead Programmer, Loki Software, Inc.

Hi,

I’ve just read all the answers to your proposals and I wonder if SDL
wouldn’t be nice and quite more powerful if it was designed as a state
machine like OpenGL is. It would guaranted a better control on status
error code i believe, but maybe I’m totally wrong.

Bye
Lawouach
www.programmationworld.com

I’m looking for general feedback here from experienced developers.
As always, please do not turn this into a flame war, or I will stop
soliciting feedback.

Thanks!

Here are some proposed conventions for the SDL 1.3 -> 2.0 rewrite:

API conventions:

SDL functions return a boolean value or a pointer value
SDL functions are prefixed with “SDL2_” … “SDL_” ?
SDL functions take a status pointer as the first argument
This status pointer can be 0, but no error information will be saved.

Why boolean? Better unsigned (or SDL_Status or int):
0 - means success,

0 - failure error code.

About this status as first function parameter- i believe it sucks ;), i have
seen/used such syntax in several places and i never have liked it. Imho it
is better that function returns status or if it is a pointer variable it
must return, then NULL means error and last error can be obtained with
function “int SDL_GetLastError()” and last error text can be obtained “char
*SDL_GetLastErrorText()” where it returns error text in static buffer.

About prefixing- maybe leave as it is, or there will be a need to change a
lot of code, just to get it compile. (especially if nothing about parameters
values is changed).

object *
SDL2_function(SDL_status status, int param)
{
/
This comment adds clarity to the code */
if ( internal_function(status) == failure )
handle_failure(status);
else
handle_success();

/*
 * Note that C++ style comments are not allowed by some compilers.
 * Also, keep the code formatted to 78 characters if possible.
 */
if ( (condition_one() == failure) ||
     (condition_two() == failure) ) {
    handle_failure(status);
} else {
    handle_success();
}
return new_object;

}

In general i agree- you style is good, but maybe i can have some proposals:

  1. If you use ‘if’ statement without { and }, maybe you should put this one
    line of code after ‘if’ statement, and generally, maybe always use { and }
    especially if you use ‘else’ as it may lead to some confusion if you do not.
    Ex.:

if(a == b) doSomething();

if(a == b) {
doSomething();
} else {
doSomethingElse();
}

  1. Maybe function declaration/definition could be in one line? Example:

object *SDL_getObject(int objectNum);

Thats, most likely all.

Kovacs

Sam Lantinga wrote:

Coding conventions:

How about mailing list conventions, like
’Son’t quote anything else than what is
needed to get your point clear’ ?-)

– Timo Suoranta – @Timo_K_Suoranta --On Mon, 28 May 2001, Sylvain.Hellegouarch wrote:

About prefixing- maybe leave as it is, or there will be a need to change a
lot of code, just to get it compile. (especially if nothing about parameters
values is changed).

I really like the consistency point told by someone; I certainly wouldn’t
like SDLK2_SPACE, SDLK8_TAB… Much better change to libSDL2.

if ( (condition_one() == failure) ||
     (condition_two() == failure) ) {
    handle_failure(status);
} else {
    handle_success();
}
return new_object;
  1. If you use ‘if’ statement without { and }, maybe you should put this one
    line of code after ‘if’ statement, and generally, maybe always use { and }
    especially if you use ‘else’ as it may lead to some confusion if you do not.
    Ex.:

if(a == b) doSomething();

if(a == b) {
doSomething();
} else {
doSomethingElse();
}

I agree here. I really hate it when I see if without curly braces in
middle of something with curly braces. I would ask to put braces always.

Anotrher thing is where to place the opening curly brace. Some put it to
a lone line, I prefer end of the if line. However, if the condition is
split on several lines, possibly indented with spaces, I recommend
putting the opening curly brace to a lone line.

– Timo Suoranta – @Timo_K_Suoranta

[…]
While I personally think this is great… I thought that the bool type
was
part of ANSI C++ and is not a required type of ANSI C… (anyone feel free
to
correct me if I’m wrong). I know that most C compilers allow for its use
(perhaps even all?), and that some compilers even treat bool in C cases as
standard zero/nonzero conditional cases… so for most (perhaps all?)
this
wouldn’t be a problem…

But, if this is the case (again, feel free to correct me if I am wrong),
then
this wouldn’t be “true” ANSI C and might not be completely portable.

[…]

ANSI C has got the type bool during the C9X standardization process.
Unfortunally there isn’t any ANSI C99 compiler available. And I think that
it will never be. Almost anyone that I know uses C++, and when I have to use
C, the previous standard is good enough.


Paulo Pinto, uRD Software Engineer
Altitude Software (formerly Easyphone)

paulo.pinto at altitudesoftware.com
www.altitudesoftware.com

The opinions expressed by myself are personal and not of my employer.
Programming languages teach you not to want what they cannot provide.

----- Original Message -----
From: criswell@geekcomix.com (Sam Hart)
To:
Sent: Sunday, May 27, 2001 7:33 PM
Subject: Re: [SDL] SDL 1.3 proposed conventions

Paulo Pinto wrote:

ANSI C has got the type bool during the C9X standardization process.
Unfortunally there isn’t any ANSI C99 compiler available. And I think that
it will never be.

People have been working to make GCC handle C99. I don’t think GCC 3.0
will have 100% of the standard, but it seems pretty likely for 3.1.

Stan

Okay, please end this thread, I’ve gotten all the feedback I need.

Thanks everybody! :slight_smile:

See ya,
-Sam Lantinga, Lead Programmer, Loki Software, Inc.

typedef int bool;

In ANSI C, this should work fine. No one has claimed that bool was built
into ANSI C. I’m sure he has an idea in mind something like this. He’d
also, of course, have some clever way to not define this type if the compiler
is actually a C++ compiler, because bool is a primitive built-in type in C++.On Sunday 27 May 2001 11:33, you wrote:

API conventions:

SDL functions return a boolean value or a pointer value

While I personally think this is great… I thought that the bool type was
part of ANSI C++ and is not a required type of ANSI C… (anyone feel free
to correct me if I’m wrong). I know that most C compilers allow for its use
(perhaps even all?), and that some compilers even treat bool in C cases as
standard zero/nonzero conditional cases… so for most (perhaps all?) this
wouldn’t be a problem…

But, if this is the case (again, feel free to correct me if I am wrong),
then this wouldn’t be “true” ANSI C and might not be completely portable.