Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Regarding:

Class Copier { Scanner scanner; Printer printer; function start() { printer.start(); } }

---

Placing a Start() in a PoweredDevice base class doesn't make sense in the real world. There are plenty of "powered devices" that don't have start buttons. A phone, a fish tank pump, a smoke alarm, none have a "start." A powered device should have just that, a PowerOn() and PowerOff() or SetPower(bool isOn). I wouldn't even create a PoweredDevice base class unless you have a reason. This is the main fault in your design.

Scanner.Start() should return a byte[] which is the result of the scan: byte[] Scanner.Start(); A scanner is an input device.

Printer.Start() should take an argument of byte[] as to what it is to print: void Printer.Start(byte[] byteArr); A printer is an output device.

Having said that, your Copier class would look like this:

  Class PoweredDevice
  {
     void SetPower(bool isOn)
     {
       ...
     }
     // Start() doesn't belong here.
  }

  Class Copier : PoweredDevice
  {

    Scanner scanner;
    Printer printer;

    void override SetPower(bool isOn) {
        printer.SetPower(isOn);
        scanner.SetPower(isOn);
        base.SetPower(isOn);
    }

    void Start()
    {
        byte[] document = scanner.Start();
        printer.Start(document);
    }
  }
This can easily be enhanced to handle copy counts:

    void Start()
    {
        byte[] document = scanner.Start();
        for (int x = 0; x < copyCount; x++)
            printer.Start(document);
    }
Ideally you wouldn't even make an inheritable Start() method. The Scanner class would have a byte[] Scan() method and the Printer class would have a Print(byte[] byteArr) method. You're trying to ram a square peg into a round hole. Use inheritance when it is convenient and makes sense to do so. Don't force it. Think, what does a scanner and a printer have in common that works the same, then put that in your base class. A power button is about it.

A lot of inheritance is done backwards. You make your classes then find commonalities and put that in your base class. Only create a base class first if you've thought about your object model and you know the commonalities.

Also, there is no reason to make your inheritance chain deep, just because. Build your objects in a way that makes sense. Don't write code or base objects you will never use. You can always insert a class in the chain when necessary.

Mastering OOP is hard, and people who have mastered it get paid a lot of money for their skill. It took me a few years to really understand how to design with it. It's invaluable though. A good object model is a thing of beauty, and a hell of a lot of fun to design.

Edit: I don't know why the editor won't keep the CR's.



Indent it with 4 spaces to make HN recognize it as a code block.


It's two spaces, but of course four works too.

https://news.ycombinator.com/formatdoc


Whoops. Too used to Markdown, I guess.


Thanks. I felt like Richard in the tab vs. space debate on Silicon Valley.




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: