How others do it.

65 posts

Flag Post

I was thinking that it’d be nice to have some sort of weekly thread where we’d pick a goal and people would post code (how we’d do it), then we’ll discuss about the proposed approaches and stuff.

Somehing like a mix of theory and practice.

It would be about small, particular problems (~100 lines, something that wouldn’t take us more than 5-10 minutes), like “how to make the same event trigger different things based on X & Y” instead of the typical “how to make a zombie game” nonsense.

My interest in such a thread is because I think many of us are so used to our own coding style and practices that we often miss different ways to solve a problem. Also, while reading other’s people code, I’ve found stuff that make me think “of course, that’s so clever!” and that I would’ve never considered otherway.

So… what do you think? Would any of you be interested in something like that?

 
Flag Post

I’d give it a shot.

 
Flag Post

Sounds like a cool idea. I would also like to see something like this.

 
Flag Post

I would possibly give it a shot too depending on the topic.

 
Flag Post

I’d LOVE that. I’ve always been a person to learn from others examples- this would be nice.

 
Flag Post

As someone who’s learned AS2/3 almost entirely from other people’s examples, you can bet I’d be interested.

 
Flag Post

I’d be interested, providing that it isn’t a super complex problem. Perhaps something that can be done in 1 function (and perhaps a few helper functions) and not a whole ton of variables.

 
Flag Post

I think it will work.

If you put a coding question on a forum, programmers will instinctively code an answer for it. Even if you didn’t ask them to

There’s probably some interesting psychology here. Now someone will reply with their solution to the fizzbuzz problem, even if it derails the thread.

 
Flag Post

^ I’ll admit, I did it in my head even though I swore I’d resist any temptation and only read the article. But yeah, simple things like that would be perfect for the OP’s idea.

 
Flag Post
Originally posted by CuriousGaming:

Even if you didn’t ask them to

Haha:

Ask: show me (on paper, but better on a whiteboard) how you would swap the values of two variables.
If they don’t start with creating a third variable, you can pretty much write that person off.

a ^= b;
b ^= a;
a ^= b;

 
Flag Post

My Fizz-Buzz solution :)


for (var i=1; i<101; i++)
{
	if (i%5 == 0 && i%3 != 0)
	{
		trace("fizz");
	}
	else
	{
		if (i%3 == 0 && i%5 != 0)
		{
			trace("buzz");
		}
		else
		{
			if (i%3 == 0 && i%5 == 0)
			{
				trace("fizz-buzz");
			}
			else
			{
				trace(i);
			}
		}
	}
}

 
Flag Post
Originally posted by Draco18s:
Originally posted by CuriousGaming:

Even if you didn’t ask them to

Haha:

Ask: show me (on paper, but better on a whiteboard) how you would swap the values of two variables.
If they don’t start with creating a third variable, you can pretty much write that person off.

a ^= b;
b ^= a;
a ^= b;

The sad part is that many of the ones who will hire you don’t know that; they actually believe a third variable is neccessary.

 
Flag Post
Originally posted by evan999333:

My Fizz-Buzz solution :)


for (var i=1; i&lt;101; i++)
{
	if (i%5 == 0 &amp;&amp; i%3 != 0)
	{
		trace("fizz");
	}
	else
	{
		if (i%3 == 0 &amp;&amp; i%5 != 0)
		{
			trace("buzz");
		}
		else
		{
			if (i%3 == 0 &amp;&amp; i%5 == 0)
			{
				trace("fizz-buzz");
			}
			else
			{
				trace(i);
			}
		}
	}
}

Nice (though you did get fizz and buzz backwards). Here’s mine:

var str:String;
for(var i=1;i<=100;++i) {
	str = "";
	if(i%3 == 0) {
		str = "fizz";
	}
	if(i%5 == 0) {
		str += "buzz";
	}
	if(str != "") {
		str = i;
	}
	trace(str);
}
 
Flag Post

I am so in to this thread :)

 
Flag Post

Ooh. I couldn’t resist trying to code Fizzbuzz… even if nobody asked me to.

// FizzBuzz
var i: int = 0;
var isMult5: Boolean;
var strFB: String;
do
{
	i++;
	isMult5 = (i % 5 == 0);
	if (i % 3 == 0)
	{
		strFB = "Fizz";
		if (isMult5)
		{
			strFB += "Buzz";
		}
	}  // if (i % 3 == 0)
	else if (isMult5)
	{
		strFB = "Buzz";
	}
	else
	{
		strFB = i.toString();
	}
	trace(strFB);
}  while (i < 100)

With regards to loops, I never use “for” or “while.” I only use “do…while.” If I need something like a “while” statement, I do this instead:

if (BoolCheck)
{
	do
	{
	} while (BoolCheck);
}

I’m a sucker for low-impact optimization.

 
Flag Post

Oh, why not? Now that other people are doing it, too…


var sResult:String = "";
for (var i:int = 1; i < 101; i++)
{
	if (i % 15 == 0) // multiples of 3 & 5: FizzBuzz
	{
		sResult += "FizzBuzz";
	}
	else if (i % 5 == 0) // multiples of 5: Buzz
	{
		sResult += "Buzz";
	}
	else if (i % 3 == 0) // multiples of 3: Fizz
	{
		sResult += "Fizz";
	}
	else // other numb@rz
	{
		sResult += i.toString();
	}
	sResult += "\r\n";
}
trace(sResult);

Edit for fugly tabbing

 
Flag Post
Originally posted by Elyzius:

Ooh. I couldn’t resist trying to code Fizzbuzz… even if nobody asked me to.

// FizzBuzz
var i: int = 0;
var isMult5: Boolean;
var strFB: String;
do
{
	i++;
	isMult5 = (i % 5 == 0);
	if (i % 3 == 0)
	{
		strFB = "Fizz";
		if (isMult5)
		{
			strFB += "Buzz";
		}
	}  // if (i % 3 == 0)
	else if (isMult5)
	{
		strFB = "Buzz";
	}
	else
	{
		strFB = i.toString();
	}
	trace(strFB);
}  while (i &lt; 100)

With regards to loops, I never use “for” or “while.” I only use “do…while.” If I need something like a “while” statement, I do this instead:

if (BoolCheck)
{
	do
	{
	} while (BoolCheck);
}

I’m a sucker for low-impact optimization.

While loops are slower than for.

 
Flag Post
trace("12Fizz4BuzzFizz78FizzBuzz11Fizz1314Fizz1617Fizz19BuzzFizz2223FizzBuzz26Fizz2829Fizz3132Fizz34BuzzFizz3738FizzBuzz41Fizz4344Fizz4647Fizz49BuzzFizz5253FizzBuzz56Fizz5859Fizz6162Fizz64BuzzFizz6768FizzBuzz71Fizz7374Fizz7677Fizz79BuzzFizz8283FizzBuzz86Fizz8889Fizz9192Fizz94BuzzFizz9798FizzBuzz")

An efficient programmer doesn’t reinvent the wheel ;)

 
Flag Post
Originally posted by RTL_Shadow:

While loops are slower than for.

These two loops:

for (var i:int = 0; i < 10; i++) { }

var j:int = 0
while (j < 10) { j++ }

Generate the exact same bytecode. Thus, one is not faster than the other. do..while is also almost identical, as would be expected.

 
Flag Post
Originally posted by BigJM:
Originally posted by RTL_Shadow:

While loops are slower than for.

These two loops:

for (var i:int = 0; i &lt; 10; i++) { }

var j:int = 0
while (j &lt; 10) { j++ }

Generate the exact same bytecode. Thus, one is not faster than the other.

http://upshots.org/actionscript/20-tips-to-optimize-your-actionscript
#3. Not sure if this is a reputable source, so let’s run some tests…

var startTime:int = getTimer();
for (var i:int = 0; i < 100000000; ++i) {
	
}
trace(getTimer()-startTime)

First ten results:
390
455
398
409
418
416
423
379
399
392
averages to: 407.9

var startTime:int = getTimer();
var i:int = 0;
while (i < 100000000) {
	i++
}
trace(getTimer()-startTime)

first 10:
8757
8318
8704
8528
8390
8591
8899
8553
8446
8752
I really don’t need to average those numbers out to you to see that the average is in fact A LOT bigger. This was all done on FP 10.1, same compiler, same IDE.

Anyways, my FizzBuzz solution:

			
var startTime:int = getTimer(); // Find the time this function started.
var str:String = ""; // Create the initial string.
for ( var i:int = 1; i <= 100; ++i ) // If i is less than (or equal to) 100, add one, and run...
{
	if ( !( i % 3 ) ) // If 'i' is a multiple of 3...
	{
		str += "Fizz"; // Add 'Fizz' to the string.
	}
	if ( !( i % 5 ) ) // If 'i' is a multiple of 5...
	{
		str += "Buzz"; // Add 'Buzz' to the string.
	}
	if ( str == "" )
	{ // If the string is empty, and nothing has been added...
		trace( i ); // Trace the number 'i'.
	}
	else
	{ // else if there is something in the string.
		trace( str ); // Trace it.
	}
	str = ""; // Reset the string.
}
trace( "Time Elapsed: " + String( getTimer() - startTime ) );

E: Now relooking over my FizzBuzz code, instead of doing !(i%5) it would be faster for me to do i%5==0 to avoid the boolean conversion, but now since I’ve told you guys this I see no reason to go edit it :P

 
Flag Post

RTL: You probably have a different compiler than me that generates shitty code for some reason.

 
Flag Post
Originally posted by BigJM:

RTL: You probably have a different compiler than me that generates shitty code for some reason.

Yes. I got a defective compiler when I downloaded it.

 
Flag Post

Perhaps empty loop optimization? Try throwing ++i inside the for loop and having an empty last statement.

 
Flag Post

Sounds cool, watching these topics will hopefully help me learn a little bit more. And my name is similar to fizzbuzz, so why not :3

 
Flag Post
Originally posted by RTL_Shadow:
Originally posted by BigJM:

RTL: You probably have a different compiler than me that generates shitty code for some reason.

Yes. I got a defective compiler when I downloaded it.

I didn’t say defective, I said different.