Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unnecessary bloat #56

Open
ryannealmes opened this issue Apr 5, 2016 · 0 comments
Open

Unnecessary bloat #56

ryannealmes opened this issue Apr 5, 2016 · 0 comments

Comments

@ryannealmes
Copy link
Collaborator

The following can be refactored

function encrypt_hook(keys) {
    var encrypt_hook = function(hook, next) {
        winston.log("debug", ">>>ENCRYPTING:", hook.data);
        function encrypt_ids(data) {
            return json_transform(data, keys, crypto.encrypt);
        }

        if (hook.data != null) {
            /* winston.log("debug", "encrypting data:", hook.data); */
            var encrypted_data = encrypt_ids(hook.data);
            /* winston.log("debug", "encrypted data:", encrypted_data); */
            hook.data = encrypted_data;
        }

        if (hook.params != null) {
            /* winston.log("debug", "encrypting params:", hook.params); */
            var encrypted_params = encrypt_ids(hook.params);
            /* winston.log("debug", "encrypted params:", encrypted_params); */
            hook.params = encrypted_params;
        }
        next();
    }
    return encrypt_hook;
}

to

function encrypt_hook(keys) {
    return function(hook, next) {
        if (hook.data != null) {
            hook.data = encrypt_ids(hook.data, keys);
        }

        if (hook.params != null) {
            hook.params = encrypt_ids(hook.params, keys);
        }
        next();
    };
}

function encrypt_ids(data, keys) {
    return json_transform(data, keys, crypto.encrypt);
}

I am not convinced about nesting functions within functions like that. Feels like a code smell.

I see why you used the variable so you could log the results. I still think it's additional bloat that makes the code less readable.

I feel like the refactoring above could be improved, but I need to understand what the difference between data and params is. Getting there ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant