• Advertisement
Sign in to follow this  

no good way to prevent these errors?

This topic is 763 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I just finished completing the implementation of rafts in Caveman v3.0, about 40 changes requiring 28 hours of time.

 

while making those changes i ran into a few bugs, each of which took a while to find.

 

the first was of the form:

 

A.mx=B.mx

A.mx=B.mz      // should be A.mz=B.mx   the x is a typo.  a logic error that creates no syntax error, so the compiler doesn't catch it.

A.x=B.x

A.z=B.z

 

the second was of the form:

 

A.x=B.x

A.z=B.z

// forgot to set A.mx and A.mz entirely!     a logic error, but no syntax error.
 
 
 
the third was of the form 
 
if out_of_bounds(current_location)     // should be new_location, not current_location!     logic error, no syntax error.
    {
    //  a collision occurred
    }

 

 

 

i understand that tools like PVS studio can find the first case.  any other options or tricks?

 

 

 

 

 

 

 

Share this post


Link to post
Share on other sites
Advertisement

If the x,z things are (mathematical) vectors, I use a vector object instead of independent variables, so I would either do

A.something = B.something

A.mSomething = B.mSomething

 

or

 

for(int i=0;i<2;++i)

{

A = B;

}

 

Both of which reduce chance for manual errors when doing repetitive stuff.

 

I guess if you have an y component and want to skip that, and its a common thing, you can write a special function for that:

A.setXZ(B)

 

So whenever I do anything related to vectors/matrices I use loops instead of manually handling every component. It also usually generalizes to any number of dimensions if I care about that.

 

 

For other things, if you cant make the type checker detect the errors, see if you can change your naming conventions to highlight them better. mX instead of mx might be more visible. Also consider adding comments that state what must be done, before actually writing the code. That will force you to think a bit more and also remind if you forget something while writing it out.

 

In the case of using current_location instead of new_location, perhaps you can limit the scope of variables even more (such as by using anonymous scope), so theres less potential wrong choices to make. Some people might also make variables useless by setting some garbage value just after the last use (like setting pointers to nullptr after delete).

 

Then theres tools like assertions and unit tests, which are also a bit like comments, except theyre actually checked. People really care about writing safe and correct code, you could even look into fields where its extra-critical and see what they do.

Share this post


Link to post
Share on other sites

When copying a structure I prefer to use memcpy(). That way you're sure to cover every members. And if you modify your structure by adding or renaming members your code will still work.

Share this post


Link to post
Share on other sites
That's a horrible way to deal with that. At best you are doing what the compiler would do anyway by doing a proper assignment. At worst you screw something up because the structure is not trivially copyable.

Share this post


Link to post
Share on other sites

Defense against typos - in my experience - involves self-care. I use typo frequency as one of my metrics to know when I need to take a break or go to bed.

Share this post


Link to post
Share on other sites

Well, that's not a problem in C, which is the language I use. smile.png

C may not not have the concept trivially copyable written into the language as in C++, but it is certainly possible to create data structures that are not trivial to copy - you can still allocate memory in C. I've had to deal with people using memcpy on things which shouldn't be copied that way in both languages.

Share this post


Link to post
Share on other sites


A.something = B.something

 

thats what i do, but it was a copy/paste/edit error:

 

A.something1 = B.something1    // this line got copy/paste/edited to create the next line

A.something1 = B.something2     // forgot to change 1 to a 2

 

when i code, its a core dump from brain to source file thru the keyboard. and fingers are the bottleneck.  i already use a macro processor/codegen with a shorthand syntax to reduce typing, but i still think in code a billion times faster than i can type. so careless errors can happen. when doing this i always try to make sure my i's a crossed and t's dotted (so to speak) but occasionally i'll overlook something. hazards of banging out simple code.

 


In the case of using current_location instead of new_location, perhaps you can limit the scope of variables even more (such as by using anonymous scope), so theres less potential wrong choices to make.

 

unfortunately both are rather necessary for a standard "move with collision check" algo.

 

 

 

 

 

Share this post


Link to post
Share on other sites


When copying a structure I prefer to use memcpy()

 

in this case i'm copying values from one type of struct to another type of struct, not all the data between two structs of the same type.

Share this post


Link to post
Share on other sites


Defense against typos - in my experience - involves self-care. I use typo frequency as one of my metrics to know when I need to take a break or go to bed.

 

geez! at that rate i'd always be on break (or asleep). <g>.    but you may be right. i put in 16 hours friday (8am friday to 5am satruday with a few breaks), and another 10 on saturday after about 6 hours sleep, then finished up this morning.  some of those typos may have been due to the late hours i was keeping.  but hey! i was on a roll! what can i say? <g>.

 

turns out i had to fix or add seven more things this morning to finish rafts.  and it still doesn't have AI to make them walk to the edge and paddle, and i still need left and right paddle animations for both male and female skeletons.   but it was pretty cool seeing shana queen of the jungle with one of her band members, one follower, and her pet dire wolf sail across the ocean and land on a new shore for the first time ever.    "that's it!   keep paddling fido!   good boy!"   <g>

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement