My ever-evolving coding style

I am a self taught hobbyist programmer. Today, my subconcious decided to be helpful and torture me with randomly fetched memories of my old coding style. Here's a sample of my coding style from when I was still in the C-with-classes phase, circa 2006:

Honestly I like returning pointers for most cases. Using the pointer you dont have to make a thousand function calls. Especially in parallel programming this is very useful when you want to avoid static variables. Sometimes its not a good idea, or not even possible depending on the use, but I believe this is a perfectly valid programming habit. IMO

Honestly I like returning pointers for most cases. Using the pointer you dont have to make a thousand function calls. Especially in parallel programming this is very useful when you want to avoid static variables. Sometimes its not a good idea, or not even possible depending on the use, but I believe this is a perfectly valid programming habit. IMO

You do realize that that method destroys your ability to enforce class invariants? And that it has no advantage over simply making your members public (and has the disadvantage of being clunky and a really non-obvious and non-idiomatic way of modifying an object)?And that this doesn't do anything to help avoid static variables (how is this even related to static variables?)?

There might be a few exceptional cases where it's a decent idea, but it should be just that, exceptional cases. If it's your preferred/common method... something is wrong.

Yes, if your class has a million getters and setters it's probably doing far too much for its own good. I like the template idea, it reduces boilerplate code while preserving encapsulation. On the other hand, making the member public is not a good idea unless it's a trivial member or a trivial class, because as soon as you need to enforce a condition on this member your class interface has to change, and all code depending on the class breaks (sure, the fix is a find/replace away, but then you need to recompile etc..)

I kind of liked the Object Pascal/Delphi way for properties (sorry if there are any syntax errors, it's been months since I wrote any):

Which I think is advantageous in that you immediately see from the interface what is read-only and what is read-write (or write-only, should you have a use case for that) and you can easily switch from a default getter (or setter) without even changing the class interface. As far as client code goes, it just thinks Foo and Bar are properties as if they were ordinary members (accessed by class.Foo) and any reads go through the getter, any writes go through the setter. It also works with indexed properties. I think it's rather neat, I'm sure it has its downsides though.

Of course, if you are absolutely certain that any value of the member is valid and you don't need to do anything when it changes, and that you will never reconsider this design decision, just make it public. This is rather rare, though, usually this kind of member is set inside a constructor or other initialization method via some argument.

The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

LoadContent: retrieves the assets used by this tank and stores them in the member variables.

Draw: adds the tank sprite to the sprite batch so it gets drawn later.

Initialize: sets some member variables that affect the behavior of the tank.

MoveTank: makes the tank move in the specific direction, applying physics as needed.

RemoveTank: called when a tank is exploding? No idea what the time is for.

MoveBullet: as MoveTank but for the bullets of each tank. I guess they're tied to tanks to ensure they can shoot only one bullet each (doesn't seem very smart, a tank could blow up before the bullet vanishes).

LaunchBullet: makes the tank shoot a bullet.

BulletDirection: retrieve bullet's direction? Why is this a string?! o_O

RemoveBullet: gets rid of the bullet when it hits something.

RotateAI: makes the AI try to rotate in a specific direction. Again, what is the string for?!

FireAI: makes the AI try to fire a bullet.

TurnAI: Like RotateAI but giving the angle directly. ...also I take that, the angles are given as strings -_-'

How much of that was wrong?

Don't pay much attention to "the hedgehog" in my nick, it's just because "Sik" was already taken =/ By the way, Sik is pronounced like seek, not like sick.

BulletDirection: retrieve bullet's direction? Why is this a string?! o_O

Hey, it's just flexible design. That way the function can return, in addition to actual directions, "over there", "this way", "behind you" or even "nowhere". You can never have too many strings.

The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

LoadContent: retrieves the assets used by this tank and stores them in the member variables.

Draw: adds the tank sprite to the sprite batch so it gets drawn later.

Initialize: sets some member variables that affect the behavior of the tank. Constructor? What's that?

MoveTank: makes the tank move in the specific direction, applying physics as needed.

RemoveTank: called when a tank is exploding? No idea what the time is for. Respawn timing.

MoveBullet: as MoveTank but for the bullets of each tank. I guess they're tied to tanks to ensure they can shoot only one bullet each (doesn't seem very smart, a tank could blow up before the bullet vanishes). Problem solved long ago by splitting the damn superclass into 3 normal ones.

LaunchBullet: makes the tank shoot a bullet.

BulletDirection: retrieve bullet's direction? Why is this a string?! o_O

RemoveBullet: gets rid of the bullet when it hits something.

RotateAI: makes the AI try to rotate in a specific direction. Again, what is the string for?!

FireAI: makes the AI try to fire a bullet.

TurnAI: Like RotateAI but giving the angle directly. ...also I take that, the angles are given as strings '

Honestly I like returning pointers for most cases. Using the pointer you dont have to make a thousand function calls. Especially in parallel programming this is very useful when you want to avoid static variables. Sometimes its not a good idea, or not even possible depending on the use, but I believe this is a perfectly valid programming habit. IMO

You do realize that that method destroys your ability to enforce class invariants? And that it has no advantage over simply making your members public (and has the disadvantage of being clunky and a really non-obvious and non-idiomatic way of modifying an object)?And that this doesn't do anything to help avoid static variables (how is this even related to static variables?)?

There might be a few exceptional cases where it's a decent idea, but it should be just that, exceptional cases. If it's your preferred/common method... something is wrong.

It is all dependent on what it is you are programming and what the expectations of that program are. To say their are only a few exceptional cases where you would return a pointer and that it has no advantages is purely based on ones own perspective.

My current, long term project is a game engine I have been working on for some time now. The overall goal is parallelism. Being able to pass off a single object pointer to multiple subsystems so that each can do its work at the same time, to me, is an intelligent design. Whether it is the object itself, or maybe just the draw location of a game entity being passed to the physics engine and to the player for keyboard input(gravity must be applied, force from other physics objects may be applied, and the player keyboard input all effect the final draw location of an in-game object). Of course their are cases where placing locks on certain members of that object are necessary, but the overall advantage I feel outweighs any syntax or non-obvious issues that may be encountered.

I would have just used 0, 1, 2, 3 for something like that (or better yet, an enum, but the basic principle is the same). Bonus because checking integers is much easier and faster than the alternatives.

Don't pay much attention to "the hedgehog" in my nick, it's just because "Sik" was already taken =/ By the way, Sik is pronounced like seek, not like sick.

I would have just used 0, 1, 2, 3 for something like that (or better yet, an enum, but the basic principle is the same). Bonus because checking integers is much easier and faster than the alternatives.

Yep. Actually I use two of them, eDir2d4 and eDir2d8... just make sure you keep track of which one your variable is. And even when I do need real angles, I prefer a 16-bit value instead of float/double in radians. 0x10000 is one full rotation, so it overflows and wraps back to 0 automatically. And you can reinterpret as signed 16 bit to get a -180 to +180 degree angle for situations like figuring whether it's quicker to turn left or right to point yourself toward something. And a quick bitshift of the unsigned 16 bit value converts to and from the eDir2d enums And you can use a 32-bit number if you do need to go over 360 degrees (e.g. a loop counter going from 0 to 360). Really convenient.

As for the original post of get/set madness, I agree with Hodgman that if you're giving out pointers to private variables, they should just be public. I have been known to write lots of get/set pairs though. Gets are generally "pure", just return a value or const reference to the variable. But sets sometimes do extra. And a lot of the time they're actually just pass-throughs to get/set functions of a member of the class. For example, in my 2D tiled game map editor, the MainWindow class has this, which I'm undecided whether it's a nightmare or not:

They're mostly there so the menu bar of the main window can call them when you switch on and off the visibility of all the things. And it does eliminate the need for the main window to give access to its sub-windows. But it's quite a wall of text.