HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptMinor

Pass in parameters instead of multiple functions

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
passinsteadmultiplefunctionsparameters

Problem

I can't quite figure out the best way to approach this. I have two jQuery objects that both are used to set cookies. I then parse the JSON and set a cookie that is either the users height or the users weight.

This works fine, but I feel like there is an extreme amount of redundant code here that could be cleaned up but for the life of me I'm blanking on how to best do this. Ideally I'd like to be able to somehow condense this and have a function that I can pass in parameters if I want to receive the height cookie or the weight cookie.

Possibly something along the lines of: userInfo(heightCookie) or userInfo(weightCookie).

I'm a bit of a newbie, so code examples are ideal, please.

var height_cookie = {
        name: 'user-height',
        options: {
            path: '/',
            expires: 365
        }
    };
var weight_cookie = {
        name: 'user-weight',
        options: {
        path: '/',
        expires: 365
        }
    };

function userHeightCookie() {
            var userData = $.parseJSON($.cookie(height_cookie.name));
            return(userData);
    };

function userWeightCookie() {
            var userData = $.parseJSON($.cookie(weight_cookie.name));
            return(userData);
    };

function readHeightCookie(userInfo) {
        $.cookie(height_cookie.name, JSON.stringify(userInfo), height_cookie.options);
    };

function readWeightCookie(userInfo) {
        $.cookie(weight_cookie.name, JSON.stringify(userInfo), weight_cookie.options);
    };

var userInfo = readHeightCookie();
var userInfo2 = readWeightCookie();

Solution

I'd put the cookies into an object so they can be named.

var userCookies = {
    height: {
        name: 'user-height',
        options: {
            path: '/',
            expires: 365
        }
    },
    weight: {
        name: 'user-weight',
        options: {
            path: '/',
            expires: 365
        }
    }
};


Then you only need read/write cookie methods: (I've not used $.cookie so I'm only guessing I haven't broken it.)

function readCookie(cookieName) {
    var userData = $.parseJSON($.cookie(userCookies[cookieName].name));
    return(userData);
};

function writeCookie(userInfo, cookieName) {
    $.cookie(weight_cookie.name, JSON.stringify(userInfo),userCookies[cookieName].options);
};


If you want to be really modular (like all the cool kids) you might do something like:

var CookieMonster =(function ($) {
    var userCookies = {
        height: {
            name: 'user-height',
            options: {
                path: '/',
                expires: 365
            }
        },
        weight: {
            name: 'user-weight',
            options: {
                path: '/',
                expires: 365
            }
        }
    };
    return {
        readCookie: function readCookie(cookieName) {
            var userData = $.parseJSON($.cookie(userCookies[cookieName].name));
            return(userData);
        },
        writeCookie: function writeCookie(userInfo, cookieName) {
            $.cookie(weight_cookie.name, JSON.stringify(userInfo),userCookies[cookieName].options);
        }
    };
})(jQuery);

var userInfo = CookieMonster.readCookie('height');
var userInfo2 = CookieMonster.readCookie('weight');

Code Snippets

var userCookies = {
    height: {
        name: 'user-height',
        options: {
            path: '/',
            expires: 365
        }
    },
    weight: {
        name: 'user-weight',
        options: {
            path: '/',
            expires: 365
        }
    }
};
function readCookie(cookieName) {
    var userData = $.parseJSON($.cookie(userCookies[cookieName].name));
    return(userData);
};

function writeCookie(userInfo, cookieName) {
    $.cookie(weight_cookie.name, JSON.stringify(userInfo),userCookies[cookieName].options);
};
var CookieMonster =(function ($) {
    var userCookies = {
        height: {
            name: 'user-height',
            options: {
                path: '/',
                expires: 365
            }
        },
        weight: {
            name: 'user-weight',
            options: {
                path: '/',
                expires: 365
            }
        }
    };
    return {
        readCookie: function readCookie(cookieName) {
            var userData = $.parseJSON($.cookie(userCookies[cookieName].name));
            return(userData);
        },
        writeCookie: function writeCookie(userInfo, cookieName) {
            $.cookie(weight_cookie.name, JSON.stringify(userInfo),userCookies[cookieName].options);
        }
    };
})(jQuery);

var userInfo = CookieMonster.readCookie('height');
var userInfo2 = CookieMonster.readCookie('weight');

Context

StackExchange Code Review Q#40171, answer score: 5

Revisions (0)

No revisions yet.