Oh what tangled webs we weave...

BlitzMax Forums/BlitzMax Programming/Oh what tangled webs we weave...

sswift(Posted 2006) [#1]
When first we practice OOP!


Text.Create(NewText$, 0, 0, 1, Board).Pivot.AnimatePosition(X#, Y#, X#, Y#-250, 1000)



Sure, it's wonderfully compact, but it's also wonderfully unreadable, and I think I'll be splitting that code into two lines and making a useless temp variable.

I just wanted to see if it would COMPILE. :-)


H&K(Posted 2006) [#2]
Text.Create(NewText$, CAConstForThisA, CAConstForThisB, AVConstForThisC, Board).Pivot.AnimatePosition(AFloatPair.X, AFloatPair.Y#, AFloatPair.X, AFloatPair.Y#-CAConstForThisD, CAConstForThisE)



Grey Alien(Posted 2006) [#3]
haha.

Yeah I quite often do Something.Create(blah).Field = x or Something.Create(blah).Method(x,y,z). It's very useful.

Or this Something.Create(x,y,AnotherThing.Create(z))


Warren(Posted 2006) [#4]
, and I think I'll be splitting that code into two lines and making a useless temp variable.

Odds are that's what the compiler is doing anyway. :)


ozak(Posted 2006) [#5]
Actually this style of coding is used extensively in Java without problems. Just make sure that the creation of said object never fails, or you could be looking down the wrong end of a null pointer one of these days ;)


sswift(Posted 2006) [#6]
"Actually this style of coding is used extensively in Java without problems."

Without execution problems maybe, but without readability problems? No. :-)


And speaking of readability... Notice how in my code you can't tell what the individual numbers do? Generally after you have three or four variables being passed to a function, it gets to the point where you can no longer remember what each parameter is, or at least, no longer tell at a glance.

If I wrote DrawText(10, 10, "Hello") you'd be able to guess that the first two numbers are X and Y position, but if I had DrawText(10, 10, "Hello", 1) you'd never know from looking at it that 1 means the text is right justified.

Of course the IDE can help with this IF it supports showing the fuction parameters when you mouse over a function. But it really seems like a language design problem to me. I'm not sure how you'd go about fixing it though.

In my sprite system, because of this issue, I'm often tempted to take the create function which is like so:

TS = Create(IMG_Bullet, 1, Background)


And instead force the user to use New to create a sprite, and then specify the image, order and parent seperately after it like so:

TS = New Sprite
   TS.SetImage(IMG_Bullet)
   TS.SetOrder(1)
   TS.SetParent(Background)



Because now you can just look at that and tell what all the parameters are.

(Actually, you can do it that way right now, but you'd be forced to do it that way if I took out that create function. I don't even do it that way myself though!)

But while I could do this for sprites in this case, you can't do it for everything. If I want to create a new sprite animation, I couldn't really expect someone to make a new animation, get a pointer to it they'll certainly never use again, and then call six or seven functions to set parameters for that animation. That's just crazy. It'd be easy to read as all hell, but it would take forever to write out.

Hm...


Grey Alien(Posted 2006) [#7]
yeah, it's just a fact of life. Some cool IDEs show you want the params are in a pop up box as you are typing the function name, but this is no use later when you are just viewing the code. You either have to memorise the main function params, or look them up - that's if you need to of course, because many times you can just code and forget. What we really need IDEs to do is show the function params (perhaps with the values you've passed in also e.g. x=10, y=20) when you move the mouse over a function and let it hover for 1 sec or so...