Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
9 | class ElggBlog extends ElggObject { |
||
10 | |||
11 | /** |
||
12 | * Set subtype to blog. |
||
13 | */ |
||
14 | protected function initializeAttributes() { |
||
19 | |||
20 | /** |
||
21 | * Can a user comment on this blog? |
||
22 | * |
||
23 | * @see ElggObject::canComment() |
||
24 | * |
||
25 | * @param int $user_guid User guid (default is logged in user) |
||
26 | * @return bool |
||
27 | * @since 1.8.0 |
||
28 | */ |
||
29 | View Code Duplication | public function canComment($user_guid = 0) { |
|
41 | |||
42 | /** |
||
43 | * Get the excerpt for this blog post |
||
44 | * |
||
45 | * @param int $length Length of the excerpt (optional) |
||
46 | * @return string |
||
47 | * @since 1.9.0 |
||
48 | */ |
||
49 | public function getExcerpt($length = 250) { |
||
56 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.