5
\$\begingroup\$

I'm somewhat new to writing real-world projects in C, so I'm not familiar with best practices and standards. I'm writing code dealing with EGL and OpenGL, and some setup calls might fail for reasons outside my control. I want to write an initialization function that performs all necessary setup for my application, but exits early and returns false if any call fails (essentially replicating simple exception handling behavior).

Given various philosophies regarding multiple returns, GOTOs, and so forth, I've written my function as follows:

int init_gl(EGLDisplay* outDisplay, EGLContext* outContext, EGLSurface* outSurface) {
 int success = 1;
 EGLConfig config;
 EGLint num_config;
 EGLDisplay display;
 EGLContext context;
 EGLSurface surface;
 static const EGLint attribute_list[] =
 {
 EGL_RED_SIZE, 8,
 EGL_GREEN_SIZE, 8,
 EGL_BLUE_SIZE, 8,
 EGL_ALPHA_SIZE, 8,
 EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
 EGL_NONE
 };
 static const EGLint context_attributes[] =
 {
 EGL_CONTEXT_CLIENT_VERSION, 2,
 EGL_NONE
 };
 bcm_host_init();
 success = success && (display = eglGetDisplay(EGL_DEFAULT_DISPLAY)) != EGL_NO_DISPLAY;
 check();
 success = success && eglInitialize(display, NULL, NULL) != EGL_FALSE;
 check();
 success = success && eglChooseConfig(display, attribute_list, &config, 1, &num_config) != EGL_FALSE;
 check();
 success = success && eglBindAPI(EGL_OPENGL_ES_API) != EGL_FALSE;
 check();
 success = success && (context = eglCreateContext(display, config, EGL_NO_CONTEXT, context_attributes)) != EGL_NO_CONTEXT;
 check();
 success = success && (surface = eglCreateWindowSurface(display, config, NULL, NULL)) != EGL_NO_SURFACE;
 check();
 success = success && eglMakeCurrent(display, surface, surface, context) != EGL_FALSE;
 check();
 if(success) {
 glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
 glClear(GL_COLOR_BUFFER_BIT);
 check();
 *outDisplay = display;
 *outContext = context;
 *outSurface = surface;
 }
 return success;
}

Is this awful? As I understand it, this is not an appropriate place to use assert() for postconditions as the success of these functions is outside of my control. Would it be better to write this as a series of if(!function()) { return 0; } or leave it as is? Don't worry about the check(); statements, this is just a debug macro for assert(glGetError() == 0).

200_success
146k22 gold badges190 silver badges479 bronze badges
asked Nov 22, 2015 at 2:23
\$\endgroup\$
0

1 Answer 1

2
\$\begingroup\$

Certainly found the alternative (2nd version) immediately understandable. Not so with the first (both simplified below). The 2nd maps directly to the stated requirement "exits early and returns false if any call fails". This alone gives it a strong preference.

int success = 1;
....
success = success && func1();
success = success && func2();
success = success && func3();
....

versus

if (!func1()) {
 return 0;
}
if (!func2()) {
 return 0;
}
if (!func3()) {
 return 0;
}
....

Minor: Why isn't the first success = success && func1(); vs. int success = func1();. Just to maintain symmetry?

Why use int instead of bool? Portability? If C99 or later available as the minimum standard, better to use bool.


The success approach may have merit in select circumstances yet do not see that called for here. I would opt for the if () style as it matches the scant coding requirements.

answered Nov 23, 2015 at 1:59
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.