2 Replies - 2896 Views - Last Post: 06 October 2012 - 11:17 AM

#1 FelipeBormann  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 19-September 12

Books and Reusable

Posted 02 October 2012 - 01:31 PM

Hi, I was programming these days and something "hit my mind", like, I was thinking how to do my code reusable?, in simple things? How a Sprite Class, animation Class. Someone know about some books which helps about xna programming? And how to improve your abstract thinking? (POO). Ty everyone.
Is This A Good Question/Topic? 0
  • +

Replies To: Books and Reusable

#2 BBeck  Icon User is offline

  • Here to help.
  • member icon

Reputation: 779
  • View blog
  • Posts: 1,871
  • Joined: 24-April 12

Re: Books and Reusable

Posted 06 October 2012 - 08:22 AM

I think LordOfDuct calls that "design patterns":


I looked on Amazon and only found a couple of books on the subject. A lot of the books were pretty old although I don't know how much has changed over the years. Most of them seem to be written for Java, but I think it's close enough to C# that it's still basically the same.

I think I may buy this one:

I also saw one on Object Oriented Analysis and Design.

But before you go that route, I would find books on C# and learn C# and Object Oriented Programming first.

There are a lot of things for code organization that come before Design Patterns. For example, your naming conventions. What name do you give to fields, methods, and classes? Choosing good names is very important in terms of making the code clear to yourself and to others and also helping you find things. A name should be self explanatory. I should expect an "enemy" class to represent pretty much everything there is about a game enemy. And I should expect it's Draw method to handle everything needed to draw it to the screen. But who knows what the D method of the E class does.

I was also taught that you should never have more than a page of code in a method (I was actually taught that with functions and procedures, but it's basically the same thing). If all the code for your method doesn't fit on one screen, it probably needs to be broken up into multiple sub-methods. I let myself break this rule sometimes, but I try pretty hard to not let it get longer than 2 pages of code. After one page of code I start looking for code that belongs in it's own method.

Commenting is super important and often neglected. I have to admit that when I'm programming professionally I'm really bad about doing this. I get into writing code and just don't think about commenting.

When I write for tutorials and such, I make myself go back and comment after I'm done. Personally, I like to put a header in front of every method. People used to do this all the time. Now days I hardly ever see anyone do it at all. I'm not sure why that is really.

In this thread I posted code where most of the methods have headers:

You can see what I'm talking about from that. Here's an example of a header:

Note:If you're new here, when you hover the mouse over the top of the code and click on the "View Source" button it makes the source code easier to read.
 public void MoveToPositionOnPath(ref Vector2 Position, ref int NextPathPoint, ref Vector2 Orientation, float Distance, bool Forward)
        //  MoveToPositionOnPath()
        //  Parameters: 
        //      Position - Screen position of the object represented as a 2D vector. Altered by this method.
        //      NextPathPoint - Points to the waypoint on the path that the object will be moving towards. Altered by this method. 
        //      Orientation - Normalized (we hope) vector that points in the direction the ship faces. Altered by this method.
        //      Distance - Distance the object will move this frame regardless of distance between waypoints.
        //      Forward - True if motion is clockwise, and false for counter-clockwise.
        //  Returns: None.
        //  Purpose: Moves the object defined by the Position towards the next waypoint that can be reached, by traveling through
        //      all intermediary waypoints, by the given distance.
        //  Explanation:
        //       The object's current position is passed in. The method starts by looking at the waypoint specified by NextPathPoint.
        //  NextPathPoint is maintained by the external program. It starts out set to the first waypoint, and then this method
        //  updates the external value to let the external program know what the correct next waypoint will be. It should be noted
        //  that the next waypoint is not simply incremented, but that many waypoints may be passed in determining the next waypoint.
        //      The method updates the position to move towards the next waypoint, and if the next waypoint can be reached without
        //  exceeding the specified distance, it will go on and do the same for the waypoint after that, and so on and so forth. Once
        //  it reaches a point inbetween waypoints where the distance runs out, it will set the final position of the object to that
        //  spot. It will output the number of the next waypoint (NextPathPoint). Then it will calculate a direction vector to point to 
        //  the direction to that next waypoint. 
        //      The object following the path could be anything. This method just treats the object according to it's position, orientation,
        //  and it's next waypoint. The next waypoint is calculated here and sent to the external program to be maintained and then
        //  it will be passed in again the next time, updated, and returned again.
            if (Distance > 0f)  //Don't try and move the object if the distance to be moved is zero. It will destroy the vectors, if you do.
                Vector2 PathToNextWaypoint;     //Used to track direction to and distance of the next waypoint.
                float DistanceToNextWayPoint = 0f;  //Info contained in PathToNextWaypoint but made redundant for clarity.
                int NumberOfPathPoints = PathPoints.Count();

It's all just comments between the declaration of the method and the body of the method. Different people like to put different things in the header. For example, some people like to put dates on when the code was modified and by whom. That's more useful if you're not the only one working on the code.

I try to explain any input into the method as well as any output from the method. I like to give a one sentence quick description. And then I like to include notes that go into detail to explain what happens inside this method.

Mainly, I'm just trying to write a header that I can look at quickly and know what that method does. If I come back to the code in 2 months, I won't remember what my own code does 2 months later. Using good names and keeping methods short helps. And comments on most every line of code helps too. But I would rather just read a header that tells me what the method does then trying to re-read all my code line by line with its comments to try and re-remember what it does. A quick header tells me all I need to know about the method without even looking at a single line of code in the method.

Anyway, those are some things I use. And then there is just Object Oriented Programming in general, which you should learn from C# books. And then after that it's probably Design Patterns.

Most of the XNA books are pretty good. I own just about all of them for XNA 3.0 and 4.0. Really, a lot of the 3.0 books are a whole lot better. That's probably because there are a lot fewer 4.0 books. However, the Draw method was completely changed between 3.0 and 4.0. So, they are not cross compatible. You have to learn to convert them. Either that or use XNA 3 for the 3.0 examples and XNA 4 for the 4.0 examples. You can still download both for free.

Do not get these books:
Only covers very specific topics that you won't need until you're pretty good with XNA

Covers some really great stuff, but it's all HLSL and very advanced XNA. Definitely not the place to start.

This post has been edited by BBeck: 06 October 2012 - 08:27 AM

Was This Post Helpful? 0
  • +
  • -

#3 FelipeBormann  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 19-September 12

Re: Books and Reusable

Posted 06 October 2012 - 11:17 AM

ty for the whole help, I know about C# and XNA(like lambda expression,POO and that things) I was just asking to do the things in a best way : ). The only thing what I need study yet is serialization and networking, but I'm worried about getting a better way to code. Ty for everything, I use the cookbook, I won't program to 3D yet, for me , is something little hard.
I'll try come into in the 3D world, I think is really hard but I need come in. I'm young but I'm really exciting with everything , like, is good, anyway,sorry for don't know how to express myself.

Something what I really want is how design my codes to make they good for other people read it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1