Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I get stuck each time I need to write a constructor for any class I design. The reason is that I am not sure what should go into a constructor and what should not.

On googling for this, I got the answer that the constructor need to do only as much as to make sure that the opbect created is valid. But my query is

How do you know if an object is in the valid state?

My class represents a chess board:

public class ChessBoard
    List<ICellRow> lstRow;
    int cellOffset;

    //Other function

    public ChessBoard()
        //What should go in here?

How do you decide when a class has the minimum information to be in the valid state? In this specific example, makes the board class valid?

share|improve this question
Write your constructor last. No logic should ever be in it so the entire objects behavior may be written without it, plus you won't know precisely what constitutes a valid state in your object until it is written. (unless you use TDD) – Jimmy Hoffa Feb 8 '13 at 14:01
@JimmyHoffa, You sure have valid points here. Writing constructor last will definitely help! – TheSilverBullet Feb 11 '13 at 6:00
up vote 4 down vote accepted

Think of an object as a value, just like a number or a string. That value represents a concept in the problem you’re trying to solve. For example, you might use an int to denote a distance in miles, or a number of points, or a set of Boolean flags. The value has no meaning on its own, but it does have an interpretation.

Whenever you create a value, you want to make sure its interpretation makes sense. It should be valid by construction—3 is a valid number, and “hello” is a valid string. In particular, there is no way to construct a string with a value that isn’t a string.

In addition, operations on values should be designed in such a way that every operation leads to a valid internal state, or else reports an error—whether at compile time through static typing, or at runtime through exceptions. Division by zero, for example, is not allowed on integers.

What this means for you is that the constructor of your ChessBoard class should produce a legal board state according to the game rules. For example, you could use the constructor to set the initial configuration of pieces for the start of a game. Operations on the board should then only allow you to move to other legal game states. By thinking along these lines, you can ensure that your ChessBoard accurately represents how a game of chess works in real life.

share|improve this answer
Thanks, Jon. The string and int example that you have given is really helpful! Finally I get the inkling of what people mean by a "valid" state. There is one question on the legality of the ChessBoard class. Suppose the design is such that there is a class Layout which infact determines the piece distribution on the board, then how do you include this to specify the legality of the ChessBoard class? Do you say that the valid state of ChessBoard also includes a valid Layout class? Do you initialize the Layout class from the ChessBoard class? – TheSilverBullet Feb 8 '13 at 7:28
@TheSilverBullet: Yes, just follow the same logic all the way through. You can usually come up with many different data representations for a problem, because they’re just abstract representations. In real life, a chess board doesn’t know anything about chess, and it doesn’t own the chess pieces. So your data representation should just be a convenient way to think and compute things about the problem. – Jon Purdy Feb 8 '13 at 7:31
And, this helps me out of my design-block! Thanks again Jon. Marking this as the answer! – TheSilverBullet Feb 8 '13 at 11:02

The constructor should also make sure any structures (colloeections for example) are initialized according to your conventions.

For example, if you expect a method or a property to always return a non-null collection (even in the case there were never any items added to it) the constructor should initialize the collection. That way any calls to methods or properties dependent on the collection will be able to rely on a valid collection.

share|improve this answer

The purpose of the constructor is for you to setup the class into a user friendly default state.

E.g. If the chessboard was a graphical object for a video game then it would be reasonable to expect that the colors of the tiles would be defaulted to black and white so as to take some burden away from the programmer having to manually do this using extra function calls.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.