aliens

Author Topic: How is my code?  (Read 22949 times)

Offline Daiky

  • Battlescape Programmer
  • Administrator
  • Commander
  • *****
  • Posts: 904
    • View Profile
Re: How is my code?
« Reply #15 on: January 11, 2011, 10:48:54 am »
I would make the setSpeed a private function... how would you handle the acceleration/deceleration logic (for example depending on the weight the plane is carrying: a fully loaded plane accelerates slower).

There is imho also no need in setting a relative speed, only an absolute speed.

You could set a target speed, for example "setTargetSpeed(PATROLLING_SPEED)", but what the current speed is, what actually the patrolling speed is for this craft or when it will reach that target speed is none of my business.

« Last Edit: January 11, 2011, 05:27:06 pm by Daiky »

Offline wITTus

  • Squaddie
  • *
  • Posts: 5
    • View Profile
Re: How is my code?
« Reply #16 on: January 11, 2011, 04:18:20 pm »
Hi!

Stroustrup says, that one must consider different levels of abstraction. You (SupSuper) said, you'd need to implement all of those other companion functions. This is not entirely true since those functions don't share the same level of abstraction.

accelerate() means, that the plane accelerates. How it does so, depends entirely on the plane and therefore on your implementation of that plane. For example, when accelerating, it might also increase fuel consumption as well.

accelerate() might call the private functions setSpeed() and setFuelConsumption()

move(position) and stop() could call accelerate() or brake() then

Another problem is code duplication. Let's say you really want to only change speed of your plane. Ok, no problem (yet). This would be:

plane.getSpeed();
- change speed here -
plane.setSpeed();

Three simple lines which might be copy & pasted everywhere they become necessary. But now you already choosed one implementation and this will constrain you in the future (and may cause some refactoring). What do you do, if you want to change speed slowly (interpolated)?

- You'd need to change those three lines everywhere in your code. Maybe it would become 5 lines now or even more.
- You might forget some locations
- You might later fix a bug in these 5 lines and forget some code locations
- Consider adding extra features like setting fuel consumption (another 3 lines to add everywhere)

Apart from that I think addSpeed() isn't a good idea (as Daiky pointed out already).
« Last Edit: January 11, 2011, 04:21:27 pm by wITTus »

Offline Zyxpsilon

  • Sergeant
  • **
  • Posts: 42
  • Here, Today.
    • View Profile
Re: How is my code?
« Reply #17 on: February 11, 2011, 06:51:23 pm »
Do whatever you must, fast!
So far, so good -- i'd say.

You code_compile_design the complex binaries, i enjoy and work on Hi-Res graphics stuff. Let's rock & roll.