PHP 代码重构、单元测试与持续集成实践
1. 代码重构的重要性与成果
在软件开发中,代码重构是提升代码质量的关键步骤。经过几次重构后,代码变得更易读、理解、修改和测试,减少了大量重复代码,同时还有进一步优化的空间。例如,以下是部分重构后的代码片段:
{
$this->src->x += ( BIKE_STEP * cos($this->angle_in_radians));
$this->src->y += ( BIKE_STEP * sin($this->angle_in_radians));
++$this->time;
print "biking... currently at (" . round($this->src->x, 2) .
", " . round($this->src->y, 2) . ")<br/>\n";
}
print "Got to destination by biking<br/>";
private function walk()
{
//walk
while (abs($this->src->x - $this->dest->x) > WALK_STEP ||
abs($this->src->y - $this->dest->y) > WALK_STEP)
{
$this->src->x += ( WALK_STEP * cos($this