最佳编码实践 [英] Best Coding Practice

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

问题描述

首先,我要说这个问题是一个相当通用的编程问题,但上下文是PHP,所以我认为这个组会有最相关的
洞察力。


无论如何,这也是一个基于意见的问题而不是一个寻求明确答案的
。最近,在保持相当大的
系统的同时。我需要将一个数组类成员添加到一个对象。这是另一个类成员完全相同的

,除了一个数组存储

普通产品,另一个存储免费促销产品。就像

这样,我需要一种方法将产品添加到新的免费阵列中。由于所有

除了价格之外,两个阵列之间的逻辑是相同的,所以
有几个不同的选项可以做到这一点。我对投票感兴趣

一些团队成员认为哪种方式最好。

当然这些是我所设想的缩写版本

可能的解决方案。

#1。

公共函数addArrayA($ object){

//逻辑

$ a [] = $ object;

}


公共函数addArrayB($ object){

/ /同样的逻辑

$ b [] = $ object;

}

#2。 (接下来的两个是这样排列的,因为使用

这个函数的类包含在很多脚本文件中,

所有这些我可能都不知道,所以那里必须是一些

默认值,这个数组总是在需要这个新数组之前使用



公共函数addArray($ object,$ free = NULL){

//逻辑

if(!$ free){

$ a [] = $ object;

}否则{

$ b [] = $ object;

}

}
< br $> b $ b或


#3

公共函数addArray($ object,$ arr =" a"){

//逻辑

$$ arr [] = $ object;

}


我结束了选项编号1,因为我觉得,尽管这是一个效率低下的冗余代码,但是对于那些可能在该项目上工作的其他程序员来说,它更具可读性。另外,我并不认为
感觉完全适应默认变量是全价产品和免费产品之间唯一的差价。想法?

解决方案

对象){

//逻辑

< br>

a [] =


对象;

}


公共函数addArrayB(

First, let me say that this question is a rather general programming
question, but the context is PHP, so I figured this group would have
the most relevant insight.

Anyways, this is also more of an opinion based question than one
seeking a definite answer. Recently, while maintaining a rather large
system. I needed to add an array class member to an object. It was
exactly the same as another class member, except that one array stored
regular products, and the other stored free promotional products. As
such, I needed a way to add products to the new, free array. Since all
the logic was the same between the two arrays aside from the price, I
had a few different options to do this. I''m interested in polling
which way some of the group members feel would have been the best.
Naturally these are abbreviated versions of what I envisioned as
possible solutions.
#1.
public function addArrayA($object){
//logic
$a[] = $object;
}

public function addArrayB($object){
//same logic
$b[] = $object;
}
#2. (These next two are arranged as such, because the class using
these functions is included in many script files,
all of which I may not be aware of, so there would have to be some
default value for the array that was always used
before this new array was needed)
public function addArray($object, $free = NULL){
//logic
if(!$free){
$a[] = $object;
}else{
$b[] = $object;
}
}

or

#3
public function addArray($object, $arr = "a"){
//logic
$$arr[] = $object;
}

I ended up going with option number 1, because I felt that despite the
inefficient, redundant code it would later be more readable to other
programmers that might work on the project. Additionally, I didn''t
feel wholly comfortable with default variables being the only
difference between a full price product and a free product. Thoughts?

解决方案

object){
//logic


a[] =


object;
}

public function addArrayB(


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

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