本文介绍了灵活性与可靠性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我的同事和我正在讨论灵活性与可靠性。 例如,这种方法非常灵活: 公共共享函数GetList(ByVal whereClause As String)As String strSQL =" SELECT field1,field2,field3,field4,field5 FROM table1" if whereClause<> String.Empty然后 strSQL& =" WHERE" &安培; whereClause 结束如果 返回strSQL 结束功能 我的同事批评它是灵活的,但更喜欢使用更多 可靠的技术,如重载和强类型参数。 公共共享函数GetList(ByVal field1Value As String,ByVal field2Value As Integer)As String strSQL =" SELECT field1,field2,field3,field4,field5 FROM table1" strSQL& =" WHERE field1 =''" &安培; field1Value& "''AND field2 =" & field2Value 返回strSQL 结束函数 我的论点是你可以'不能预测每个可能的字段 值和参数的组合,所以where子句和重载方法的数量将会增加到荒谬的比例。 让其他人遇到这个问题吗?如果是这样你怎么看待它? 欣赏任何见解和经验。 谢谢! - - Joe FallonMy co-worker and I are debating Flexibility vs. Reliability.For example this method is highly flexible:Public Shared Function GetList(ByVal whereClause As String) As StringstrSQL = "SELECT field1,field2,field3,field4,field5 FROM table1 "If whereClause <> String.Empty ThenstrSQL &= "WHERE " & whereClauseEnd IfReturn strSQLEnd FunctionMy co-worker agress that it is flexible but would prefer to use morereliable techniques like Overloading and Strong Typing of parameters.Public Shared Function GetList(ByVal field1Value As String, ByValfield2Value As Integer) As StringstrSQL = "SELECT field1,field2,field3,field4,field5 FROM table1 "strSQL &= "WHERE field1=''" & field1Value & "'' AND field2=" &field2ValueReturn strSQLEnd FunctionMy argument is that you can''t predict every possible combination of fieldvalues and arguments so the number of where clauses and overloaded methodswould grow to ridiculous proportions.Have other people faced this issue? If so what did you think about it?Appreciate any insights and experiences.Thanks!--Joe Fallon推荐答案 选择其中一个的标准之一是谁有权获得 it这意味着如果UI开发人员使用GetList(),那么你的第一个例子 就可以了,并且公平地相信UI开发者会传递一个合法的字符串或者 遭受后果。如果用户的输入将用于提供 where子句,那么你应该考虑更严格的控制。 另一个衡量标准是有没有有什么问题吗?意思是......我们应该怎么样?b $ b过分关注一个愚蠢的条款,当条款从未发生过b $ b时发生了?如果您的灵活实施每天都失败,导致混乱和 中断,那么确定现在是时候解析dang字符串并添加一个你确定是?对话框或重新分配这样做的人。 你想要支持LIKE和>,> =,<和< =所以 组合继续增长......更不用说OR,BETWEEN和 某些条件的括号分组。 如果我想控制一些东西,我也不认为我会超载这个方法 a更多。我可能会将这些方法命名为与各种版本所做的相关的事情,即GetListWhereName(),GetListWhereId(), GetListWhereState()或其他什么。他们可能会提供一个通用的GetList,但是如果你试图重载GetList(),那么在你完成之前,你将会耗尽长的 的独特签名。 汤姆One of the criteria for choosing one over the other is "who has access toit" meaning if GetList() is used by the UI developer then your first exampleis fine and it is fair to trust the UI developer will pass a legal string orsuffer the consequences. If a user''s input would be used to supply thewhere clause however then you should consider stricter control.Another measure is "have there been any problems?" Meaning... should we beoverly concerned about a goofy where clause being passed when it has neverhappened? If your flexible implementation fails every day causing chaos anddisruption then "sure" it''s time to parse the dang string and add an "areyou sure?" dialog box or reassign the person doing it.You would want to support "LIKE" and >, >=, < and <= also so thecombinations continue to grow... not to mention OR, BETWEEN and theparenthetical grouping of some conditions.I also don''t think I would overload the method if I wanted to control thingsa bit more. I''d probably name the methods something related to what thevarious versions did, i.e. GetListWhereName(), GetListWhereId(),GetListWhereState() or whatever. They might feed a generic GetList but ifyou try to overload GetList() you will run out of unique signatures longbefore you are even close to done.Tom 这篇关于灵活性与可靠性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-09 00:44