[crossfire] Duplicate code

Alex Schultz alex_sch at telus.net
Wed Jan 3 00:45:52 CST 2007


Hi,

I just ran some duplicate code checking stuff over the cf code base, and
made a little report of some of the more significant cases. Perhaps we
should store this list on the wiki or something so people can easily
check off dealing with aspects?

Alex Schultz

random_roll() at line 66 in common/utils.c
random_roll65() at line 111 in common/utils.c
    Some duplicated code. Not sure at a glance if it can be merged.
----
roll_stats() at line 821 in server/player.c
swap_stat() at line 872 in server/player.c
    swap_stat() seems to duplicate some code for resetting some things
like level. To me this looks redundent.
----
*_onion() in random_maps/room_gen_onion.c
    Some code in for those functions is duplicated. May be possible to
merge, but not certain of how
    useful/practical it would be in this case.
----
find_skill_by_name() at line 147 in server/skill_util.c
find_skill_by_number() at line 197 in server/skill_util.c
    Code to find the correct skilltool object is duplicated. That should
probably be moved to it's own function.
----
line 306 in include/global.h
line 8 in include/xdir.h
    Handling for HAVE_DIRENT_H is in both. Should be removed from one.
----
fire_bolt() at line 304 in server/spell_attack.c
fire_bullet() at line 629 in server/spell_attack.c
    Identical 14 line code block used for handling reflecting bullets
and bolts. Should probably be moved into it's own
    function.
----
socket/*.c
    In many places there are significant amounts of duplicated code to
send or recieve similarly formatted commands. Perhaps
    more helper functions for socket stuff is in order? Not much that
could be done other than add some more helper functions
    which isn't a high priority.
----
kill_player() at line 2677 in server/player.c
kill_player() at line 2900 in server/player.c
    Identical 17 line code chunks used for removing poisoning and
confusion, once for in battlefields and once for outside. A
    seperate function should be made for this. Perhaps a two functions,
one for poison and one for confusion.
----
line 111 in common/init.c
line 696 in include/spellist.h
    spellpathnames is defined identically in each. It should only be
defined in one. Probably best to remove it from init.c
----
various functions in server/weather.c
    Various functions meant to be called from weather_effect() start
nearly identically for quite a few lines. Perhaps could
    be improved a little.
----
get_pet_enemy() in server/pets.c
    Between lines 184 and 219 and between lines 115 and 150 are duplicated.
    Probably easily solvable by making a function to search given coords
for a valid enemy
----
cast_change_ability() in server/spell_effect.c
cast_bless() in server/spell_effect.c
cast_curse() in server/spell_attack.c
    All duplicate many lines of code for force insertion.



More information about the crossfire mailing list