Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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();
share|improve this question
1  
Welcome to CR! +1 for an on-topic first post! :) –  Mat's Mug Jan 27 at 16:22
 
you are missing an apostrophe, fixed it –  Malachi Jan 28 at 15:58
add comment

1 Answer

up vote 3 down vote accepted

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');
share|improve this answer
 
nice approach! Thanks @James Khoury –  Delmon Young Jan 29 at 20:55
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.