Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Critique for my code

Name: Anonymous 2012-12-21 10:06

Give your critique for this snippet, please.

How to improve aesthetics?


int prog (Directory *directory)
{
    int error = 0;

    Compile compile = { directory, null, null, null, null };

    if ((compile.file_depender = tree_new ()) == null)
        goto exception;
    if ((compile.topologics = list_doubly_create ()) == null)
        goto exception;
    if ((compile.dependers = list_singly_create ()) == null)
        goto exception;
    if ((compile.prog_files = list_singly_create ()) == null)
        goto exception;
    if (list_singly_foreach (directory->files, (Function)depender_create, &compile) != 0)
        goto exception;
    if (list_singly_foreach (compile.dependers, (Function)depender_link, &compile) != 0)
        goto exception;
    if (list_doubly_foreach (compile.topologics, (Function)topologic_sort, &compile) != 0)
        goto exception;

    compile.topologics->reverse = 1;

    if (list_doubly_foreach (compile.topologics, (Function)topologic_compile, &compile) != 0)
        goto exception;
    if (list_singly_count (compile.prog_files) == 0)
        goto exit;
    if (compile_app_link (&compile) != 0)
        goto exception;

    goto exit;
exception:
    error = 1;
exit:
    list_singly_foreach (compile.dependers, (Function)depender_destroy, null);
    list_singly_destroy (compile.dependers);
    list_doubly_foreach (compile.topologics, (Function)topologic_destroy, null);
    list_doubly_destroy (compile.topologics);
    list_singly_destroy (compile.prog_files);
    tree_destroy        (compile.file_depender);

    return error;
}


Thank you!

Name: Anonymous 2012-12-21 10:47

Maybe split it up?

int try_compile(Compile *compile) {
    if ((compile->file_depender = tree_new()) == null)
        return 1;
    if ((compile->topologics = tree_new()) == null)
        return 1;
    if ((compile->dependers = list_singly_create()) == null)
        return 1;
    if ((compile->prog_files = list_singly_create()) == null)
        return 1;
    if (list_singly_foreach(compile->directory->files, (Function)depender_create, compile) != 0)
        return 1;
    if (list_singly_foreach(compile->dependers, (Function)depender_link, compile) != 0)
        return 1;
    if (list_doubly_foreach(compile->topologics, (Function)topologic_sort, compile) != 0)
        return 1;
       
    compile->topologics->reverse = 1;
   
    if (list_doubly_foreach(compile->topologics, (Function)topologic_compile, compile) != 0)
        return 1;
    if (list_singly_count(compile->prog_files) == 0)
        return 1;
    if (compile_app_link(compile) != 0)
        return 1;
       
    return 0;
}

int prog(Directory *directory)
{
    Compile compile = { directory, null, null, null, null };
    int error = try_compile(&compile);
   
    list_singly_foreach(compile.dependers, (Function)depender_destroy, null);
    list_singly_destroy(compile.dependers);
    list_doubly_foreach(compile.topologics, (Function)topologic_destroy, null);
    list_doubly_destroy(compile.topologics);
    list_singly_destroy(compile.prog_files);
    tree_destroy(compile.file_depender);

    return error;
}


(I haven't tested this.)

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List