The standard line length of code is 80 characters per line. This is accepted and followed by the most of programmers.

I working on a state machine of a character and is necessary for me follow this too.

I have four classes who pass this limit.

I can subclass each class in two more and then avoid the line length limit.

class Stand

class Walk

class Punch

class Crouch

The new classes would be StandLeft, StandRight and so on. Stand, Walk, Punch and Crouch would be then abstract classes.

The question if there is a limit for the long of the hierarchies tree or this is depends of the case.

  • 9
    The 80 characters per line is more of a guideline than a rule. Making a program more semantically complicated for the sole reason of making it easier to read would be counterproductive to someone's ability to understand it. Also, there isn't really anything like a hard and fast rule on inheritance depth, so you may want to ask/research something more along the lines of "Is it worth adding an extra abstraction to make my program easier to understand, and how can I make sure I'm really making it easier?" – Snagulus Oct 22 '13 at 21:08
  • 8
    Changing your design to accommodate an arbitrary formatting rule is... rage inducing. – Telastyn Oct 22 '13 at 21:29
  • Yeah, line length is pretty much the last thing to worry about, after all other considerations have been addressed. If you're regularly exceeding 80 characters per line, it could be a symptom of another problem, but it's not a problem all by itself. – Robert Harvey Oct 22 '13 at 22:01
  • sorry if i'm being dense - but how does subclassing reduce the number of characters per line? I can see that it would reduce the number of lines per class, but that's something else – HorusKol Oct 23 '13 at 1:02
  • @HorusKol Because in the first case (only with Stand, Walk, Punch and Crouch) when I check the collisions I have to ask if the character has moved to the right or to the left and then resolve the collisions (adding with this if more indentation and reducing the caracters per line left). In the second case (with StandLeft, StandRight, etc.) I don't have to check the side so I reduce my characters per line. – Super User Oct 23 '13 at 1:44

Subclasses and subtypes bring a lot of baggage with them in the form of inheritence and the desirabality of supporting the Liskov Substitution Principle. It would be pretty foolish to start creating subclasses simply to support an arbitrary naming convention.

It's unclear why you would need separate classes for what you are describing. How is StandLeft significantly different from StandRight? can you separate them and use composition instead? That is, the Stand class has an attribute called direction, which is in turn a Left or Right implementation of the IDirection interface:

class Stand {
    private IDirection direction;

The IDirection object would either define useful state information or a different strategy for how the Stand object should behave.

  • I resolve this looking the increment of the character (if is positive or negative) and then (for example in Walk) move the character to the left or to the right. I think this is the answer, because it's simple and doesn't add complexity creating more classes like Direction. What do you think? – Super User Oct 23 '13 at 1:45
  • Why would you need to check the direction and then move the character to the left or right? If you add a negative number, that should move the character left. If you add a positive number, that should move the character right. – Amy Blankenship Oct 23 '13 at 3:18
  • Because when my character go to the left I check his collisions with the another objects and his left side and viceversa. – Super User Oct 23 '13 at 4:01

As far as I'm aware, there's no hard limit in terms of class hierarchy as far as coding conventions or anything like that go, but you need to make sure making more subclasses and such is really the answer.

I would never recommend making more subclasses to help limit the number of characters per line. Usually you solve a problem like that just by concatenating strings, using more variables, etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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