何时使用哪种-多种方法,多种参数或选项参数 [英] When to use which - multiple methods, multiple parameters, or an options parameter

查看:81
本文介绍了何时使用哪种-多种方法,多种参数或选项参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题是从javascript的角度来看的,但是它肯定可以应用于其他语言.

This question is coming from a javascript point of view, but it certainly could apply to other languages.

我最近越来越多地涉足这一领域,并且想知道何时建立方法的最佳实践,或者至少是好的设计标准.

I have been running into this more and more lately, and was wondering if there was a best practice, or at least good design standard, for when how to build your methods.

我看到的明显选项如下,每个选项都有一个简单的示例

The obvious options that I see are as follows, along with a trivial example for each

  • 多种方法:

  • Multiple methods:

this.makeGetRequest = function(controller){...}
this.makeSynchronousGetRequest = function(controller){...}
this.makePostRequest = function(controller, data){...}

  • 一种具有更多参数的方法:

  • One method, with more parameters:

    //data would be an optional parameter
    //  this.makeRequest("friends", "GET", true);
    //  this.makeRequest("friends", "POST", false, newFriend);
    this.makeRequest = function(controller, type, isSynchronous, data){...}
    

  • 一种方法,带有options参数:

  • One method, with an options parameter:

    this.makeRequest = function(controller, type, options);
    this.makeRequest("friends", "POST", {data:newFriend, isSync:false});
    

  • HTTP请求示例仅是一个问题,但这适用于具有大量自定义/变量的任何面向公众的功能.

    这三个功能显然都是一样的.但是什么是好习惯?有没有可以遵循的标准或准则?

    All three are obviously just as functional. But what's good practice? Is there a standard, or guideline that tends to be followed?

    推荐答案

    options参数的优点在于,它可以在不牺牲清晰度的情况下提供无限的选项.当有人指定data:isSync:时,显然会填充什么设置.记住将函数中的第13个参数设置为false是造成混乱的秘诀.

    The advantage of the options parameter is that it allows unlimited options without sacrificing clarity. When someone specifies data: or isSync: it's plainly obvious what settings are being filled in. Remembering to set the 13th parameter in a function to false is a recipe for confusion.

    因此,在以下两个选项之间,我仅在以下情况下使用多个参数:(1)总共少于四个参数;(2)顺序合理且易于记忆(URL,然后键入,然后是数据) ,以及(3)不超过一个(最后一个)是可选的.无论如何,这都是经验法则.

    So, between the last two options I only use multiple parameters in cases where (1) there are fewer than four parameters in total, (2) the sequence is logical and easy to remember (URL, then type, then data), and (3) no more than one (the last one) is optional. That's as a rule of thumb, anyway.

    与参数化相比,多种方法呢?当您有少量可能性(GET与POST,UseDefaults与DontUseDefaults等等)时,多种方法只是一个选择.考虑一下,只有在以下情况下,我才可能设置单独的方法:

    How about multiple methods versus parameterization? Multiple methods is only an option to begin with when you have a small number of possibilities (GET vs. POST, UseDefaults vs. DontUseDefaults, or whatever). Thinking about it, I'm likely to setup separate methods only when:

    1. 区别非常重要(同步请求的行为与异步请求在本质上是不同的,我希望开发人员自觉选择要应用的对象.(相比之下,GET与POST只是请求的另一个属性-如果您做错了,那不是一个最终的决定性交易.)

    1. The distinction is highly important (a synchronous request behaves fundamentally different from an asynchronous request and I want the developer consciously choosing which to apply. (Whereas by contrast GET vs. POST is just one more property of your request — it's not an end-all, deal-breaking decision if you do it wrong.)

    您没有其他设置要设置的选项.如果我首先要记住是调用foo.get()还是foo.post() ,那么请记住填写options对象 无论如何 ,我只是强迫您在两个不同的地方做出决定.我宁愿将所有设置都放在一个地方.

    There aren't other options you'll want to set anyway. If I first have to remember whether to call foo.get() or foo.post() and then remember to fill in an options object anyway, I'm just forcing you to make decisions in two different places. I'd rather have all the settings in one place.

    这篇关于何时使用哪种-多种方法,多种参数或选项参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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