[quote name='speciesUnknown' timestamp='1318387960' post='4871701']
I take issue with the concept of Best Practice; the biggest issue I have is that many supposed practictioners or Best Practice have no understanding of WHY something is considered to be best. To use it correctly, you have to understand why a particular practice is considered to be the best, who by, and in what situation.
For example, globals are a brilliant way to solve a problem immediately, but they have implications, so to avoid cowboying, you should consider the implications of the use of globals. The same thing applies to singletons; in what situations are they likely to cause problems? What problems might they cause? What alternatives are there?
Not sure I agree with this. A lot of best practices are great for those who don't fully understand it and deviating from best practices is best left to those who understand the problem and why deviating from accepted practices is the right move to make. In your singleton example, the general understanding of singletons is that they are anti-patterns and it's best to not use them. That's not to say that someone who understands the problems with singletons cannot effectively use them when the situation is right. Globals also go against best practices, and while it's important for someone to understand
why globals are a bad practice, they can gain immediate benefit to their code by following the best practice.
[/quote]
Just to clarify, it is the concept of Best Practice which I have issues with. Most people I have asked about this define it with a partial tautology: they are the things you should do because they are the best, or words to that effect. What people don't seem to understand is that there is no one voice declaring what is best, and even if there was, it would still be just an opinion. I've been frustrated many times when I was told to do something because it was Best Practice, even if it didn't fit the requirements. Who decides that it is best practice? How did they reach that conclusion? Why should we listen to them?
I've been told to further normalise a database which was already in 3NF, by adding a joining table for a 1:many relationship, because this is apparrently best practice. I've been told to break a class with only 2 methods into two smaller classes because this is best practice. I've been told to create an empty constructor in a language where an empty constructor replaces the (necessary) constructor from the parent class, thus breaking my exception tree, because apparently this is also best practice. In reality, these 3 different people all decided that they were wiser than me and used Best Practice as a meaningless argument.
In the first example, this person knew a few facts: many to many relationships use a joining table, thus everybody uses joining table, thus I should have used a joining table. They missed the fact that joining tables are used in a many:many relationship out of necessity, rather than because somebody somewhere wrote an article and decided it was best.
In the second example, this person had implemented a much more elaborate version of the same system, in which the two classes were larger, and separating them made sense. What they failed to notice was that my system was just delegating to a procedural texture loading API for use in a small game, while theirs was wrapping up a custom built texture loading API for an AAA game.
In the third example, this person saw somebody do this in a tutorial, but in a totally different context. Not feeling comfortable that I had empty classes (who's only purpose was to be leaf nodes in an exception tree) they felt that I wasn't doing Best Practice, i.e. what everybody else was doing.
I think that the best antidote to the feeling that you are a cowboy yourself would be to adopt defensive programming.[/quote]
Which itself is a best practice!
According to who exactly? I often recommend it, but I'm still really a beginner. This is a typical example of why I dislike the term - people use it to give their own opinion the appearence of having additional weight - you are basically pulling an argumentum ad populum by suggesting that a large number of people agree with you.
[/quote]
The solution, which I have now learned to provide in future, is to provide a data access method along the lines of getPageSafeData() which performs all the page safe escaping for you. This way, next time I work with noobs, I can be sure my data isnt going to inject nasty things into their pages.[/quote]
I don't think that will necessarily help you. The bad programmers will likely not use your new safe getPageSafeData method and will pull out the data themselves. After all, they could have easily used the built in [color="#1C2837"]htmlspecialchars method to escape the data, but they didn't. Unless they have ego problems or are really bad programmers, I think a better approach is education. Point out why what they are doing is bad, and how they can prevent that in the future. There is a good article somewhere over why trying to write code to protect against bad developers is a bad thing, but I'm having trouble finding it right now.
[/quote]
Needs must when the devil drives. Having had this bad experience, I've decided that the "best" way to avoid such problems in future is to provide a safe data access option. The alternatives is more people blaming me for their oversights, or disallowing control characters in user input.
Since what is best is a matter of opinion (everybody has different criteria) the only real way to determine what is best is to objectively look at the known facts.