Code gardening

REVIEWED: Some early returns, avoid if possible
REVIEWED: Some return variable names, for consistency, rename `success` to `result`
This commit is contained in:
Ray
2026-03-29 00:37:01 +01:00
parent 5fad835ff1
commit 8c44ea5032
7 changed files with 188 additions and 179 deletions

View File

@ -1621,18 +1621,20 @@ int GetFPS(void)
for (int i = 0; i < FPS_CAPTURE_FRAMES_COUNT; i++) history[i] = 0;
}
if (fpsFrame == 0) return 0;
if ((GetTime() - last) > FPS_STEP)
if (fpsFrame != 0)
{
last = (float)GetTime();
index = (index + 1)%FPS_CAPTURE_FRAMES_COUNT;
average -= history[index];
history[index] = fpsFrame/FPS_CAPTURE_FRAMES_COUNT;
average += history[index];
}
if ((GetTime() - last) > FPS_STEP)
{
last = (float)GetTime();
index = (index + 1)%FPS_CAPTURE_FRAMES_COUNT;
average -= history[index];
history[index] = fpsFrame/FPS_CAPTURE_FRAMES_COUNT;
average += history[index];
}
fps = (int)roundf(1.0f/average);
fps = (int)roundf(1.0f/average);
}
else fps = 0;
#endif
return fps;
@ -2025,7 +2027,7 @@ void UnloadFileData(unsigned char *data)
// Save data to file from buffer
bool SaveFileData(const char *fileName, void *data, int dataSize)
{
bool success = false;
bool result = false;
if (fileName != NULL)
{
@ -2043,20 +2045,20 @@ bool SaveFileData(const char *fileName, void *data, int dataSize)
else if (count != dataSize) TRACELOG(LOG_WARNING, "FILEIO: [%s] File partially written", fileName);
else TRACELOG(LOG_INFO, "FILEIO: [%s] File saved successfully", fileName);
int result = fclose(file);
if (result == 0) success = true;
int closed = fclose(file);
if (closed == 0) result = true;
}
else TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to open file", fileName);
}
else TRACELOG(LOG_WARNING, "FILEIO: File name provided is not valid");
return success;
return result;
}
// Export data to code (.h), returns true on success
bool ExportDataAsCode(const unsigned char *data, int dataSize, const char *fileName)
{
bool success = false;
bool result = false;
#ifndef TEXT_BYTES_PER_LINE
#define TEXT_BYTES_PER_LINE 20
@ -2096,14 +2098,14 @@ bool ExportDataAsCode(const unsigned char *data, int dataSize, const char *fileN
byteCount += sprintf(txtData + byteCount, "0x%x };\n", data[dataSize - 1]);
// NOTE: Text data size exported is determined by '\0' (NULL) character
success = SaveFileText(fileName, txtData);
result = SaveFileText(fileName, txtData);
RL_FREE(txtData);
if (success != 0) TRACELOG(LOG_INFO, "FILEIO: [%s] Data as code exported successfully", fileName);
if (result != 0) TRACELOG(LOG_INFO, "FILEIO: [%s] Data as code exported successfully", fileName);
else TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to export data as code", fileName);
return success;
return result;
}
// Load text data from file, returns a '\0' terminated string
@ -2166,7 +2168,7 @@ void UnloadFileText(char *text)
// Save text data to file (write), string must be '\0' terminated
bool SaveFileText(const char *fileName, const char *text)
{
bool success = false;
bool result = false;
if (fileName != NULL)
{
@ -2181,14 +2183,14 @@ bool SaveFileText(const char *fileName, const char *text)
if (count < 0) TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to write text file", fileName);
else TRACELOG(LOG_INFO, "FILEIO: [%s] Text file saved successfully", fileName);
int result = fclose(file);
if (result == 0) success = true;
int closed = fclose(file);
if (closed == 0) result = true;
}
else TRACELOG(LOG_WARNING, "FILEIO: [%s] Failed to open text file", fileName);
}
else TRACELOG(LOG_WARNING, "FILEIO: File name provided is not valid");
return success;
return result;
}
// File access custom callbacks
@ -3628,7 +3630,7 @@ void UnloadAutomationEventList(AutomationEventList list)
// Export automation events list as text file
bool ExportAutomationEventList(AutomationEventList list, const char *fileName)
{
bool success = false;
bool result = false;
#if SUPPORT_AUTOMATION_EVENTS
// Export events as binary file
@ -3646,7 +3648,7 @@ bool ExportAutomationEventList(AutomationEventList list, const char *fileName)
memcpy(binBuffer + offset, list.events, sizeof(AutomationEvent)*list.count);
offset += sizeof(AutomationEvent)*list.count;
success = SaveFileData(TextFormat("%s.rae",fileName), binBuffer, binarySize);
result = SaveFileData(TextFormat("%s.rae",fileName), binBuffer, binarySize);
RL_FREE(binBuffer);
}
*/
@ -3677,12 +3679,12 @@ bool ExportAutomationEventList(AutomationEventList list, const char *fileName)
}
// NOTE: Text data size exported is determined by '\0' (NULL) character
success = SaveFileText(fileName, txtData);
result = SaveFileText(fileName, txtData);
RL_FREE(txtData);
#endif
return success;
return result;
}
// Setup automation event list to record to