[QUOTE=ROBO_DONUT;32713682]Right, but that's the entire point of the postfix operator, not really a 'side-effect'.[/QUOTE]
Yes, but that could trip some programmers up, so I don't use it.
[editline]11th October 2011[/editline]
[QUOTE=ief014;32713859]Alright children, I'm sorry that I told you about the differences between prefix and postfix operators. Can we please just move on now? It's really not that big of a deal.[/QUOTE]
No, it's only a few clock cycles of a deal.
Ontopic: You can XOR your variables instead of setting them to 0.
You can also use XOR to swap values without the need for a temporary variable.
a = a^b /* a^b */
b = a^b /* a */
a = a^b /* b */
When in doubt, if-else.
Always code as if it's your job to maintain the code. Use proper comments and try to keep your code simple. If you were at work, you might have to explain this code to someone else, so keeping it readable is good.
However, it also has to work. So keep your priorities in line (Having it working first is foremost) but having it understandable to read is a huge advantage.
(Commenting does not mean explaining what it does, [b]Good[/b] commenting is explaining WHY that code is there.)
I always thought sufix increment is better because you only have to change the last sign to = if you want to increment by something other than one.
That counts more than a few microseconds in my book.
Thanks, I'll try using some of these in my next C++ project!
[editline]a[/editline]
Sorry I mean my next ++C project ;)
Most people here probably already know about this, but whenever you have an array or list and you want to iterate over it in pairs and have it wrap around, you can use the modulus operator. Like this (C#, but you should get the idea):
[cpp]
int[] a = { 1, 2, 3 };
for (int i = 0; i < 3; i++)
Console.WriteLine(a[i].ToString() + " " + a[(i + 1) % a.Length].ToString());
[/cpp]
This would output "1 2", "2 3", and "3 1".
[QUOTE=ROBO_DONUT;32714413]You can also use XOR to swap values without the need for a temporary variable.
a = a^b /* a^b */
b = a^b /* a */
a = a^b /* b */[/QUOTE]
I wanted to post this too, but decided not to because it's incredible slow. (compared to the naive approach)
Copying to a temporary variable should [B]always [/B]be preferred.
Nice trick anyway!
[QUOTE=Felheart;32748910]I wanted to post this too, but decided not to because it's incredible slow. (compared to the naive approach)
Copying to a temporary variable should [B]always [/B]be preferred.
Nice trick anyway![/QUOTE]
There's a simpler variant for numbers.
a = a + b
b = a - b
a = a - b
tmp = a
a = b
b = tmp
[QUOTE=noctune9;32747776]Most people here probably already know about this, but whenever you have an array or list and you want to iterate over it in pairs and have it wrap around, you can use the modulus operator. Like this (C#, but you should get the idea):
[cpp]
int[] a = { 1, 2, 3 };
for (int i = 0; i < 3; i++)
Console.WriteLine(a[i].ToString() + " " + a[(i + 1) % a.Length].ToString());
[/cpp]
This would output "1 2", "2 3", and "3 1".[/QUOTE]
Modulo is rather expensive. An easier way would be
[cpp]
int[] a = { 1, 2, 3 };
for (int i = 0; i < a.Length - 1; i++)
Console.WriteLine(a[i].ToString() + " " + a[i + 1].ToString());
//special case for last element to wrap around to the first
Console.WriteLine(a[a.Length - 1].ToString() + " " + a[0].ToString();
[/cpp]
[QUOTE=Rocket;32749855]That's using a temporary variable.[/QUOTE]
Yep.
[QUOTE=Rocket;32749855]That's using a temporary variable.[/QUOTE]
*Whoosh*
So this isn't programming per say, but its critical you know your IDE and what it can do.
In VS 2008:
ctrl-e,d //Auto-formats document.
ctrl-k,s //surrounds highlighted with stuff like control statements
most 'blue' keywords, hit tab twice //automatically fills out the code snippet. Amazing with for loops!
f12 //takes you to the definition of a variable from where it is called.
shift-f12 //[B]finds every use of the variable you have the cursor on, with easy navigation![/B]
In C#:
The 'is' keyword is amazing! Use it to find children in a collection of more generic types like this:
foreach parent p in parentlist
{
if (p is child)
return p;
}
I'll post more later.
[QUOTE=Darwin226;32749149]There's a simpler variant for numbers.
a = a + b
b = a - b
a = a - b[/QUOTE]
My god, that's amazing!
To toggle between two code blocks in languages that support both // and /* */ style comments, use this construct:
[IMG]http://dl.dropbox.com/u/1453576/comment_toggle_1.png[/IMG]
Now to switch to the lower portion, just remove the first "/":
[IMG]http://dl.dropbox.com/u/1453576/comment_toggle_2.png[/IMG]
Also, to type /* */ comments, use this on your keyboard:
[IMG]http://dl.dropbox.com/u/1453576/numpad.png[/IMG]
So much faster :v:
To alternate parts in code you could also use #if 0 ... #endif or whatever that construct was.
[QUOTE=Dragonsdoom;32771550]So this isn't programming per say, but its critical you know your IDE and what it can do.
In VS 2008:
ctrl-e,d //Auto-formats document.
ctrl-k,s //surrounds highlighted with stuff like control statements
most 'blue' keywords, hit tab twice //automatically fills out the code snippet. Amazing with for loops!
f12 //takes you to the definition of a variable from where it is called.
shift-f12 //[B]finds every use of the variable you have the cursor on, with easy navigation![/B]
In C#:
The 'is' keyword is amazing! Use it to find children in a collection of more generic types like this:
foreach parent p in parentlist
{
if (p is child)
return p;
}
I'll post more later.[/QUOTE]
I could be mistaken but I'm pretty sure yo can just do foreach (Child c in parentList)
If it's C or C++, yes.
[cpp]#if 0
//disabled code
#else
//enabled code
#endif
#if 1
//enabled code
#else
//disabled code
#endif[/cpp]
Has the benefit of not screwing up when you use C-style (multiline) comments.
[QUOTE=ZeekyHBomb;32749817]Modulo is rather expensive. An easier way would be
[cpp]
int[] a = { 1, 2, 3 };
for (int i = 0; i < a.Length - 1; i++)
Console.WriteLine(a[i].ToString() + " " + a[i + 1].ToString());
//special case for last element to wrap around to the first
Console.WriteLine(a[a.Length - 1].ToString() + " " + a[0].ToString();
[/cpp][/QUOTE]
String concatenation is incredibly much more expensive than some module. You need to know what to optimize first, and that'll be get rid of concatenation somehow.
[QUOTE=BlackPhoenix;32776800]String concatenation is incredibly much more expensive than some module. You need to know what to optimize first, and that'll be get rid of concatenation somehow.[/QUOTE]
He does not have any additional string operations in there, because he's only looping from 0 - 1.
But I don't like the redundant code doing it this way.
I presume the C# compiler would pick that up and allocate enough memory for it to fit in a string right away.
If not, just use a StringBuilder then.
The redundant code looks bad indeed. Can't think of a way to get rid of it without division/modulo though.
Sorry, you need to Log In to post a reply to this thread.