Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 47 48 [49] 50 51 ... 91

Author Topic: Programming Help Thread (For Dummies)  (Read 100820 times)

Deon

  • Bay Watcher
  • 💀 💀 💀 💀 💀
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #720 on: June 29, 2012, 01:06:10 pm »

Thanks for reply Starver! And don't forget that I use C#, so no &/* magic for me, just straight references! :)
Logged
▬(ஜ۩۞۩ஜ)▬
✫ DF Wanderer ✫ - the adventure mode crafting and tweaks
✫ Cartographer's Lounge ✫ - a custom worldgen repository

AlStar

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #721 on: June 29, 2012, 01:08:47 pm »

This is why it's always good policy to lead with the constant when not doing assignment.

Don't check "if(x==1)", check "if(1==x)" instead and it will catch a lot of those mistakes that might otherwise slip by.

At least, that's how I've always done it: If Desired Value is the same as Given Value, then...

For most C++ compilers there are optional warnings you can enable to make it shout angry words at you for doing assignment in a conditional. In 9.998 out of 10 cases it's unintentional so I always enable it and elevate to an error, along with the one for "not all control paths returns a value" and a few others.

Randomly curious - what case would that .002% usage look like?

I'm trying to think of a situation where you'd want to do that, and failing.

olemars

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #722 on: June 29, 2012, 01:27:22 pm »

It saves you a line of code sometimes, so its intentional use is usually either a case of laziness or inexperence. The lazy wants to type as little as possible, and the inexperienced thinks there is some sort of achievement in writing short code.

I've seen it intentionally used once at work (a case of laziness) since it triggered the warning I had elevated, but then in a while loop like

Code: [Select]
while(x = getNextSomething())
{
     do shit to x in the safe knowledge that it is not NULL
}

You can do that without triggering the warning/error by adding a test, ie
Code: [Select]
while( (x = getNextSomething()) != 0 )although that adds the new pitfall of forgetting one set of parantheses.
« Last Edit: June 29, 2012, 01:29:33 pm by olemars »
Logged

Starver

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #723 on: June 29, 2012, 02:02:49 pm »

Thanks for reply Starver! And don't forget that I use C#, so no &/* magic for me, just straight references! :)

[edit: Whoops, first meant to say "Well, I hope you understood it, at least..." (with the emphasis on me having written something that was generally understandable!), before going on to waffle with the following diversion...]


I need to get into C#.  I know most other variations on C (from K&R onwards) but, as you can see, I'm currently on a bit of a Perl-binge for prototyping[1], and tend to use a form of it even in what should be Pseudocode, and I really need to force myself back to the bleeding-edge again, and last time I touched it I got distracted by some other shiny-shiny language or other...

And while I can't believe that you can't do something very similar in C#, I'm pretty sure that it's not best practice to overly rely on it.  (Probably akin to littering the code with GOTOs... ;) )  And the type-freedom of Perl does tend to spoil me...

Code: (Example) [Select]
my $variable;
$variable = "Hello World"; print "$variable\n";
$variable = [split(//,$variable)]; print join(",",@$variable)."\n";
my $temp=$variable; $variable =+{}; map {$$variable{$_}++} @$temp;
print join("\t",map {"$_: ".$$variable{$_} } sort {$$variable{$b} <=> $$variable{$a}} keys %$variable);
#And that's not even using coderefs and globrefs!

Quote from: Result...
Hello World
H,e,l,l,o, ,W,o,r,l,d
l: 3    o: 2    e: 1    H: 1    r: 1    W: 1     : 1    d: 1

(But as for readability... I can do much better than that... ;) )



[1] In the more mundane "build it quickly, to test", sense, rather than the usual procedural-definition sense!
« Last Edit: June 29, 2012, 02:07:26 pm by Starver »
Logged

Virex

  • Bay Watcher
  • Subjects interest attracted. Annalyses pending...
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #724 on: June 29, 2012, 02:32:19 pm »

(But as for readability... I can do much better than that... ;) )
I'd hope so. To me that almost looks like a piece of assembly code that's been stripped of comments...
Logged

Araph

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #725 on: June 29, 2012, 04:52:21 pm »

if(x = 1) means TRUE, because any value that is not 0 is also TRUE and variable x has from now on the value 1.
if(x == 1) actually tests whether x has the value 1.

Oh, WOW. How did I miss that?! Everywhere else, I didn't mess up on that, and then I somehow managed to screw it up here.

Thank you again for all the help! I'm so excited that I can finally move along with this project!

EDIT: Accidental-psyche! I have a new question: is there a reason that, according to the Firebug console, the variable xOne would go '1, 0.9, 0.8, 0.7000001'? It should only be decreasing by .1 each iteration. Also, looping that function seems to gradually freeze Firefox.
« Last Edit: June 29, 2012, 05:10:46 pm by Araph »
Logged

Normandy

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #726 on: June 29, 2012, 07:15:07 pm »

@Araph:
Floating point numbers are subject to rounding errors, and since they're not stored in base-10, not all simple decimal fractions will necessarily be represented well in floating point.
Logged

Deon

  • Bay Watcher
  • 💀 💀 💀 💀 💀
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #727 on: June 30, 2012, 05:31:50 am »

snip
You can use *var and &var in C#, but this triggers a lot of warnings and you have to enable the "allow unsafe code" check for the Visual Studio to compile it. C# has a lot of inbuilt memory management. For example, you don't have to clear memory, and that's great for me (because all of my more or less long programs in C++ always had memory leaks).
Logged
▬(ஜ۩۞۩ஜ)▬
✫ DF Wanderer ✫ - the adventure mode crafting and tweaks
✫ Cartographer's Lounge ✫ - a custom worldgen repository

Araph

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #728 on: June 30, 2012, 06:43:01 pm »

@Araph:
Floating point numbers are subject to rounding errors, and since they're not stored in base-10, not all simple decimal fractions will necessarily be represented well in floating point.
Oh, okay. I switched the decimals out for whole numbers and changed the size of the boxes to compensate, and now that works. Is Firefox freezing just from the looping function? If so, is it possible to fix that?
Logged

olemars

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #729 on: July 01, 2012, 03:59:37 am »

@Araph:
Floating point numbers are subject to rounding errors, and since they're not stored in base-10, not all simple decimal fractions will necessarily be represented well in floating point.
Oh, okay. I switched the decimals out for whole numbers and changed the size of the boxes to compensate, and now that works. Is Firefox freezing just from the looping function? If so, is it possible to fix that?

I've never done much with javascript, but I suspect that each time setInterval is called you're adding another callback to the function, building up until they bog down execution and freeze up firefox.
Logged

Siquo

  • Bay Watcher
  • Procedurally generated
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #730 on: July 01, 2012, 05:49:30 am »

No, that should not be it, since there is no real loop. Could you post the entire thing (html+js) in a spoiler so I can see?
Logged

This one thread is mine. MIIIIINE!!! And it will remain a happy, friendly, encouraging place, whether you lot like it or not. 
will rena,eme sique to sique sxds-- siquo if sucessufil
(cant spel siqou a. every speling looks wroing (hate this))

olemars

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #731 on: July 01, 2012, 07:54:44 am »

No, that should not be it, since there is no real loop. Could you post the entire thing (html+js) in a spoiler so I can see?

Assuming this is reasonably close to the current code he's using, setInterval will be called a number of times depending on the value of xFour.
setInterval isn't singleshot, so each time that function is called a new recurring timer callback will be added with a 10ms timeout. They'll never stop, and if it is pass by value the number of them will grow exponentially.

setTimeout is singleshot, which I think is what's wanted here.
Logged

Siquo

  • Bay Watcher
  • Procedurally generated
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #732 on: July 01, 2012, 09:01:08 am »

setTimeout is singleshot, which I think is what's wanted here.
#$%^& damn did I mess up. So, to fix: use setTimeout not setInterval. Sorry  :-[
Logged

This one thread is mine. MIIIIINE!!! And it will remain a happy, friendly, encouraging place, whether you lot like it or not. 
will rena,eme sique to sique sxds-- siquo if sucessufil
(cant spel siqou a. every speling looks wroing (hate this))

Araph

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #733 on: July 01, 2012, 11:10:20 am »

setTimeout is singleshot, which I think is what's wanted here.

Hooray! It works now! Thanks for pointing that out!

setTimeout is singleshot, which I think is what's wanted here.
#$%^& damn did I mess up. So, to fix: use setTimeout not setInterval. Sorry  :-[

No need to be sorry, you've been an amazing help!
Logged

Deon

  • Bay Watcher
  • 💀 💀 💀 💀 💀
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #734 on: July 02, 2012, 03:46:01 pm »

I wanted to ask, what is Console.SetBufferSize(x, y) for?
Logged
▬(ஜ۩۞۩ஜ)▬
✫ DF Wanderer ✫ - the adventure mode crafting and tweaks
✫ Cartographer's Lounge ✫ - a custom worldgen repository
Pages: 1 ... 47 48 [49] 50 51 ... 91