Could I improve this code?

Could I improve this code with references or pointers? Is it even possible to use reference or pointer in this case?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int humanTurn(const char table[][7], char human)
{
	int chooise;
	bool access = false;

	do
	{
		do
		{
			cout << "What would you like to choose? :";
			cin >> chooise;

		} while (chooise < -1 || chooise > 7);

		if (checkForEMPTYs(table, chooise) == 0)
			continue;

		access = true;

	} while (!access);

	return chooise;
}
Well it depends. You should use references or pointers when you want to be able to modify that value. As it stands now, your first function arguments is passed by pointer and the second makes a copy of the value passed into it and the copy gets modified. So do you want to be able to change the value of the arguments passed in? If so, use references/pointers.

EDIT: Forgot to mention using references/pointers is also quicker then passing it by value(which is what you're going). Keep in mind that the differences will be negligible. It really only makes a different when you're passing in large data types.
Last edited on
1
2
3
4
5
6
7
8
9
10
int humanTurn(const char table[][7])
{
    int chooise;
    do{
        cout << "What would you like to choose? :";
        cin >> chooise;
    } while ((chooise < -1 || chooise > 7) &&
             !checkForEMPTYs(table, chooise));
    return chooise;
}

As it stands now, your function arguments make a copy of the value passed into them and the copy gets modified
Arrays are not passed by value.
Last edited on
I think I've read in book that if you use reference for constant value that It is faster than normal const.. That this shows in big programs tho it shouldn't have much effect in my connect 4 game.
Arrays are not passed by value.

Correct. Arrays are passed by pointer. My bad. I'll edit post.
MiiNiPaa thanks for showing me how to make more readable code, tho If I use your code , even if I press 9 for example it will still go to next point.
Replace && with ||
Thank you very much!
With all the talk about which passing method is faster and whatnot, it might be worthwhile to note a few general rules for program optimization.

Rule 1: Don't do it
99.9% of the time code doesn't need to be optimized. Improving the speed of most code won't improve your overall performance by any noticeable degree. And truly optimized code is often ugly and bizarrely organized and can obfuscate the true purpose of what the code actually accomplishes.

Rule 2: If you absolutely have to do it, make sure you're doing it in the right place. Use code profilers and such tools to find out where your code really needs it, and concentrate your efforts there. When optimizing, make sure to document the hell out of what changes are made and why.

Rule 3: Measure the results.
Don't just assume that because you changed something, that it'll have a positive impact simply because you read somewhere that a certain method was more efficient that another. Different compilers generate different code even for the exact same program. Furthermore, modern compilers are often far better at optimizing code than we lowly humans are, so let it do the work.

All this is just my way of saying that unless you have a very good reason not to, you should write your code to be as straightforward and simple as possible. Write your code to be read by humans, not computers.

I'll get off my soapbox now.
Topic archived. No new replies allowed.