Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 51 52 [53] 54 55 ... 796

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 887336 times)

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #780 on: January 22, 2012, 06:37:44 pm »

Oooooh, ok.
Yea that one will be fun.

EDIT: So just out of curiosity, I decided to go ahead and make my library work with just SDL, and OH MY GOD THIS IS SLOW TO DRAW!!! Yea, moving to OpenGL, in the meantime, have some dorf.
Spoiler (click to show/hide)
It may not look very impressive, but it is just a test. It can display any color and any tile from your tilesheet. I'm currently using curses.

That was as easy as this code, at least on the implementation side, the library side has a bit more to it.
Spoiler (click to show/hide)

Mego

  • Bay Watcher
  • [PREFSTRING:MADNESS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #781 on: January 22, 2012, 07:42:59 pm »


...

I might have to slap you on the wrist if you ask me for help with code using a do loop.

...

But there is a command called break that will pull you out of a loop or switch that you're in immedietly.  It's another one of those commands that I'll shoot you if I see you using in a loop.

You're fired. There is nothing wrong with using do-loops. Even if a while-loop can do the same thing with some setup, a do-loop is often much more intuitive if you want it to run at least once. When you're reading code written by someone else, you would spend more time figuring out what the while loop is doing with all those booleans than you would simply reading the do-while and realizing what it is doing.

Also, break is good. Using booleans for everything means more memory is being used, and we all know how inefficient Java is with memory. The less memory you use, the less Java will reassign every 3 seconds, which means your computer won't have to transfer as much memory from disk to RAM when it page flips because you declare a new variable/object.

GlyphGryph

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #782 on: January 22, 2012, 07:49:56 pm »

Oh god, this brings back memories of how damned unpleasant Java arrays are to use, comparatively.

Logged

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #783 on: January 22, 2012, 07:51:06 pm »

Hey Glyph, try and declare a rectangular 2d array. Go on, try it.  :P

Valid_Dark

  • Bay Watcher
  • If you wont let me Dream, I wont let you sleep.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #784 on: January 22, 2012, 07:54:19 pm »

So I just started learning SDL yesterday, and just finished my first program.  Take a look? 
Spoiler (click to show/hide)

is there anything I'm doing that is bad practice or just stupid or could be written a better way?
and is there a better way to make a rainbow gradient ?  I'm using a sinewave assigned to red green and blue values, but I don't like how the sine wave flats out at the top and bottom of the graph.  I guess I could try using a triangle wave, but thats a retarded amount of math.

or is there an easier way to do a rainbow gradient?
« Last Edit: January 22, 2012, 08:15:43 pm by Valid_Dark »
Logged
There are 10 types of people in this world. Those that understand binary and those that don't


Quote
My milkshake brings all the criminals to justice.

Mego

  • Bay Watcher
  • [PREFSTRING:MADNESS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #785 on: January 22, 2012, 08:13:16 pm »

Not really. Ignore the fact that I totally misread and wrote a function for something completely different.

Code: [Select]
double triangle_wave(double x) {
if((int)x % 2 == 0) return x - (int)x;
else return 1 - (x - (int)x);
}
« Last Edit: January 22, 2012, 08:17:30 pm by Mego »
Logged

Stargrasper

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #786 on: January 22, 2012, 08:24:57 pm »


...

I might have to slap you on the wrist if you ask me for help with code using a do loop.

...

But there is a command called break that will pull you out of a loop or switch that you're in immedietly.  It's another one of those commands that I'll shoot you if I see you using in a loop.

You're fired. There is nothing wrong with using do-loops. Even if a while-loop can do the same thing with some setup, a do-loop is often much more intuitive if you want it to run at least once. When you're reading code written by someone else, you would spend more time figuring out what the while loop is doing with all those booleans than you would simply reading the do-while and realizing what it is doing.

Also, break is good. Using booleans for everything means more memory is being used, and we all know how inefficient Java is with memory. The less memory you use, the less Java will reassign every 3 seconds, which means your computer won't have to transfer as much memory from disk to RAM when it page flips because you declare a new variable/object.

I seem to recall directly stating that I was taught not to use do loops and walking through how and when to use them.  Booleans aren't difficult to read or use.  I never found reading someone else's code to be excessively difficult no matter what crazy thing they did so long as there was enough whitespace and proper indentation.  I've done a lot of tutoring, so maybe I'm just unusually good at reading code.

As for break, it's bad coding practice.  It breaks program flow, which is bad for beginners trying to trace the code.  Java isn't so inefficient that it can't handle a couple extra boolean values.
Logged

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #787 on: January 22, 2012, 08:27:55 pm »

Why would you be taught not to use do-while loops? That was sort of silly on your teachers behalf...

Then again, my first software teacher insisted that NOTHING could be static but the main, regardless of functionality, and all fields MUST be private, protected is NOT ACCEPTABLE!!! Fuck that bitch was crazy... Maybe programming teachers just go insane over time?

Mego

  • Bay Watcher
  • [PREFSTRING:MADNESS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #788 on: January 22, 2012, 08:29:25 pm »

Break is bad coding practice? Really? Then why is it included in almost every modern programming language that supports loops, as well as some pre-modern ones? Are multiple exits from a method also bad?

Why would you be taught not to use do-while loops? That was sort of silly on your teachers behalf...

Then again, my first software teacher insisted that NOTHING could be static but the main, regardless of functionality, and all fields MUST be private, protected is NOT ACCEPTABLE!!! Fuck that bitch was crazy... Maybe programming teachers just go insane over time?

The author of one of the Java books that we use in class insists on private fields EVERYWHERE. No static allowed, either.

Valid_Dark

  • Bay Watcher
  • If you wont let me Dream, I wont let you sleep.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #789 on: January 22, 2012, 08:31:49 pm »

Not really. Ignore the fact that I totally misread and wrote a function for something completely different.

Code: [Select]
double triangle_wave(double x) {
if((int)x % 2 == 0) return x - (int)x;
else return 1 - (x - (int)x);
}

can you explain this further as I don't quite understand what you're doing here?.
for example, my algorithm for my sine wave is

Value = Sine ( Frequency * increment + alignment)* amplitude + center

and it implimented in c++
Spoiler (click to show/hide)


Logged
There are 10 types of people in this world. Those that understand binary and those that don't


Quote
My milkshake brings all the criminals to justice.

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #790 on: January 22, 2012, 08:34:35 pm »

The author of one of the Java books that we use in class insists on private fields EVERYWHERE. No static allowed, either.
Singleton and I became good friends, just to spite that sort of black and white thinking.

Stargrasper

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #791 on: January 22, 2012, 08:35:46 pm »

Why would you be taught not to use do-while loops? That was sort of silly on your teachers behalf...

Then again, my first software teacher insisted that NOTHING could be static but the main, regardless of functionality, and all fields MUST be private, protected is NOT ACCEPTABLE!!! Fuck that bitch was crazy... Maybe programming teachers just go insane over time?

Could be.  I'm not saying my way is right or that anyone else's is wrong.  I'm saying that I teach what I know.  It's hard not to pick up that professor's habits when he teaches literally the first half of the entire curriculum.

I tried my best to teach how to use these things while stating that I generally don't.  If you think I need to revise something, say so and I'll consider it.

Break is bad coding practice? Really? Then why is it included in almost every modern programming language that supports loops, as well as some pre-modern ones? Are multiple exits from a method also bad?

Why would you be taught not to use do-while loops? That was sort of silly on your teachers behalf...

Then again, my first software teacher insisted that NOTHING could be static but the main, regardless of functionality, and all fields MUST be private, protected is NOT ACCEPTABLE!!! Fuck that bitch was crazy... Maybe programming teachers just go insane over time?

The author of one of the Java books that we use in class insists on private fields EVERYWHERE. No static allowed, either.

Break is a holdover from earlier times.  And yes, I was taught that multiple return statements in a method are generally bad.  I'm going off of what I was taught to do.  The memory thing is misleading.  Java's bad at memory, but not that bad.  The reading code thing is subjective.  I'm pretty sure the reason break is everywhere is historic reason.  Can you give me a challenge that doesn't amount to "I don't like how you did it"?
Logged

Mego

  • Bay Watcher
  • [PREFSTRING:MADNESS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #792 on: January 22, 2012, 08:39:04 pm »

What the code does:

If the value passed to the function, truncated to an int, is even, the function returns the fractional part of the value.
If the value, truncated to an int, is odd, the function returns 1 - the fractional part.

Put that in your code somewhere above the main method, and replace the function call to sin with a call to triangle_wave.

@Stargrasper: I'm a firm believer in duct tape programming. I don't consider any programming practice inherently good or bad, as there are always both good and bad ways to use it. If it works, use it. If it doesn't, don't. Calling something "good" or "bad" is a sign of ignorance and close-minded refusal to accept that anything can be used both properly and improperly.

I don't even consider the oft-cursed goto to be inherently evil, despite never using it. Goto can be used correctly, but the amount of work you have to do to make sure it doesn't completely wreck your program is too much to bother with in my opinion.

Can you give me a challenge that doesn't amount to "I don't like how you did it"?

The only real reason your professor taught you not to use it is that argument, essentially. Your professor is not the Programming God. Neither am I.
« Last Edit: January 22, 2012, 08:41:36 pm by Mego »
Logged

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #793 on: January 22, 2012, 08:41:26 pm »

In regards to break, you always have to understand the justification behind using or not using something. If it makes your code more understandable, go for it.
We don't use goto because that never makes things clearer. Ever. It is just begging to cause mass chaos.
However, when you think break might be useful, you have to look at your code and think 'Is this easier to understand because of this break statement?'

GlyphGryph

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #794 on: January 22, 2012, 08:43:10 pm »

Hey Glyph, try and declare a rectangular 2d array. Go on, try it.  :P

Code: [Select]
class RArray
  def initialize
    @collection=[]
  end

  def [] (x,y)
    @collection[x][y]
  end

  def []=(x,y,z)
    if @collection[x]
      @collection[x][y]=z
    else
      @collection[x]=[]
      @collection[x][y]=z
    end
  end
end

Boom!
Logged
Pages: 1 ... 51 52 [53] 54 55 ... 796