Can copy paste errors be avoided?

I ran into this bug while coding last night

mRegisteredComponents[ TacComponentType::Stuff ] = TacRegisterComponent(
  "Say",
  [](){ return new TacSay(); } );
mRegisteredComponents[ TacComponentType::Say ] = TacRegisterComponent(
  "Stuff",
  [](){ return new TacStuff(); } );

That made me think:
How can I avoid this mistake in the future?
I’ll just… be more careful?

Then two hours later I found out that I had written this:

mRegisteredComponents[ TacComponentType::Stuff ] = TacRegisterComponent(
  "Stuff",
  [](){ return new TacStuff(); },
  TacStuffBits );
mRegisteredComponents[ TacComponentType::Say ] = TacRegisterComponent(
  "Say",
  [](){ return new TacSay(); },
  TacStuffBits );
mRegisteredComponents[ TacComponentType::Model ] = TacRegisterComponent(
  "Model",
  [](){ return new TacModel(); },
  TacModelBits );

For lack of a better solution, that function now enforces correctness through ridicule, containing the following comment:

// copy paste error count: 2

 

Leave a Reply

Your email address will not be published. Required fields are marked *