Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 42 43 [44] 45 46 ... 796

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

GlyphGryph

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #645 on: January 17, 2012, 03:32:17 pm »

Nooo. Nadaka, tell ME about the third way! What is the third way! What is it!

And I always forget the first way is an option in some languages. I can't imagine it would end well in most situations though.

And hey! C++ has duck typing now, right?
Logged

Nadaka

  • Bay Watcher
    • View Profile
    • http://www.nadaka.us
Re: if self.isCoder(): post() #Programming Thread
« Reply #646 on: January 17, 2012, 03:33:47 pm »

Nooo. Nadaka, tell ME about the third way! What is the third way! What is it!

And I always forget the first way is an option in some languages. I can't imagine it would end well in most situations though.

And hey! C++ has duck typing now, right?

The third way is called reflection. And I have already said too much.
Logged
Take me out to the black, tell them I ain't comin' back...
I don't care cause I'm still free, you can't take the sky from me...

I turned myself into a monster, to fight against the monsters of the world.

Vactor

  • Bay Watcher
  • ^^ DF 1.0 ^^
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #647 on: January 17, 2012, 03:35:59 pm »

Have all your objects inherit the same class, something like "gameObject"  using something like this loosened the lid on how idiot proof your code is though, as you could potentially pass an inappropriate object without the compiler noticing, so make sure you use something to guard against that.
Logged
Wreck of Theseus: My 2D Roguelite Mech Platformer
http://www.bay12forums.com/smf/index.php?topic=141525.0

My AT-ST spore creature http://www.youtube.com/watch?v=0btwvL9CNlA

Levi

  • Bay Watcher
  • Is a fish.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #648 on: January 17, 2012, 03:40:08 pm »

And hey! C++ has duck typing now, right?

It does?  That is pretty awesome, I prefer duck typing to inheritance most of the time.
Logged
Avid Gamer | Goldfish Enthusiast | Canadian | Professional Layabout

Nadaka

  • Bay Watcher
    • View Profile
    • http://www.nadaka.us
Re: if self.isCoder(): post() #Programming Thread
« Reply #649 on: January 17, 2012, 03:42:33 pm »

I am not familiar with that phrase: "duck typing"?
Logged
Take me out to the black, tell them I ain't comin' back...
I don't care cause I'm still free, you can't take the sky from me...

I turned myself into a monster, to fight against the monsters of the world.

lordnincompoop

  • Bay Watcher
  • Allusionist
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #650 on: January 17, 2012, 03:43:50 pm »

And hey! C++ has duck typing now, right?

It does?  That is pretty awesome, I prefer duck typing to inheritance most of the time.

It doesn't have duck typing in the language itself. There's apparently a way to do it via Boost, though.

I am not familiar with that phrase: "duck typing"?

Quack quack.
Logged

Levi

  • Bay Watcher
  • Is a fish.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #651 on: January 17, 2012, 03:47:21 pm »

I am not familiar with that phrase: "duck typing"?

"If it walks like a duck and talks like a duck, it must be a duck".

Basically its about the object's class not being as important as the actual methods it provides.

So with inheritance, you might see something like:


Code: [Select]
Class Automobile:
  def drive():
Class Car << Automobile
Class Truck << Automobile


With duck typing, you would just be happy enough that both objects provide the same method.

Code: [Select]
Class Car:
  def drive():
Class Truck:
  def drive():

list = [Truck.new, Car.new]
for each obj in list:
  obj.drive

This also can remove the problem of multiple inheritance, because as long as I provide the right methods to each object, it'll be happy pretending to be whatever you want.

The downside (to some) is that its a bit more dangerous and easier to mess up, because you don't have quite as strict interfaces keeping you honest.
« Last Edit: January 17, 2012, 03:50:56 pm by Levi »
Logged
Avid Gamer | Goldfish Enthusiast | Canadian | Professional Layabout

GlyphGryph

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #652 on: January 17, 2012, 03:49:14 pm »

Huh, I heard the new C++ 11 had something that could do ducktyping. Not everything was ducktyped, but that it was an option. Did I hear wrong?

Quote
"If it walks like a duck and talks like a duck, it must be a duck".
Note quite. It's more "If it walks like a duck and talks like a duck, treat it like a duck" Heh. :P
Logged

Nadaka

  • Bay Watcher
    • View Profile
    • http://www.nadaka.us
Re: if self.isCoder(): post() #Programming Thread
« Reply #653 on: January 17, 2012, 03:50:14 pm »

Yea, I do it all the time in javascript. That is horrible from a code maintenance POV and very strongly recommend against it, almost as much as you shouldn't use reflection.
Logged
Take me out to the black, tell them I ain't comin' back...
I don't care cause I'm still free, you can't take the sky from me...

I turned myself into a monster, to fight against the monsters of the world.

Levi

  • Bay Watcher
  • Is a fish.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #654 on: January 17, 2012, 03:52:40 pm »

Yea, I do it all the time in javascript. That is horrible from a code maintenance POV and very strongly recommend against it, almost as much as you shouldn't use reflection.

I love messy programming, so I like using both.  :D 

Logged
Avid Gamer | Goldfish Enthusiast | Canadian | Professional Layabout

GlyphGryph

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #655 on: January 17, 2012, 03:54:11 pm »

Nadaka, what exactly are you recommending against, there?
Logged

Nadaka

  • Bay Watcher
    • View Profile
    • http://www.nadaka.us
Re: if self.isCoder(): post() #Programming Thread
« Reply #656 on: January 17, 2012, 04:02:06 pm »

Nadaka, what exactly are you recommending against, there?

Duck Typing. In javascript it is a necessity because of the nature of its inheritance model and universally dynamic objects. In a language with a solid class based inheritance model (like c#, c++ or java) it makes it difficult to know what kind of object you are dealing with. And enables occasional errors where the type of object you have is not the type that you expect and your code blows up. Using it means that you get an error at run time instead of compile time when you pass in an object lacking the features you need.

Its only real place is in tiny snippets of code you will never have to modify or maintain.
Logged
Take me out to the black, tell them I ain't comin' back...
I don't care cause I'm still free, you can't take the sky from me...

I turned myself into a monster, to fight against the monsters of the world.

Willfor

  • Bay Watcher
  • The great magmaman adventurer. I do it for hugs.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #657 on: January 17, 2012, 04:03:58 pm »

But... reflection is the most fun thing I have ever found in programming. D: The easiest way to completely screw yourself over, yes, but damn fun in my opinion...
Logged
In the wells of livestock vans with shells and garden sands /
Iron mixed with oxygen as per the laws of chemistry and chance /
A shape was roughly human, it was only roughly human /
Apparition eyes / Apparition eyes / Knock, apparition, knock / Eyes, apparition eyes /

GlyphGryph

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #658 on: January 17, 2012, 04:15:03 pm »

Well, admittedly, it might be a bad idea for those languages, because they weren't built with it in mind. But that's not because duck typing is horrible from a code maintenance POV, it's because you're trying to fight the language instead of using it the way it was intended.

For languages like Python, Lisp and Ruby, duck typing doesn't cause any of the above problems. And having looked it up just now, it SEEMS pretty reliable in C# (though I haven't used it myself there).

If anything, I've found it makes my code more portable, more easily maintained, easier to read, easier to change, and easier to work with overall. If you just want to limit your criticisms to say it makes things more difficult in Java or C++, sure, maybe I'll give you that. But you're original comment was a lot more sweeping.

Quote
And enables occasional errors where the type of object you have is not the type that you expect and your code blows up
If you're using it properly, this should be impossible.

Quote
Using it means that you get an error at run time instead of compile time when you pass in an object lacking the features you need.
Quote
And it also means you get to ignore a bunch of busywork that would create "errors" only in the technical sense that the language doesn't allow them, rather than because it won't actually work.
« Last Edit: January 17, 2012, 04:17:01 pm by GlyphGryph »
Logged

LoSboccacc

  • Bay Watcher
  • Σὺν Ἀθηνᾷ καὶ χεῖρα κίνει
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #659 on: January 17, 2012, 04:55:03 pm »

But... reflection is the most fun thing I have ever found in programming. D: The easiest way to completely screw yourself over, yes, but damn fun in my opinion...


next level: messing stuff around with bytecode injection!
Logged
Pages: 1 ... 42 43 [44] 45 46 ... 796