Classes vs Procedural Programming - No If Statements

Discussion in 'PHP' started by uniqueasitis, Jun 19, 2007.

  1. #1
    I was recently reading an article about sophisticated oop programming which showed how you could structure your objects to eliminate the need of if else statements. The author at the end claimed that you could eliminate almost all if statements using objects except in a few circumstances such as user input etc.

    I find the concept of programming with very few if statements very fascinating and would like to hear what people on this forum have to say about this.
     
    uniqueasitis, Jun 19, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    Sounds like a complete bogus to me.

    The advantage of classes is that they can be reused in other parts of your sites, and you don't have to write the same code over and over again.

    If that's what you're referring to, then yes, that's nice. But nothing unusual.

    But either way, the if() statements are still there, in your class. There just have to be certain conditions, alone to make your code secure.

    Can you post a link to the article?
     
    nico_swd, Jun 19, 2007 IP
  3. uniqueasitis

    uniqueasitis Peon

    Messages:
    661
    Likes Received:
    29
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Here is the link to the website. At first I also thought it was bogus but I then understood how you could program without if statements except for some instances. But then I could be wrong. Here is the link

    http://csis.pace.edu/~bergin/patterns/ppoop.html

    Yes I also believed that the true purpose of classes was to write code which could be reused. But as you can see, combine this with variable functions in php and you could do away with if else statements.
     
    uniqueasitis, Jun 19, 2007 IP
  4. uniqueasitis

    uniqueasitis Peon

    Messages:
    661
    Likes Received:
    29
    Best Answers:
    0
    Trophy Points:
    0
    #4
    And do give you a simple example consider this. You want to extract the filename from the url and then include a certain file. You could use an if else if statement or a switch statement. Or you could simple name the file the same name as in the url or have a set pattern according to which you change the name to correspond to a file you want to include and then use the name in the "include filename-after-pattern-matching.something."

    Here you have used pattern matching to rid yourself of the switch statement.

    Similarly when you get a user browser type why use an if statement to perform an action when you can set the same function name for all classes and then conocate the name you get for the browser type with the -> operator and function to invoke the proper object.

    So you see you could do away with if else statements with pattern matching. The only time when you would get stuck is when you get a return value of 0 or 1 from php bool functions. If you do not mind bad name conventions there is a work around to that as well. Ofcourse the only thing is that I do not know how useful doing away with if statements is.
     
    uniqueasitis, Jun 19, 2007 IP
  5. InFloW

    InFloW Peon

    Messages:
    1,488
    Likes Received:
    39
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Ok it's not removing if statements entirely it's the decision making of what class to call.

    In PHP5 anyways things like Zend framework make use of this method as well as several other frameworks.

    So here's a semi decent example here

    
    $a = new MyClass;
    
    function __autoload($class_name)
    {
    	$class_file = $class_name.'.class.php';
    	
    	$directories = array('classes/', 
    			'lib/',
    			);
    	foreach ($directories as $directory)
    	{
    		if (file_exists($directory.$class_file))
    		{
    			include_once($directory.$class_file);
    			
    			return true;
    			break;
    		}
    	}
    	return false;
    		
    }
    
    PHP:
    The autoloader is called any time PHP cannot find a class that you specified so being creative you could do something like

    
    $a = new $_REQUEST['myclass'];
    
    PHP:

    it'll only search in the few directories php has specified. Of course if it still cannot find it then it throws an error but there are ways around that using exceptions.

    You can also do this in PHP4 just for the opening of classes based on page input but the autoloader can do it for any class you try to init.
     
    InFloW, Jun 19, 2007 IP
  6. samusexu

    samusexu Well-Known Member

    Messages:
    138
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    108
    #6
    While this may be a new way to do things I think it complicates things a bit... why not keep it simple. if statements are not evil :p.
     
    samusexu, Jun 20, 2007 IP
  7. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #7
    Yes, but who can be bothered writing the code in the classes :p

    Having said that, I am almost finished with my little application framework for PHP (don't like Zend/Symfony/others), and yes, working with one does make like much easier, and the initial work involved is made up quickly. I don't see what the obsession about if statements if though... I would point out the drastic time savings and maintainable and reliable code.
     
    krt, Jun 20, 2007 IP
  8. InFloW

    InFloW Peon

    Messages:
    1,488
    Likes Received:
    39
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Well the way I look at it imagine a piece of PHP software where you allow others to create addons for it. Well if you're doing some giant structured if to make calls on other files then they now need to modify the controller. With an autoloader or a php4 based semi auto loader now the user just uploads the file and calls it via index.php?s=MyFile. I think it makes things much more simple compared to the if statements.

    This is especially true on a current project of mine where the administrative area alone is made up of 45 php classes which is also 45 files. When it's fully done it'll have 100+ classes that generate specific pages and functionality. I would not like to being doing 100 cases or 100 elseif's. Then if anyone ever wanted to add another option to the system that would be yet another line added to the controller.
     
    InFloW, Jun 20, 2007 IP
  9. samusexu

    samusexu Well-Known Member

    Messages:
    138
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    108
    #9
    Yeah I use this autoloader functionality also. But there are also cases when trying to use something else instead of if is just a cost of time/resources. Maybe it is something that needs to be thought of in every case.
     
    samusexu, Jun 20, 2007 IP
  10. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #10
    1st that article was written in 2000
    2nd that article concerns java not php.
    3rd if/else/elseif conditions are a necessary part of any interpreted or otherwise language, "getting rid of them" just isnt an option.
     
    krakjoe, Jun 20, 2007 IP
  11. uniqueasitis

    uniqueasitis Peon

    Messages:
    661
    Likes Received:
    29
    Best Answers:
    0
    Trophy Points:
    0
    #11
    spirit walker when it comes to oop it doesn't matter what language the tutorial deals with. If you are a professional programmer you should be able to apply the "principles" presented in one language to another. And second yes you can code without if and else statements. I will be writing an article which shows you exactly how. The only time you really need if statements is when a method returns a bool function. Instead of using an if structure you can divert the flow to functions which take care of the problem by using pattern matching (not in the sense used in oop but the sense i explained above).
     
    uniqueasitis, Jun 20, 2007 IP
  12. CygnetGames

    CygnetGames Peon

    Messages:
    43
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #12
    That's a nice article, but it doesn't seem to be trying to get rid of if/else statements.
    The if/else statements are still there - in the implementation of the hash table.

    The point is that the person writing the "is this box good or bad" program doesn't need to manage a huge case statement for each possible type of box.

    This appears to be an exercise in abstraction and scalable programs rather than an attack on if/else.
     
    CygnetGames, Jun 20, 2007 IP
  13. uniqueasitis

    uniqueasitis Peon

    Messages:
    661
    Likes Received:
    29
    Best Answers:
    0
    Trophy Points:
    0
    #13
    samusexu yes it does seem to complicate things a bit but when you consider scalability then this is neccessary. Why expand on an if else structure when you can simple add another class called freebsd (as in the article). Additionally, when ever you check for a condition you need to have one of two alternatives take place. You can structure your program to choose a alternative by picking one. You would have a statement which would execute and if it shouldn't you would just have a function which wouldn't execute. Just return a null or empty value.
     
    uniqueasitis, Jun 20, 2007 IP
  14. uniqueasitis

    uniqueasitis Peon

    Messages:
    661
    Likes Received:
    29
    Best Answers:
    0
    Trophy Points:
    0
    #14
    Cygnet you are right. But read what the guy says at the end. There are some instances where you JUST CANT avoid if else statements like user input although with php variable functions you could. The purpose is to limit the statements to a neleligible amount. Most of the time we programmers take the easy way out and bunch of switch and if else statements. Had we thought a little we could have come up with an if less solution.

    Take the example of a form which radio buttons. A novice would set up a if else to check which radio button was selected whereas an expert would simply set the value of each radio button to a corresponding method and then use variable functions to invoke the proper function. Neat, clean, and scalable.
     
    uniqueasitis, Jun 20, 2007 IP
  15. InFloW

    InFloW Peon

    Messages:
    1,488
    Likes Received:
    39
    Best Answers:
    0
    Trophy Points:
    0
    #15
    I've posted on how to do this already above doing PHP. It really just makes the controller a little smarter and more self aware without someone needing to add logic. I posted a php5 example but the same can be produced with php4 along the same lines.

    This concept is catching on now with PHP frameworks and things of that nature but just looking at the article tells you this is not a new concept it just hadn't caught on with php people. Now that MVC is catching on a lot more it makes sense that this extension to it is added making the self aware controller that searches for the classes opposed to you using logic to find them.
     
    InFloW, Jun 20, 2007 IP
  16. uniqueasitis

    uniqueasitis Peon

    Messages:
    661
    Likes Received:
    29
    Best Answers:
    0
    Trophy Points:
    0
    #16
    EXACTLY. Berserker is right. Thanks for letting me know it is not a new concept because I for one just came across it and that is why it seemed new to me. I had always wondered what the difference between oop and procedural code was if you had to use logic. Classes seemed like a cover up for the procedural part. Well hopefully I myself, who has programmed using procedures, will be adopting this method in the future.
     
    uniqueasitis, Jun 20, 2007 IP
  17. lionheart008

    lionheart008 Guest

    Messages:
    43
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #17
    Using If statements in OOP is not bad. However a block of if statements is usually a code smell and could sometimes be replaced with a factory pattern. The idea of OOP is code reuse and scalability not to complicate things. In my opinion debugging/upgrading procedural scripts is much more complicated anyway.
     
    lionheart008, Jun 20, 2007 IP
  18. uniqueasitis

    uniqueasitis Peon

    Messages:
    661
    Likes Received:
    29
    Best Answers:
    0
    Trophy Points:
    0
    #18
    Yes that is a better way to put it lionheart. A block of if statements is what people really need to replace.
     
    uniqueasitis, Jun 20, 2007 IP