编写通用代码 [英] Writing a generalized code

查看:102
本文介绍了编写通用代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在硬编码if条件(例如'AssignedTo','ClaimStatusId','ClaimTypeId')时具有以下功能.

I have the following function where i hard code if conditions like 'AssignedTo','ClaimStatusId','ClaimTypeId'.

因此,strCondition字符串具有类似于'AssignedTo','ClaimStatusId','ClaimTypeId'的任何值,然后它将被重定向到相应的if条件

So, the strCondition string has any of the values like 'AssignedTo','ClaimStatusId','ClaimTypeId', then it will be redirected to corresponding if conditions

function ChangeIDToString(strCondition,id)
    {
        if (strCondition.indexOf("AssignedTo") > -1)
            return GetUserName(id)
        else if (strCondition.indexOf("ClaimStatusId") > -1)
            return GetClaimStatus(id)
        else if (strCondition.indexOf("ClaimTypeId") > -1)
            return GetClaimType(id);           
        else
            return id;
    }

有什么办法可以消除这种硬编码的值吗? "AssignedTo","ClaimStatusId","ClaimTypeId"?

Is there any way i can eliminate this hardcoded values like 'AssignedTo','ClaimStatusId','ClaimTypeId'?

推荐答案

您可以尝试使用对象存储字符串值,当然它仍然是硬编码的,因为机器不可能理解需要执行的内容条件为"AssignedTo"时为"GetUserName()".

You can try to use an object to store the string values, of course it is still hard coded because it is impossible that the machine understands that it need to execute "GetUserName()" when the condition is "AssignedTo".

var Conditions = {
    GetUserName: "AssignedTo",
    GetClaimStatus: "ClaimStatusId",
    GetClaimType: "ClaimTypeId"
}
function ChangeIDToString(strCondition,id)
{
    if (strCondition.indexOf(Conditions.GetUserName) > -1)
        return GetUserName(id)
    else if (strCondition.indexOf(Conditions.GetClaimStatus) > -1)
        return GetClaimStatus(id)
    else if (strCondition.indexOf(Conditions.GetClaimType) > -1)
        return GetClaimType(id);           
    else
        return id;
}

这篇关于编写通用代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆