Archive

Posts Tagged ‘code’

Early-outs for n00bs

November 26th, 2011 No comments

Sometimes I see people code something up like the following:

void AwesomeFunc( void )
{
    if( var == 3 )
    {
        if( var2 == 4 )
        {
            if( var4 == "napkin" )
            {
                if( var5 == 8495 )
                {
                    g_flag = true;
                }
                else
                {
                    Debug.Log( "var5 wasn't 8495" );
                }
            }
            else
            {
                Debug.Log( "var4 was not napkin" );
            }
        }
        else
        {
            Debug.Log( "var2 wasn't 4" );
        }
    }
    else
    {
        Debug.Log( "var wasn't 3" );
    }
}

Read more…

Categories: Blog, Tips 'n Tricks Tags: ,

Testing at Microsoft

November 26th, 2011 No comments

I was a contract employee at Microsoft, an SDET I. While I was there I learned quite a lot about testing, and I really believe it made me a much better programmer. Although my time there was short lived, I managed to retain some of my testing knowledge, and I hope to share that with you now. So I’ll quickly and briefly cover the basics of testing. I’m not talking about unit testing either. I’ll break down the types of tests you have into different categories. There may be more.

  • BVT (Base verification tests)
  • –Positive test cases
  • FVT (Functional verification tests)
  • –Positive Test CasesĀ (Tester tests for expected failures )
  • –Negative Test Cases (We catch unexpected failures during testing (hopefully) )

Read more…

Categories: Blog Tags: , , , , ,

Const qualifier reminder

November 7th, 2011 No comments

Just a reminder how const-ness works. If I remember correctly.

// These two lines are the same type
// const and int can be switched around and the result is the same.
const int i;
int const j;

// If the word "const" follows the "*", it affects the pointer
int* const k;

// Both the pointer and the int are const
const int* const m;

// Same as line above, it just looks weird
int const * const n;

// Does the const affect the pointer or the int? The int..
int const * p;
Categories: Blog, Miscellaneous, Tips 'n Tricks Tags: ,

Math trickery for 1D & 2D arrays

August 14th, 2011 No comments

Once upon a time, there was a language that didn’t have 2 dimensional arrays. This greatly upset the programmer. Fortunately, the programmer was swift in the art of arithmetic and overcame this obstacle easily.

Lets say for example, we only have access to a 1 dimensional array data structure and it has 50 elements in it.

int data[50];

Lets break this array up into 10 rows and 5 columns to make a grid. So how do we get access to a position say data[4][3]?
There are two things I’m going to teach you now:
1) Retrieve the column & row numbers from a linear index counter.
2) Retrieve the index from a row & column numbers.

Read more…

Categories: Articles, Blog, Miscellaneous Tags: , ,

Things you can do to make your code better

January 6th, 2011 No comments

I got most of this list from some other place on the internet, but I felt there could be more added to it.
I’ll continue to add to this list as I think of things or come across things.

  1. Test Driven Development Really Is Worth It
  2. Don’t Rely On Comments Too Much: Make Your Code Self-Explanatory
  3. Don’t Let Exceptions Disappear Into A Black Hole
  4. Don’t reinvent the wheel. Use libraries when possible because you know they work.
  5. Code like you will reuse the code for other projects.
  6. Learn what good design should look like an emulate it until you see why it is good design (and what you dislike about it).
  7. Too many newbies like to over-engineer just to pretend to be smart or having fun with resume-driven-development. In other words, implementing every pattern in the book or putting bayesian filters in every corner of the code should be avoided if they aren’t needed. Debugging, testing, maintenance will be all easier, not mentioning performance.
  8. Have your code reviewed often.
  9. Be wary of code smells (Class too big, method too big, method with too many arguments, Ask don’t Tell etc).
  10. Enforce standards by using checkstyle / PMD, cobertura / emma, findbugs, etc within maven build cycle to make sure code in repository is adhering to a certain quality standard.
  11. Make sure it meets the quality standard for the project.
  12. Just keep it simple. Functions should be short and sweet, and do just one thing. They should fit on one or two screenfuls of text (the ISO/ANSI screen size is 80×24, as we all know), and do one thing and do that well.
  13. Test, test, test.
  14. You will see how many times you will have to tradeoff between design and schedule. Forgive yourself but remember to refactor whenever possible.
  15. Use Standard Annotation Language (SAL) if you are using C or C++, to prevent bugs and make your code robust. It is found in the book “Writing Secure Code for Windows Vista.”
  16. Code with contracts (partially enforced with SAL).
  1. Avoid global mutable state, such as static variables and static singletons.
http://misko.hevery.com/2008/11/11/clean-code-talks-dependency-injection/
http://misko.hevery.com/2008/11/21/clean-code-talks-global-state-and-singletons/
  1. Make your methods small and well named.
http://c2.com/ppr/wiki/WikiPagesAboutRefactoring/ComposedMethod.html
http://tottinge.blogsome.com/meaningfulnames/
  1. Simple Design
http://jamesshore.com/Agile-Book/simple_design.html
  1. Adhere to SOLID Principles
http://en.wikipedia.org/wiki/Solid_(object-oriented_design)
http://butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod

Simple coding tips 01

November 24th, 2010 No comments

Simple tip: Naming functions

Examples:
GetName()
SetName()
DoAreaTransition()
CreateBird()
DestroyBird()

Note how each function name begins with a verb and usually, each function ends with a noun. Naming your functions this way helps you think more clearly about what you’re doing and helps other people understand your code more thoroughly.

Categories: Blog, Tips 'n Tricks Tags: ,

A Few Code Snippets

February 19th, 2010 No comments

Periodically I’ll stumble across a small but useful piece of code. I have a small list right now, but as I stumble across them, I’ll post them here. Note: I do not take credit for any of this code.

Pausing your application

__asm int 3;

or

__debugbreak();

Convert any data type to a std::string object

#include <sstream>
template <typename T>
std::string toString( const T &arg )
{
    std::stringstream ss;
    ss << arg;
    return ss.str();
}

Test if a float is zero or near zero

inline bool IsZero( float val )
{
    return( fabs( val ) < EPSILON);
}

EPSILON is a #define that is something small, like 0.002f
Note, I will repost, I just realized that serendipity can’t handle angle brackets…

Categories: Blog, Tips 'n Tricks Tags: , ,