mercredi 29 août 2018

How to rewrite this pseudocode to make it easier to read and maintain?

interface TableCreator {
    void createTable();
}

interface UserWriter {
    void updateUser(User user);
}

interface UserReader {
    User getUser(string id);
}

// This is like Golang interfaces, where UserClient implicitly inherits other interfaces.
interface UserClient {
    TableCreator();
    UserWriter();
    UserReader();
}

function void createUser(User user, UserClient client) {
    error = createUser(client, user);

    // If attempting to create a user in database failed, table does not exist so create one and try again.
    if error != null {
        createTable(client);
        error = createUser(client, user);
        if error != null {
           print("Tried to create a table and a user, but failed again.")
        }
    }  
}

createUser(UserWriter writer, User user) {
    writer.updateUser(user);
}

createTable(TableCreator creator) {
    creator.createTable();
}

Is there a way to make this code cleaner? Especially in regards to interfaces and createUser function?

I will write this either in Java or Golang if we need proper language implementation.

Thanks :)

Aucun commentaire:

Enregistrer un commentaire