Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


My ever-evolving coding style

  • You cannot reply to this topic
14 replies to this topic

#1 fastcall22   Crossbones+   -  Reputation: 4454

Like
3Likes
Like

Posted 15 May 2013 - 07:04 PM

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:
class CFoobar
{
private:
	// Public member variables are bad, mmkay?
	int m_iFoo;
	RGB m_rgbBar;
	double m_dBaz;
	
public:
	// Encapsulation is good, mmkay?
	// I'm so clever, using a pointer to avoid both a get AND a set for each member variable!
        int *GetFoo() { return &m_iFoo; }
	RGB *GetBar() { return &m_rgbBar; }
	// (Repeat for every member of CFoobar)
};


void frobnicate()
{
	CFoobar *foo = new CFoobar();
	
	// Wow! This is much easer than writing foo->setFoo(foo->getFoo()+7);
	*foo->GetFoo() = *foo->GetFoo()+7;

	double thing = *foo->GetBaz();
	// etc...
	
	delete foo;
}
Yup. Complete with the Microsoft bastardization of the Hungarian notation and the ever-so-important C-prefix on the class names so you always when you're using a class and when you're using a struct.



Guuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu.
c3RhdGljIGNoYXIgeW91cl9tb21bMVVMTCA8PCA2NF07CnNwcmludGYoeW91cl9tb20sICJpcyBmYXQiKTs=

Sponsor:

#2 Slig Commando   Members   -  Reputation: 334

Like
1Likes
Like

Posted 15 May 2013 - 08:22 PM

void frobnicate()
{
    CFoobar *foo = new CFoobar();
    
    //Fixed
    int* var = foo->GetFoo();
    *var += 7;

    double thing = *foo->GetBaz();
    // etc...
    
    delete foo;
}

 

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


Edited by Slig Commando, 15 May 2013 - 08:24 PM.


#3 Cornstalks   Crossbones+   -  Reputation: 6991

Like
1Likes
Like

Posted 15 May 2013 - 08:40 PM

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.


Edited by Cornstalks, 15 May 2013 - 08:42 PM.

[ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

#4 Hodgman   Moderators   -  Reputation: 31785

Like
2Likes
Like

Posted 15 May 2013 - 09:06 PM

Honestly I like returning pointers for most cases

I think the point is that his 'getters' are useless, because his code is equivalent to as if the variables were just public...

It's just a really bloated way of writing this:

struct Foobar
{
    int foo;
    RGB bar;
    double baz;
};
void frobnicate()
{
    Foobar foo;
    foo->foo = foo->foo+7;
    double thing = foo->baz;
}


#5 Trienco   Crossbones+   -  Reputation: 2223

Like
1Likes
Like

Posted 15 May 2013 - 09:51 PM

Whenever writing a million getters and setters gets annoying, I usually ask myself two things:

 

a) if I just go and write trivial set/get methods for everything, why not just stop pretending and make it public?

 

b) why not be lazy and wrap it in a template that handles it?

 

class ...

{

...

   Property<int> stuff;

};

 

int value = obj.stuff();

obj.stuff(5);

obj.stuff( obj.stuff() + 5 );

 

Not calling it get and set allows to use the template for all trivial cases and do custom implementations where needed without requiring a different syntax.

 

Still, if a class has a dozen members and trivial accessors for all of them, encapsulation probably just went out the window. So it's more of a convenient way to remain consistent.


f@dzhttp://festini.device-zero.de

#6 Bacterius   Crossbones+   -  Reputation: 9262

Like
0Likes
Like

Posted 16 May 2013 - 01:26 AM

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):

 

interface

...

private
  fFoo: Integer;
  fBar: String;

public
  property Foo: Integer read fFoo; // read-only, default getter (no boilerplate getter method)
  property Bar: String read getBar write setBar; // read-write, custom getter/setters

...

implementation

function getBar: String;
begin
  if (fBar <> "sentinel") then Result := fBar;
end;

procedure setBar(Value: String);
begin
  if (Value <> "sentinel") then fBar := Value;
end;

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.

 

- Pessimal Algorithms and Simplexity Analysis


#7 Krohm   Crossbones+   -  Reputation: 3245

Like
0Likes
Like

Posted 17 May 2013 - 01:44 AM

Today, my subconcious decided to be helpful and torture me with randomly fetched memories of my old coding style

I wonder what you're trying to tell yourself by self-inflicting this torture. Thanks god those things are gone! tongue.png

#8 Aurioch   Crossbones+   -  Reputation: 1304

Like
0Likes
Like

Posted 17 May 2013 - 06:03 AM

Oh god, when I decided to check my old Battle City code (from about 9 months ago) and found stuff like this:

 

namespace BattleTanks
{
    class Tank
    {
        #region Variables
        public void LoadContent(ContentManager content, string TankAssetName, string BulletAssetName) { ... }
        public void Draw(SpriteBatch spriteBatch) { ... }
        public void Initialize(int X, int Y, float rotation, float scaling, int speed, bool playerbullet) { ... }
        public void MoveTank(bool HorAxis, int direction, bool cancel) { ... }
        public void RemoveTank(TimeSpan time) { ... }
        public void MoveBullet() { ... }
        public void LaunchBullet() { ... }
        public string BulletDirection() { ... }
        public void RemoveBullet() { ... }
        public void RotateAI(out bool HorAxis, out int direction, out string text) { ... }
        public void FireAI() { ... }
        public void TurnAI(string direction) { ... }
    }
}

And I was happy at that time, especially because it worked. Try to guess what is hidden inside some of the methods.

 

2 months later I open the same code out of curiosity... Reaction: "What in the mother of f- was I doing back then >.<"

 

And for some reason I'm pretty sure that I'll say the same for my current code after 1 year.



#9 Sik_the_hedgehog   Crossbones+   -  Reputation: 1833

Like
0Likes
Like

Posted 18 May 2013 - 01:33 PM

Try to guess what is hidden inside some of the methods.

  • 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.

#10 Bacterius   Crossbones+   -  Reputation: 9262

Like
1Likes
Like

Posted 18 May 2013 - 01:38 PM

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.

 

- Pessimal Algorithms and Simplexity Analysis


#11 Servant of the Lord   Crossbones+   -  Reputation: 20946

Like
3Likes
Like

Posted 18 May 2013 - 01:58 PM

tank.FireProjectile(Direction("North-westerly"));

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


#12 Aurioch   Crossbones+   -  Reputation: 1304

Like
0Likes
Like

Posted 18 May 2013 - 02:40 PM

Try to guess what is hidden inside some of the methods.

  • 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 sleep.png'

How much of that was wrong?

 

Comments added in quote with blue font.

 

Regarding angles and strings - it was for convenience; movement is restricted to 4 angles (north, south, west, east) so it's easier to perform direction checks with strings instead of writing Math.PI * (0, 0.5, 1, 1.5).

 

Don't worry, I'm using constants now. Not to mention that I splitted "TankBulletAI" class into 3 separate ones, each one tracking only what it's SUPPOSED to track.

 

Oh yeah, everything was also stored in arrays instead of lists. And I was wondering why did game crash when only 2 enemies remained (out of 30) in order to finish the level...



#13 Slig Commando   Members   -  Reputation: 334

Like
0Likes
Like

Posted 19 May 2013 - 05:30 PM

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.



#14 Sik_the_hedgehog   Crossbones+   -  Reputation: 1833

Like
1Likes
Like

Posted 20 May 2013 - 10:54 AM

Respawn timing.

...suddenly I feel like an idiot for not realizing that one (sorta dumb though when you consider it's probably the same time always).

 

Regarding angles and strings - it was for convenience; movement is restricted to 4 angles (north, south, west, east) so it's easier to perform direction checks with strings instead of writing Math.PI * (0, 0.5, 1, 1.5).

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.

#15 DekuTree64   Members   -  Reputation: 986

Like
0Likes
Like

Posted 10 June 2013 - 12:39 AM

Regarding angles and strings - it was for convenience; movement is restricted to 4 angles (north, south, west, east) so it's easier to perform direction checks with strings instead of writing Math.PI * (0, 0.5, 1, 1.5).

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 smile.png 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:

    bool isMapListWindowVisible() const { return mMapListWindow->isVisible(); }

    bool isTilesetWindowVisible() const { return mTilesetWindow->isVisible(); }

    bool isSpriteWindowVisible() const { return mSpriteWindow->isVisible(); }

    bool isLayerWindowVisible() const { return mLayerWindow->isVisible(); }

    bool isGameViewVisible() const { return mMapEditorWindow->isGameViewVisible(); }

    bool getSpriteMarkersVisible() const { return mMapEditorWindow->getSpriteMarkersVisible(); }

    bool getFrontLayersTransparent() const { return mMapEditorWindow->getFrontLayersTransparent(); }

    void setMapListWindowVisible(bool visible) { mMapListWindow->setVisible(visible); resizeRedraw(); }

    void setTilesetWindowVisible(bool visible) { mSpriteWindow->setVisible(false); mTilesetWindow->setVisible(visible); resizeRedraw(); }

    void setSpriteWindowVisible(bool visible) { mTilesetWindow->setVisible(false); mSpriteWindow->setVisible(visible); resizeRedraw(); }

    void setLayerWindowVisible(bool visible) { mLayerWindow->setVisible(visible); resizeRedraw(); }

    void setGameViewVisible(bool visible) { mMapEditorWindow->setGameViewVisible(visible); }

    void setSpriteMarkersVisible(bool visible) { mMapEditorWindow->setSpriteMarkersVisible(visible); }

    void setFrontLayersTransparent(bool transparent) { mMapEditorWindow->setFrontLayersTransparent(transparent); }

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.







PARTNERS