• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Mikea15

Constructor Chaining Order to Parameters or Parameterless.

8 posts in this topic

Hello.

 

So I've been dwelling on this topic today. Which is, which way do you want to chain your constructor.

 

Say you have a basic BaseMap class.

public class BaseMap
{
    protected int _height;
    protected int _width;

    protected Node[,] _map;

    public BaseMap()
    {
        _width = 0;
        _height = 0;
        _map = null;
    }

    public BaseMap(int width, int height)
    {
        _width = width;
        _height = height;
        _map = new Node[_width, _height];

        for (int x = 0; x < _width; x++)
        {
            for (int y = 0; y < _height; y++)
            {
                _map[x, y] = new Node(x, y);
            }
        }
    }
}

Now my question is, how you chain the constructors? So far, I've been chaining them this way:

public class BaseMap
{
    protected int _height;
    protected int _width;

    protected Node[,] _map;

    public BaseMap()
    {
        _width = 0;
        _height = 0;
        _map = null;
    }

    public BaseMap(int width, int height) 
        : this( )
    {
        _width = width;
        _height = height;
        _map = new Node[_width, _height];

        for (int x = 0; x < _width; x++)
        {
            for (int y = 0; y < _height; y++)
            {
                _map[x, y] = new Node(x, y);
            }
        }
    }
}

But I've searched online and found people where chaining the other way around. Like this:

public class BaseMap
{
    protected int _height;
    protected int _width;

    protected Node[,] _map;

    public BaseMap()
        : this( 10, 10 ) // default values
    {

    }

    public BaseMap(int width, int height) 
    {
        _width = width;
        _height = height;
        _map = new Node[_width, _height];

        for (int x = 0; x < _width; x++)
        {
            for (int y = 0; y < _height; y++)
            {
                _map[x, y] = new Node(x, y);
            }
        }
    }
}

So, from what I understand, people are instantiating with default values, when calling the default constructor, leaving it empty. My Approach was to start with the constructors with parameters, and call this( ) to instantiate variables, before getting into the parameter constructor's body. This particular example isn't the most obvious tho, but say you have a node with edges.

public Node( )
{
	Adjacents = new List<Node>();
	State = EnumNodeState.UNKNOWN;
}

public Node( int x, int y )
	: this( )
{
	PosX = x;
	PosY = y;
}

This makes sense, right? I instantiate the List and state, then define position, when calling Node( 10 , 10 );
Is it 'better' to do it the other way around?

 

Thanks. smile.png

0

Share this post


Link to post
Share on other sites

By the end of construction your object should be left in a valid state. You also want to reduce/eliminate code duplication. If you can't do this by chaining constructors then consider factoring out the common code into a separate method and calling that from within your constructor.

2

Share this post


Link to post
Share on other sites

By the end of construction your object should be left in a valid state. You also want to reduce/eliminate code duplication. If you can't do this by chaining constructors then consider factoring out the common code into a separate method and calling that from within your constructor.

 

Exactly.  Choose whichever method makes sense for the given class.

1

Share this post


Link to post
Share on other sites

Okay, so there is no "best way" to do it. That's what I wanted to know. smile.png

 

Thanks for the feedback. smile.png

 

Edit:

To get a better view. Here's the main difference with another class.

public X( )
{
    Left = 0;
    Right = 0;
    BreakLeft = false;
    BreakRight = false;
    BreakTime = 0;
}

public X(int speed)
    : this( )
{
    Left = speed;
    Right = speed;
}

public X(int left, int right)
    : this( )
{
    Left = left;
    Right = right;
}

public X(int left, int right, int time, bool breakWheel)
{
    Left = left;
    Right = right;

    BreakLeft = breakWheel;
    BreakRight = breakWheel;

    BreakTime = time;
}

public X(int left, int right, int time, bool breakLeft, bool breakRight)
{
    Left = left;
    Right = right;

    BreakLeft = breakLeft;
    BreakRight = breakRight;

    BreakTime = time;
}

And Method 2:

public X( )
    : this( 0 )
{

}

public X(int speed)
    : this( speed, speed )
{
    
}

public X(int left, int right)
    : this( left, right, 0, false )
{

}

public X(int left, int right, int time, bool breakWheel)
    : this( left, right, time, breakWheel, breakWheel )
{

}

public X(int left, int right, int time, bool breakLeft, bool breakRight)
{
    Left = left;
    Right = right;

    BreakLeft = breakLeft;
    BreakRight = breakRight;

    BreakTime = time;
}

For this particular case. This is the better approach. It has less code, Its easier to maintain and is cleaner. smile.png

Edited by Mikea15
1

Share this post


Link to post
Share on other sites

Okay, so there is no "best way" to do it. That's what I wanted to know. smile.png

 

Thanks for the feedback. smile.png

 

Edit:

To get a better view. Here's the main difference with another class.

public X( )
{
    Left = 0;
    Right = 0;
    BreakLeft = false;
    BreakRight = false;
    BreakTime = 0;
}

public X(int speed)
    : this( )
{
    Left = speed;
    Right = speed;
}

public X(int left, int right)
    : this( )
{
    Left = left;
    Right = right;
}

public X(int left, int right, int time, bool breakWheel)
{
    Left = left;
    Right = right;

    BreakLeft = breakWheel;
    BreakRight = breakWheel;

    BreakTime = time;
}

public X(int left, int right, int time, bool breakLeft, bool breakRight)
{
    Left = left;
    Right = right;

    BreakLeft = breakLeft;
    BreakRight = breakRight;

    BreakTime = time;
}

And Method 2:

public WiGoSpeed( )
    : this( 0 )
{

}

public X(int speed)
    : this( speed, speed )
{
    
}

public X(int left, int right)
    : this( left, right, 0, false )
{

}

public X(int left, int right, int time, bool breakWheel)
    : this( left, right, time, breakWheel, breakWheel )
{

}

public X(int left, int right, int time, bool breakLeft, bool breakRight)
{
    Left = left;
    Right = right;

    BreakLeft = breakLeft;
    BreakRight = breakRight;

    BreakTime = time;
}

For this particular case. This is the better approach. It has less code, Its easier to maintain and is cleaner. smile.png

 

In that case I would avoid the multi-chaining since it's hard to follow what the end result of construction will be. Rather call you 'main' constructor, i.e. the one doing all the work, directly from the others and make it explicit what each parameter is being set to.

 

Like so:

public WiGoSpeed( )
    : this( 0 )
{

}

public X(int speed)
    : this( speed, speed, 0, false, false )
{
    
}

public X(int left, int right)
    : this( left, right, 0, false, false )
{

}

public X(int left, int right, int time, bool breakWheel)
    : this( left, right, time, breakWheel, breakWheel )
{

}

public X(int left, int right, int time, bool breakLeft, bool breakRight)
{
    Left = left;
    Right = right;

    BreakLeft = breakLeft;
    BreakRight = breakRight;

    BreakTime = time;
}
1

Share this post


Link to post
Share on other sites

So I guess, I would have to re-think on how much control those classes provide. Thank you :)

1

Share this post


Link to post
Share on other sites

There are a few other options to consider.
 
You could use a combination of default values and named parameters:

public class Test
{
    private readonly int _x;
    private readonly int _y;
    private readonly int _z;
    private readonly string _name;
    private readonly double _speed;

    public Test(int x = 0, 
        int y = 0, 
        int z = 0, 
        string name = null, 
        double speed = 0.0)
    {
        _x = x;
        _y = y;
        _z = z;
        _name = name;
        _speed = speed;
    }
}

var t = new Test(y: 3, name: "bob", speed: 66.6);

pros: one constructor, sensible defaults defined in one place, user can choose what params they need to set
cons: easier for user to set some incompatible set of parameters, optional argument change issue (in practice this is pretty rare)
 
Another option (similar to above) is to make the values properties and use a property initialiser

public class Test
{
    public int X {get;set;}
    public int Y {get;set;}
    public int Z {get;set;}
    public string Name {get;set;}
    public double Speed {get;set;}
}

var t = new Test
{
    X = 3,
    Name = "Alice",
    Z = 4
};

pros: minimal code, easy for user of class to set values
cons: no defaults (although you could modify the properties to be backing props with sensible defaults), same issue with setting incompatible set of default params

 

 

I'm not advocating any of these choices, merely presenting them as options. Each has it's pros and cons, but ultimately, the choice of initialisation method is dependant on the class you are creating and the invariants you need to preserve.

2

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0