After reading noobs, I'm interested in possible way to improve readability of the following short snippet.
- a
struct pictdb_file
is a small file system containing ametadata
array and a file descriptorfpdb
. - the function allocates the ressources, and forwards initialization to
db_create_logic
.
How can I improve the readability of this snippet ? Please comment about: the syntax, white-lines, comments, function extracting, etc.
Also, which version do you prefer and why ?
version 1:
enum error_codes
db_create_ressources(const char* db_filename, struct pictdb_file* db_file)
{
//allocate metadata
db_file->metadata = calloc(db_file->header.max_files, sizeof(pict_metadata));
if (db_file->metadata == NULL) {
return ERR_OUT_OF_MEMORY;
}
//open fpdb
db_file->fpdb = fopen(db_filename, "wb");
if (db_file->fpdb == NULL) {
free(db_file->metadata);
db_file->metadata = NULL;
return ERR_IO;
}
//forward
enum error_codes e;
e = db_create_logic(db_filename, db_file);
//release ressources
free(db_file->metadata);
db_file->metadata = NULL;
db_close(db_file);
return e;
}
version 2: (comment and blank-lines changes)
enum error_codes
db_create_ressources(const char* db_filename, struct pictdb_file* db_file)
{
db_file->metadata = calloc(db_file->header.max_files, sizeof(pict_metadata));
if (db_file->metadata == NULL) {
return ERR_OUT_OF_MEMORY;
}
db_file->fpdb = fopen(db_filename, "wb");
if (db_file->fpdb == NULL) {
free(db_file->metadata);
db_file->metadata = NULL;
return ERR_IO;
}
enum error_codes e;
e = db_create_logic(db_filename, db_file);
free(db_file->metadata);
db_file->metadata = NULL;
db_close(db_file);
return e;
}