Jump to content
  • Advertisement
Sign in to follow this  
Synex

[.net] How do YOU code?

This topic is 4838 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm in the middle of my first BIIIIG C# project, porting over a lot of my old game-engine code to C#, making it all OOP and lovely. Now, in good old C++, you'd end up having a million get/set functions for each of your variables, but good old C# now lets you have properties! Also, theres a million different ways of naming your functions. I was just wondering what everyone else does... C++ Code
class FooBar
{
   private int d_height;
   private int d_width;

   public void setHeight(int height) { d_height = height; };
   public int getHeight() { return d_height; };
   public void setWidth(int width) { d_width = width; };
   public int getWidth()  { return d_width; };
}

USAGE:

Foobar temp = new Foobar();
temp.setHeight(250);
temp.setWidth(250);

Type 1 C# Code - The Encapsulated setData
class FooBar
{
   public struct FooBarSize
   {
      int Height;
      int Width;
   }

   private int d_height;
   private int d_width;

   public FooBarSize Size
   {
      get
      {
          FooBarSize _size = new FooBarSize;
          _size.Height = d_height;
          _size.Width = d_width;
          return _size;
      }
      set
      {
          d_height = value.Height;
          d_width = value.Width;
      }
   }
}

USAGE:

FooBar temp = new FooBar();
FooBar.FooBarSize tempSize = FooBar.FooBarSize();
tempSize.Width = 250;
tempSize.Height = 250;
temp.Size = tempSize;

C# Type 2 - The setSize function
USAGE:

FooBar temp = new FooBar();
temp.SetSize(250, 250);

That is a quick couple of ways I could think of - which way do you do it? Using the old "class.setSize(x, y)" is a bit C++'ish, but starting writing structs for every kind of data type is a bit of a pain, as is seperating "Width" and "Height" into two seperate properties... Just my 2 cents

Share this post


Link to post
Share on other sites
Advertisement
First of all, in C++ I never had a whole bunch of accessor methods. The entire point of OOP is that you shouldn't need a whole lot of accessor methods. A class does more than just encapsulate the data, it encapsulates how the data is used.

If you have a lot of accessor methods then you might want to see how much of that actually needs to be exposed. A careful evaluation of how the data is being accessed and used will reveal algorithms that would be better served as members of the class encapsulating the data.

Secondly, your example makes no sense to me. If you are going to return the structure, then store it in the structure. You don't need to convert to the structure just to return the data. It also means you can use the size structure to encapsulate access to the width and height, thus enabling constraints to be applied.

Share this post


Link to post
Share on other sites
Ok, that makes some sense, but the idea behind OOP is that you keep as much as you can private, and access the data through accessor functions, otherwise you might as well just make a whole load of variables in a class as public right?

Share this post


Link to post
Share on other sites
Define accessor?
If you mean get/set functions then no. OOP is not about get/set functions. They are a necessary evil however, since often times you do need to access the data that a class provides. OOP is about abstractions. Data-hiding is one technique used to achieve this, but if you expose that data via get/set methods, then you have not gained anything. Instead the object should encapsulate the access to the data by exposing algorithms to operate on the data.

Share this post


Link to post
Share on other sites
Get/Set functions that just return a member variable are useless and just take more time to type if you ask me (Then again I am an idiot).
However I use get/set if I want the user to only access part of something, for example:

class A
{
vector<int> foo;
public:
int GetSize()
{
return foo.size();
}
}

Share this post


Link to post
Share on other sites
Quote:
Original post by Ainokea
(Then again I am an idiot).

No, that would be smart_idiot.

Share this post


Link to post
Share on other sites
What do I do?

Before writing any code, I decide on the interface of the classes I use (what I need to do with them), write that interface down, and go do something else (like have dinner). I force myself not to think of the internal properties of the object, or anything related to its implementation.

When I come back, I implement the interface without adding new members to it.

You'll find that quite often, after a bit of training, in the first step of this method, you think in terms of "what can I do with it", not "what is inside it". You can end up in functions that look like accessors: Resize instead of SetSize, for instance. But by thinking in terms of actions instead of properties, you end up naming your function as actions that are a little bit more deep than "Get the value, set the value", and also make the interface more consistent.

Share this post


Link to post
Share on other sites
if i notise i need to use get and set funktions i just make the variable public instead. easyer quicker. Might be dangerus but my projekt is small so far.
Largest is i think 13k lines.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
If I am adding a set/get member I ask my self a couple of questions:
1. Is it possible to access this object from multiple threads?
2. Should the caller know the internal storage structure of the object?
3. Is this a singleton holding Configuration or Resource information for the whole applications?

If I answer no to all 3 of these questions, I need to re-examine why the data member is not public instead of requiring a function member to access.

Share this post


Link to post
Share on other sites
For naming your functions and variables, searching for 'Design Guidelines for Class Library Developers' in your .NET Framework SDK docs is good.
Just a few excerpts:

Method/class/property/interface/enum etc names, pascal case.
Parameter names, camel case.

You can really do whatever you like with your internal variables since they are not exposed, but I use 'm_' prefix for class members and camel case for variables in functions. I don't like the style where some people use plain camel notation for class members because it promotes confusion with function parameters (at least for me [grin])

Just my humble opinion!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!