Skip to main content

Is it bad to declare an empty class?


In my spare time, I am building a Sudoku solver to try to get the hang of OOP in PHP. A Sudoku puzzle, for those of you who don't know, is in its most common form a 9x9 matrix of numbers from 1 to 9, with 3x3 squares delineated in a tic-tac-toe like pattern. Some numbers are filled in in advance. The goal of the puzzle is to fill in the remaining numbers, so that no row, column or 3x3 square contains the same number more than once.



To do this, I made a number of classes. A Cell can be an element of a Constraint , which are the rows, columns, and 3x3 squares. A Sudoku is a collection of Constraint s and Cell s. I have a SudokuSolver class which dynamically includes source files with SolverHelper subclass class declarations, and instantiates one of each subclass. A helper has a Solve() function that takes a Sudoku as an argument. It examines the Constraint s and asks its cells to eliminate value possibilities based on what it finds. The program itself just loops over the helpers until none of them report that they were able to eliminate any possibilities anymore.



But the fact that all cells in a row or column line up, has certain corollaries that are taken advantage of in certain solution techniques. So I need to differentiate between rows/columns, and other Constraint s. I could have the rows and columns in different arrays, which is not a bad solution. This has the advantage of allowing a good optimization opportunity: no column ever intersects another, for instance. I could also add a boolean property IsLinear .



Or, and now we get to my question: I could subclass the Constraint class to have a LinearConstraint . But that class would be empty. It would not need to override anything in the Constraint class. It would be a pair of curly braces, and that's it; a LinearConstraint object is special by virtue of being an instance of its class. If I wanted or needed to have special code that pertains to linear constraints, I could always add it. My question is: is the fact that I'm considering declaring and using an empty class, a sign that I'm doing something wrong? Am I being too abstract and theoretical about this?


Source: Tips4allCCNA FINAL EXAM

Comments

  1. An empty class is not at all bad. People use that in various other scenarios; especially in Exception specialisation.

    I do not believe that it will be any worse than adding a flag into the Constraint class, to differentiate the two types. I would much rather prefer the extended class, for at least the sake of code readability.

    ReplyDelete
  2. I may be misunderstanding the question, so bear with me if I am! From my understanding you wish to have different types of Constriant classes so that the Solve() function will act differently depending on the type of grid? (For optimization reasons, presumably).

    Extending the Constraint class in this way would certainly be one way of distinguishing the types, and I don't think is particularly bad practice.

    You may however wish to in future have even more different types of Constraint (I dunno, say you went nuts and decided to do 3D sudoku or something). In this case it may be better to turn the original Constraint class into an abstract class and extend that. You could set up how ever many subconstraints you wished, and they would all have the same functionality (plus any added functions unique to that constraint).

    This way you could set up your empty LinearConstraint subclass and not have to worry about shuffling everything around again when you decide to have even more Constraint subclasses.

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.