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
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.
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.
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:
All things considered, I'd bin this code, and start again. Keep asking yourself these simple questions:
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/
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.
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..
Build up emergency funds and pay off high-interest debts. Science: Tutorial videos on topics including arithmetic, algebra, trigonometry .This page supports our Roman Homework pages. .
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.
British Cartoon Prints Collection, Library of Congress, Washington, D.C. (Digital File Number: cph 3g02711)
XYZ Affair (Student Encyclopedia (Ages 11 and up))
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.
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.
mv simple.txt other.txtList 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.
cp other.txt something.txtList your directory to see that you now have two files.
less other.txt something.txtHit the spacebar to go from one file to the next, or use the "q" key to quit.
rm -i something.txtNow your subdirectory xyz123 should be empty.
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.