Ideck Xyz Homework

Description

Review of object-oriented skills with War game - Code Review Stack Exchange

This particular sample is the card game 'War'. The interviewer off-handedly asked me: "what if I wanted to play War with an Uno deck of cards?" I threw an Uno deck implementation in there as well, so don't be confused.

I have been told that my main() function is too procedural, and also I have been told that the output logic should be separated. Do you agree with these assessments? Do you see anything else that will make me a better coder? Also, am I botching the factory pattern?

Here is some of the code that I would like to have reviewed:

Here are my interfaces:

Here is an abstract deck of cards:

And here is a standard deck of cards extending the abstract deck:

asked Aug 16 '13 at 22:23

Jamal ♦ 29k ● 10 ● 109 ● 219

Erm, right. I don't mean to be rude, but OO in PHP isn't too different from OO in any other language. The SOLID principles apply there, too. You seem to be writing code as if you had a phobia of all things SOLID stands for. Is the main method too procedural. having a method called main is ok. What you're doing in the main method is too procedural. Do you have to separate the output from the logic. Of course! Why would you bother with OO, if you don't separate things, you could just as well write one class, create an instance and use that class' scope instead of the global scope. But why bother with that class in the first place? Are you botching the factory pattern? You're using the factory pattern?. (yes, I'm afraid you are)

But let's start with something basic, yet important: Even though PHP isn't standardized just yet, there is an unofficial coding standard, which can be found here. All major players (Zend included) subscribe to this standard, as should you. Classes start with an Upper-Case, yours don't. Fix that. But that's just a cosmetic issue.

Factory : You have an awful lot of static 's in your code. I'm editing this answer, because I just noticed that this is to implement the factory pattern. Don't. Last time I applied for a job as PHP dev, I actually got a high-five, because I set off on a rant about why statics are, essentially, as bad as using eval or global. They have their use-cases, but in PHP, I've only ever really needed them 2, or 3 times in the past 5 years. Tops. Read about the D in SOLID, and learn to write tests. You'll soon find yourself hating statics and singletons as much as the next man. Even though a Factory can be handy (as can a Registry ). They're really just globals in drag.

Main: For some reason, you also define a public function main in an object. I can understand where that might come from. Other languages (Java, C, C++, Python. ) require a main or __main__ function to be defined somewhere. The thing is: these are other languages. It's a bit like using a double negative, because some languages use double negation (Afrikaans, French). Just because some of the more popular languages require a main function, doesn't mean that all languages need this. Having a main function isn't all that bad, nor is it "too procedural". It's what you're doing in that method that is just not OO at all.

In OOP, a class represents a single entity, and therefore, it should have one (and only one) task. A class can be responsible for rendering output, or interacting with the database, or processing the (raw) request data. That's all fine, but what a single class can't do is handle the response and insert data in the db. Even worse would be if that same class were then to echo output. That's a gross violation of the Single Responsibility Principle. Which brings us to your third question.

Separation of output Your class echoes, creates instances, performs all sorts of things in a single method. If that weren't bad enough, it also wraps bits and pieces into a try..catch. only to echo $e->getMessage(); and carry on as if nothing happened! The way you should think of Exception s is, they are things that are thrown outside of the normal flow of the code, because there's something odd going on. A class that inserts data into a DB doesn't know (nor does it need to know) where that data comes from or what it means. If the insert fails, an exception is thrown, and the code that called the insert method should deal with the Exception. Not the code that just passed the data on to the DB. If the code that called the insert method can't handle the exception, let it go. Its an error, don't hush it up and try to get by, let the script die, it's to prevent any further damage from being done.

Imagine I were to give you this piece if truly horrid code, and you had to debug it:

The call to saveStuff will always return true, so the cookie will always be set, or will it? If the saveStuff method caught a PDOException. it'll echo the error, and the headers will be sent. I can't set a cookie anymore. In this case, though, the problems are pretty obvious, but the larger your codebase becomes, the harder it'll be to debug code like this. Imagine there being 20-some instances involved, then it'll be up to you to uncover which one of these instances is causing the headers to be sent before the cookie was sent. Have fun and try not to cry. Ok, dry those tears, and just remove the try. catch block. What you'll get now is an error-page saying uncaught exception on line X. You can then look at the stack trace, see what data came from where, and what exactly it was that caused everything to go pear-shaped. There's no insert 's being performed after the one that went wrong. in this case, a "crash" is the cleaner alternative (google Worse is Better)

Other than that, you are using type-hints, which is a good thing, you are using getters and setters, that allow for some injection. Again, nothing wrong with that, although, you might want to consider implementing the fluent interface, also known as chainable methods:

There are still a lot of issues I haven't discussed, though, for example:

There is a 1/8 chance that both variables will be assigned the same name here. That's not what you want, is it? The number of available names, by the way, is probably a global state per request. If there is a max of 8 concurrent players, then the names that are taken should be unset, so that no two players can possibly get the same name. Now this, perhaps, a valid use-case for a static variable:

Of course, I don't actually know what you're doing here, nor do I know where the names are comming from, and if the number of players is to be limited, but I do know that a 1/8 chance of 2 variables being assigned the same value is not what you want, nor does it make sense (to me) to have 2 variables, called $player0 and $player1 . To me, a list (or array) just makes more sense:

Is my class doing more than 1 thing? If it is, split into multiple classes.

Do I have to scroll to read the entire method? If so, there's something wrong with it

Am I sure that, after calling a method, nothing, except for the instance (perhaps) has changed (no headers, no pass-by-reference)? If not, your code is smelly

Am I keeping things simple? If not Keep It Simple, Stupid

Can I write a test, without running into the static wall of death? If you can't Inject, don't use singletons.

answered Aug 18 '13 at 16:06

ugh. I don't even know where to start.

The code you wrote is not OO. In no way at all.

Let's answer you questions first:

I have been told that my main() function is too procedural

You have a 'War' class with a main() function that does everything. This is soooo java main() where you have to write your controller in the main(). But in pp you don't have to encapsulate a cotroller in a class and cramp everything in a main()

I have been told that the output logic should be separated

What if ou want to change the way a 'jack of spades' is displayed? You wil now have to dive into your business logic code. That simply asks for problems

Then overall: Using the words 'class' and 'function' doesn't mean you are writing proper OO code. When writing good code always think SOLID:http://en.wikipedia.org/wiki/SOLID_%28object-oriented_design%29

So, what will make you a better programmer? SOLID, always start there. Then read about the different patterns and when they should be used. A good tut from ibm: http://www.ibm.com/developerworks/library/os-php-designptrns/

Other articles

IDeCK To Prepare Smart City Plan

IDeCK To Prepare Smart City Plan

Eight firms had responded to corporation's invite for RFP. Six firms came up for presentation and three firms; iDeCK, CRISIL Risk and Infrastructure Solutions Ltd and ICRA Management Consulting Services were shortlisted for the final stage. The firms were judged based on team composition, work plan, urban experience in the state, approach and methodology. Approach and methodology carried the maximum weightage of 60 marks. ICRA was earlier chosen to prepare the SCP for Kochi.

iDeCK submitted the lowest financial bid of Rs 24.67 lakh as consultancy fee. CRISIL and ICRA quoted Rs 44.85 and Rs 51.175 lakhs respectively. The consultancy fee was fixed at Rs 40 lakh in RFP. The state mission management unit had evaluated the documents of firms and checked if the documents complied with the conditions mandated in RFP.

Xyz homework help

Xyz homework helpSno Question Price; 10023:

January 19, 2014 by Mark

On this page you can learn about Thesis Acknowledgement. The first few community service essay questions Sophie Germain primes are 2, 3, 5, 11, 23, 29, 41, 53, 83, 89. A prime is said to be a Sophie Germain prime if both and are prime. Jesse Kasserman how to do a research paper. walks into the office of Dr. 8680: Just look …. Science: Set your goals and don't be afraid to personal essay for university take risks, xyz homework help and essay green economy does it include you keep your investments diverse. Students who are 'priced out' have a lower chance of xyz homework help success. $6: Tour Start here for a quick overview of the site Help Center Detailed answers to any. online writing service Click on The Link Below to Purchase (Chapter 1 disobeying orders essay – 20) All Possible help with research paper thesis statement homeworkhelptips info Questions With Answers..

XYZ Affair -�Kids Encyclopedia

XYZ Affair

View full-size image

A British engraving satirizes French mistreatment of a female America during the XYZ Affair. The five figures at lower right represent other European countries, while John Bull (England) sits laughing on Shakespeare's Cliff at upper right.

During part of the French Revolution, France was also at war with Great Britain. The French expected the United States to enter the war on their side, because the British were seizing American ships. In 1794, however, Britain and the United States settled their differences for the time being by a.

To cite this page:

MLA APA Harvard Chicago Manual of Style

While every effort has been made to follow citation style rules, there may be some discrepancies. Please refer to the appropriate style manual or other sources if you have any questions.

TAMU Supercomputing - Homework: Get Acquained With Unix

Homework: Get Acquained With Unix

Before doing the lab exercises on the fourth day of class, you should be prepared to be able to login to one of the systems and be familiar with some basics. You can do these exercises in your spare time from your office or home, so that you can easily start the lab exercises and focus on learning Perl.

If you do not have an account on ada. visit the Howto Guide for instructions.

Login to ada. (For additional details check here .) So, for example, if you are using ssh and your login name is mylogin . you would type:

Once you've entered your password, you will be logged into ada .

Print your working directory:
which should output your home directory. On ada. that would be /g/home/mylogin .

List the contents of your home directory:
which should output a list of files, such as README .

Get a detailed listing:
Now, you'll see detailed information on each file, including permissions, ownership, file size, and modification date.

List "hidden" files:
The list includes the "hidden" files, which are just the files which start with a period (dot files). These are usually the startup files, like .bashrc or .profile .

View the README file:
The less program shows you the contents of a file, page by page. Each time you hit the spacebar, it will scroll down one page. You can also use the arrow keys, the Page Up and Page Down keys, etc. To quit, just hit the "q" key. For help on other key commands, hit "h".

Change your working directory:
This will change to your scratch directory. Confirm this by using the pwd command. You can list the files there as well. There should be a directory called tmp there, along with any other files you created.

Create a new directory:
List the contents again with ls -l to see this new subdirectory.

Change your working directory again:
This will change to the directory you just created. Notice that without any slashes ("/") to start the name of the directory xyz123. the cd command starts looking in the current working directory. This is called a relative path. So, from your scratch directory, "xyz123 " is the same as "/scratch/user/mylogin/xyz123 ". The first form is a relative path and the second is an absolute path (sometimes referred to as a full path ).

Now that you're in the xyz123 subdirectory, create a file called simple.txt. First, pick which editor you will use. (For details on text editor, see check here .) Assuming you are on ada and you want to use nano. you would type:
Your terminal screen will now be cleared and the top line will indicate "File: simple.txt ". At the bottom of the window, it will have a list of the key commands you can use. For example: "^X Exit " means that by holding the "Control" key and hitting "x", you will exit the editor.

Type a few sentences and then save the file using the "^O " key command (write out). Then, use the "^X " key command (exit) to go back to your normal terminal prompt.

List the directory:
You should see a line for the new file, simple.txt. which shows who owns it (you), the group owner (users), the number of bytes in the file, the the date it was last changed.

View your file:
Since it's a short file, there are not multiple pages to go through, just the one. To quit, just hit the spacebar or "q" key.

View your file with cat :
This simply outputs the file without pausing. If you use cat on a large file, like README. the hundreds of lines will scroll past very quickly.

Rename your file from simple.txt to other.txt :

mv simple.txt other.txt

List your directory to see that the name has changed. If you try to use less or cat on simple.txt. you'll get an error.

Make a copy of other.txt. called something.txt :

cp other.txt something.txt

List your directory to see that you now have two files.

Edit something.txt :
Add a few new lines to it. Save and quit. List the files again to verify that the size has changed.

View both files:

less other.txt something.txt

Hit the spacebar to go from one file to the next, or use the "q" key to quit.

Remove other.txt :
The -i option is for "interactive", which means you will be asked to confirm the deletion. It is good practice to use this option to avoid accidentally deleting files. List your directory again to see there is only one file remaining.

Remove something.txt :

rm -i something.txt

Now your subdirectory xyz123 should be empty.

Change back to the directory above:
Confirm that you are in /scratch/mylogin with the pwd command.

Remove the xyz123 directory:
If you have any files or subdirectories in xyz123. you can't remove it. In this case, you should have no problems.

Logout of ada :

You can also use "^D" (control-d).

These simple exercises should be enough for you to be able to do the laboratory exercises. To learn more about basic Unix commands, go to the New User Information page, and look under the Getting Started section for links to additional information. Read the Introduction to Unix and Basic Unix Commands pages. Experiment with the commands while you are logged into the system.