SitePoint Sponsor |
|
User Tag List
Results 101 to 125 of 136
Thread: How much OO is too much?
-
Aug 14, 2005, 03:19 #101
- Join Date
- Nov 2001
- Location
- Bath, UK
- Posts
- 2,498
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by McGruff
As you point out, talking about "Model objects" is a bit ambigious (though I thought the mention of AR vs Domains made it clear enough what I was talking about). I was referring to a objects which exist in the Model layer in MVC, which includes objects which talk to the database. Calling them "ActiveRecord based Model objects" is closer to the mark, but it's a bit unwieldly for more than a paragraph.
The distinction I was trying to make, is that an "ActiveRecord based Model object" is responsible for talking to the database, but a "Domain object" never is. (The "ActiveRecord based Model object" could delegate database calls behind the scenes, but that's an implementation detail. The existance of "behind the scenes" is important in OOP)
Regards,
DouglasHello World
-
Aug 14, 2005, 12:19 #102
- Join Date
- Sep 2003
- Location
- Glasgow
- Posts
- 1,690
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Actually active records are domain objects. They create a type of domain model where there's a close match between the db table structure and the domain objects.
I'm not sure what I think about validation. There are checks for valid request syntax which don't involve the domain, and other checks which do. Perhaps it should be a two stage process although at present I just do it all in a Request object. I like a single front door - this possibly helps security. Nothing gets through which hasn't validated and all validation is carried out in one place rather than scattered around the app.
-
Aug 14, 2005, 13:11 #103
- Join Date
- Nov 2001
- Location
- Bath, UK
- Posts
- 2,498
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by McGruff
If you put all validation on that door, you have similar maintenance problems as between documentation and code. Another place to make changes when you want to update an object behind the curtain. Though nothing that can't be overcome through extra work
DouglasHello World
-
Aug 14, 2005, 14:42 #104
- Join Date
- Sep 2003
- Location
- Glasgow
- Posts
- 1,690
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by DougBTX
However, it might not be possible to define all the validation requirements in a domain object without giving it knowledge of the presentation. For example, with a forum post, you might want to check for XSS. This kind of rule would be required only in the http context.
Not that you'd be likely to try to make a CLI forum of course...
-
Aug 15, 2005, 05:13 #105
- Join Date
- Nov 2001
- Location
- Bath, UK
- Posts
- 2,498
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Off Topic:
Originally Posted by McGruff
Hello World
-
Aug 15, 2005, 18:57 #106
- Join Date
- Sep 2003
- Location
- Glasgow
- Posts
- 1,690
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Another example might be escaping parameters which will be used in SQL queries. Filtering makes the query safe but to actually detect an attempt at sql injection you'd have to do more - and ideally you'd have some way to identify the attacker. Unfortunately there isn't any guaranteed way to do that. You might get lucky. At the least it could tell you how often you're being attacked.
-
Aug 15, 2005, 23:47 #107
- Join Date
- May 2005
- Posts
- 255
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by charmedlover
Also, it's not anything unique. It's called a singleton. It's a useful design pattern. The only people I've ever heard complaining about singletons are those who've never designed a real-world application.
-
Aug 16, 2005, 00:02 #108
- Join Date
- May 2005
- Posts
- 255
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by REMIYA
Can anyone imagine 50 000 lines in procedural programming, and how would they be kept up to date
PHP has become the C++ of the web; it took procedural roots, and, as it became extremely popular and began to see corporate usage, began evolving into an OO language. Yes, that means it has plenty of constructs and flaws. It also has many strengths, namely in that you don't *have* to do everything with objects. It's not always appropriate, especially not for a stateless, interpreted language that has potentially infinite entry points.
PHPs biggest advantage is that it's not going away. It's truly cross-platform, completely open source (a claim that neither Java nor C#* can make). PHP is not an all-purpose language. Things like php-gtk usually just make me shake my head. It's a web programming language, suited for the special needs of web development. It isn't Java, it isn't C#, and it isn't C++. So stop comparing them.
If you're going to compare PHP to another language, compare it to Perl, Python, and Ruby, as those are languages specifically designed for the same sorts of tasks that PHP was.
* Mono is open source, but the most critical aspects of C# are not. Windows.Forms, anyone?
-
Aug 16, 2005, 01:35 #109
- Join Date
- May 2002
- Location
- Gent, Belgium
- Posts
- 284
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by DougBTX
As you can see, the contract code is still there, it is just in a different place. The magic, is that it lets us think of things in a rather different way. Note that the assert line now uses $this->color instead of testing the input $color directly, this means we can test compliance with the contract whenever it is most useful for us.PHP Code:s =& new Shape('bobo');
We must make sure validate() gets called whenever we want to do something with a Shape object (like the save() example you gave). For methods in Shape or its parent, that's probably ok if we're the writers.
But, it's impossible to predict what clients will do with it, so we have to make sure that validate() gets called in the client code before that. How should we do this? By warning writers of client code in our doc comments?
It is just a contract. That's all we have to write. We now have the option of constructing the new Shape implicitly:
PHP Code:$shape = new Shape($_REQUEST);
if ( !$shape->save() )
{
# send the form back with error messages...
}
save() calls validate()
validate() calls assert()
assert() throws an exception if contract is breached.
So, you cannot do if (! $shape->save()) for checking user input, you're back to exception checking...
Or you return false in validate() for example, in which case you don't have the 'crash early' approach for when a contract is breached.
Which brings up, I think, another key point where our opinions differ;
I still strongly believe that there's a distinct difference between client code not living up to a contract and invalid user input:
Not living up to a contract is a bug, it is an exceptional situation and thus an exception should be raised (or something similar drastic), imo, we should crash early.
Which is something different than validating user input, where we totally expect errors to be made and we should fall back gracefully.
They're different monkeys, we should stroke 'm differentlyPer
Everything works on a PowerPoint slide
-
Aug 16, 2005, 01:49 #110
- Join Date
- May 2002
- Location
- Gent, Belgium
- Posts
- 284
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by Etnu
Furthermore, let me be one of those never-designed-a-real-world-application-people and state that the singleton pattern is overrated, I prefer the "Highlander principle": you pull out an ancient sword, very convincingly shouting "There can be only one!" and behead anyone daring to make more than one instance of such a classPer
Everything works on a PowerPoint slide
-
Aug 16, 2005, 02:01 #111
- Join Date
- Nov 2004
- Location
- Romania
- Posts
- 848
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by Etnu
Originally Posted by Etnu
Originally Posted by Etnu
Originally Posted by Etnu
Originally Posted by Etnu
Originally Posted by Etnu
-
Aug 16, 2005, 06:21 #112
- Join Date
- Nov 2001
- Location
- Bath, UK
- Posts
- 2,498
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by been
Originally Posted by been
PHP Code:class ActiveRecordBase
{
var $_errors = array();
# remember that array() == false
function is_valid ( )
{
$this->_errors = array();
$this->_validate();
return !$this->_errors;
}
###
function assert ( $field, $valid, $message = 'Invalid' )
{
# isset lets us return only the first error for each field,
# so that "missing" can be shown instead of "invlid".
if (!isset($this->_errors[$field]) && !$valid)
{
$this->_errors[$field] = $message;
}
}
###
function save()
{
if ($this->is_valid())
{
# more code...
}
}
###
# more code...
}
class Shape extends ActiveRecordBase
{
function _validate ( )
{
$this->assert('color', in_array($this->color, array("green", "orange", "purple")), "What is this colour?");
}
}
Off Topic:
If you wanted to make the above more flexable, you could add an assert_rule method too, which would act a bit like the addRule method in the ApplicaitonController thread. I do think though that using an object where a boolean works is overkill and leads to bloated code, so should be treated as the exception rather than the rule.
But without affecting other code, it could look like this:
PHP Code:class ActiveRecordBase
{
function assert_rule ( $field, $rule, $message = 'Invalid' )
{
if (!isset($this->_errors[$field]) && !$rule->passes())
{
$this->_errors[$field] = $message;
}
}
}
Originally Posted by been
- You set some things on an object
- You try and do something with the object
- If it fails, you look at the errors
(I disagree that it is impossible to predict what a client will do with our object, because unless they are simply treating it as a hash table, they will have to call our mthods. If they call our methods, we know what they are trying to do, and because they are our methods, we know if we need to call validate or not.)
From ouside, validate doesn't have to exist, it is just a way for the object to keep track of itself. I suppose it is a bit like an inversion of contracts, or at least an inversion of how they work in the getter/setter part of the code. Rather that having to meet a contract before you can set something on an object, you have to meet the contract before you can do anything with the object. It might just be that claim that a setter "does" something that doesn't sit well. When you are just taking in input (from a user or a client), are you "doing" anything?
From a physical perspective, yes, you are putting something in memory. In a language with strong static typing, yes, you are implicitly comparing the input with a strict type definition which you use to protect your memory, regardless of what you do, if you get the types wrong you'll have exceptions to deal with. What about a language with strong dynamic typing? Or even in PHP, with loose dynamic typing? You're not really storing the input, you're just keeping a reference to it. I'd argue that at a high level, you're not doing anything with it, because "doing" something is a cause, which implies an affect. But assigning to a variable which is loose and dynamic, nothing will ever happen beyond the assignment, there will be no external affects. With no affects, was there a cause? If there was no cause, did you really "do" anything? The only way to see if anything has even happened is to read the variable back, but by then processing has moved on to the next statement.
Regards,
DouglasHello World
-
Aug 16, 2005, 06:30 #113
- Join Date
- Nov 2001
- Location
- Bath, UK
- Posts
- 2,498
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by Etnu
DouglasHello World
-
Aug 16, 2005, 08:08 #114
- Join Date
- Jan 2003
- Posts
- 5,748
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
But in saying that, shared hosting is the stable diet for PHP. So it appears that we are stuck with PHP4.x for the forseeable future at least. I'd suggest that it's going to take about 2 years for a good bit of PHP4.x development to migrate to PHP5.
PHP is popular not only because it's easy to pick up and use, but the hosting is there for everyone... It's extremely affordable, hell I not more than £30 per year for my hosting, and that's PHP5 on Linux.
I don't see the lack of PHP5 hosting being a hinderance currently as it stands today, as there is hosting available. You just have to look for it, that's all... Just don't expect it to jump out and bite your a**e, that's all
-
Aug 16, 2005, 09:43 #115
- Join Date
- Aug 2004
- Location
- California
- Posts
- 1,672
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I know this is an OO thread and I know (oh how painfully clearly) all of your opinions about PHP and OO (and every other language for that matter), but the one thing not mentioned here is that the reason for PHP's popularity is that it happily allows Procedural programming which is much simpler for amateur programmers to learn and be productive. Face it, the web's two most popular languages are simplfied C (PHP) and BASIC (ASP). I know you will all whinge about the tons of crap code in PHP, but the bottom line is that PHP allows a broader group of people to build web applications than any other language. All that crap code is the result of languages like PHP that have enabled a revolution where many more people can program. Like it or not, code that revulses you drives a big piece of the web.
Sure the code does not meet your high standards, but fortunately only people like you care. It's the Purity vs. Utility argument and Utility repeatedly win much to your Purist chagrin. The reason there are no huge Python or Ruby support forums is that amateurs are just not going to learn languages like that. Probably the only hope for pure OO languages in the mainstream is code generation so that amateurs don't have to program.
Just because I think the fact that PHP allows amatuer programmers to fairly easily produce useful code (which you consider nightmarish crap) as a virtue, does not mean that I don't think it is a parallel virtue to encourage professional programmers (and those amateurs wanting to improve) to produce quality OO code.
One of the reasons I got involved in the "skeleton" threads is because I saw an amateur (cadmiumgreen whereever you are?) who was "wanting to improve" but there is very little OO code in PHP to lead the way. Sure there are a million frameworks, but you can't realisticly learn OO from them. I still believe that easy to implement, patterns based, OO code that can piece by piece be used as building blocks to learn OO would be a valuable contribution to PHP. I note that many of the opinionists in this thread were noticably absent in that development, perhaps because they did not understand the goal or ...Christopher
-
Aug 16, 2005, 11:50 #116
- Join Date
- Nov 2004
- Location
- Romania
- Posts
- 848
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by Dr Livingston
Also Tomcat hosting prices start at aprox $15 but that mean usually Tomcat 4 but if you want Tomcat 5 you have to pay aprox $20 more. And those are just starter packages.
The $5 hosting plans are only available for PHP. That's hard to beat.
The most affordable sollutions for other platforms (other than PHP) are those virtual private servers (VPS) offers. Really, you can install on them what you want without asking some Grinch to install it for you. And pricing starts at $20.
that pricing is per month
-
Aug 16, 2005, 11:57 #117
- Join Date
- Nov 2001
- Location
- Bath, UK
- Posts
- 2,498
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Good post Chris, it is something that needs to be acknowledged. It reminds me somewhat of the "solve simple problems instead of hard ones" post at Loud Thinking
I note that many of the opinionists in this thread were noticably absent in that development, perhaps because they did not understand the goal
I still believe that easy to implement, patterns based, OO code that can piece by piece be used as building blocks to learn OO would be a valuable contribution to PHP.
Procedural programming which is much simpler for amateur programmers to learn and be productive.
We don't need a Request or a Responce or Context or a Logger (which doesn't even write to a log? I don't get it) let alone a hundred IHandler's floating around. PHP wouldn't be where it is today if it didn't support all that out of the box. PHP also benifits from massive amounts of documentation focused on using it for the web. You can't say the same about Ruby (its been in Japanese for the last 8 years) or Python (the lead developer isn't interested in web programming) or practically any language. It seems like the choise was C or Perl, so someone wrote something in Perl to make their life easier. Now we've got people writing similar things for Ruby (Rails) and Python (Django) with the goal of making development of web apps easier.
Procedural programming which is much simpler for amateur programmers to learn and be productive. ... Probably the only hope for pure OO languages in the mainstream is code generation ...
You can see the march towards simplicity in PHP. In PHP 4, you had to know about the differences between assigning by reference vs copying. In PHP 5, you can ignore that, and just use = whenever you like. It is becoming simpler to use.
I don't believe that using objects is inherantly more complicated than using ie MySQL result handles. But handles are simple enough so that we don't need to listen to people shouting "it uses the ResourceHandlePattern!" before we can use or understand them. It is just accepted, and to an extent hidden under the surface, just like assignment by reference is now hidden in PHP 5.
And hay, I can always use the grass is greener defence
DouglasHello World
-
Aug 16, 2005, 12:15 #118
- Join Date
- Nov 2004
- Location
- Romania
- Posts
- 848
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by arborint
In my view phpBB represents utility. And yet there are webmasters that simply banned phpBB from their server for it's design flaws that lead to hacker attacksI think no other comments are needed.
Don't get upset about this, I don't want to offend you, but the Purity vs. Utility argument it is only available for programmers who don't give a <censored> about performance/security/scalability (I for one am like when it is all about receiving the money as fast as possible) or for designers that just started programming.Last edited by r937; Aug 16, 2005 at 12:56.
-
Aug 16, 2005, 12:45 #119
- Join Date
- May 2005
- Location
- Finland
- Posts
- 608
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Off Topic:
Originally Posted by DougBTX
Originally Posted by DougBTX
I hope I didn't really come off as a pedantic pattern aficionado. If I did, well... ouch.
-
Aug 16, 2005, 13:30 #120
- Join Date
- Aug 2004
- Location
- California
- Posts
- 1,672
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by DougBTX
Originally Posted by DougBTX
Originally Posted by DougBTX
Originally Posted by DougBTX
Originally Posted by DougBTX
Christopher
-
Aug 16, 2005, 13:39 #121
- Join Date
- Aug 2004
- Location
- California
- Posts
- 1,672
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by bonefry
phpBB allowed probably millions of people to create billions of posts and communicate with each other. It was one of the many enabling programs that has helped move the web forward.
Honestly "performance/security/scalability" are not the only things that are important. For enterprise programs they may be critical, but again those are not the only kind of programs. What scripting languages show is that crap code is better than no code in creating progress in software. If only programs that met you approvial were allowed the web would have never happened.Christopher
-
Aug 16, 2005, 13:52 #122
Originally Posted by bonefry
-
Aug 16, 2005, 13:56 #123
- Join Date
- Jan 2003
- Posts
- 5,748
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
but to wrapper their code in some goodness so that the goodness slowly seeped into their code.
That is why when I started out learning PHP I quickly realised that if I wanted more, if I was to be bigger, better, and bolder, I had to move to object oriented programming. You talk about me having views about procedural programming <> object oriented programming.
And, referring back to the earlier posting about Highlander,... There can only be one, and that my friend is object oriented programming. There is just way too many advantages of it, that it just can't be ignored.
As for those people who script spagetti code, sure there is a lot of that, and that is one of the short falls of PHP and it's ease of use. Like I said, anyone can use PHP, but alas, and this is a valid point, not everyone can develop.
Not so long ago I made the statement that some people see OO as a way of life, and others as a religion yes? Well I fall into the later category
-
Aug 16, 2005, 13:59 #124
- Join Date
- Jan 2003
- Posts
- 5,748
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Are you trying to insinuate that phpBB is a security risk because it is largely procedural? That is what it sounds like.
Care to argue huh?
-
Aug 16, 2005, 14:02 #125
- Join Date
- Sep 2003
- Location
- Glasgow
- Posts
- 1,690
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thread tools ---> unsubscribe from this thread. Phew.
Bookmarks