Making Decisions Inside Function Calls

I am currently actively developing using CORE. CORE is a library I’m writing to simplify the tedious process of writing HTML. Instead of doing this:

<div class="wrap">
  <p>Hello There <a href="somePage.html">read this</a></p>
</div>

You can do this:

div(
  p(
    'Hello There'
    .a('read this', 'somePage.html')
  )
  ,'wrap'
);

I really find it more pleasing to write that kind of code than writing markup, and I’ve seen an increase on my productivity due to the fact that I have to write less code, the end result is easier to read, it is easy to divide the page in functions that display different sections of the page. For example,you can do this:

div(
  theContent()
  .theComments()
);

theContent() is a function that returns the markup for the content of the page, and theComments() does the same for the comments section of the page. There are many other benefits, like the fact that I no longer need to worry about closing tags in the right order, or making sure all tags have a matching closing tag.

There is, however, one problem that was driving me nuts. Quite often you want to write something or other depending on some parameters. For example, if you are developing a wordpress theme, you might want to display some alternative text when there are no comments to show on a page. The usual way to do that would be:

if(have_comments()){
  //code here to show the comments
}else{
  ?><p>There are no comments to display</p><?php
}

But how do you do that with CORE?

One way would be to manage your logic outside the function call:

if(have_comments()){
  $comments = //code to show the comments
}else{
  $comments = p('There are no comments to display);
}

div(
   theContent()
   .$comments
);

But there are sometimes when you just want to handle the logic inside the function call. I can’t remember how many times I said “I wish I could just handle that inside the function call instead of having to create an extra if”.

Well, today I got an Idea. It istarted with a simple

alert(1==2 || 'b');// this is javascript

which then became

alert((1==1 ? 'b' : 'c') + ' yep');//still javascript.

Then I decided to take that idea into php. I think that was the best thing that I did today. Now I can do something like this:

div(
        h4('What people are saying')
        .(have_comments() ?
                '<ol id="comment_list">'
                .loadFunction('wp_list_comments')
                .'</ol>'
        :       
                'There are no comments to display. Add yours'
        )
        .br()
        .loadFunction('comment_form')
        ,'two_third'
);

Don’t mind the loadFunction() calls, they just “trap” the output of functions that do not return their result, but print it. What is important here is to notice the ternary operator, which makes handling the logic inside the function call possible.

What do you think about this? Personally, I believe it opens up many new possibilities, especially for people like me, who love working with functions, and who got excited when php 5 started supporting lambdas.

4 thoughts on “Making Decisions Inside Function Calls

  1. You are using a sort of S-expressions just as Lisp. Lisp has been unfairly criticised for its syntax which abounds in brackets, but they are there for a reason: to keep the syntax simple and flexible by allowing arbitrary nesting levels. S-expressions are briefer and pollute the text less compared to tag-ridden formats as HTML and XML. I wonder how one can criticise S-exprs and say nothing bad about bloated tags!

    But it is not only a matter of syntax sugar here, although syntax matters as well. The real value is provided by functional programming (FP), toward you are slowly going to.

    Unlike HTML, which is only data expressed in a declarative paradigm, your syntax forms have both data and code that expands to all data server-side. Only, the syntax is not the same for both data and code as in Lisp and alike. That is your language syntax is not homoiconic, which would be useful in some circumstances.

    It is interesting to compare your approach to some OO HTML generators and gain an insight into FP at the same time. FP is a superior paradigm of programming that can be applied to any problem and it proves particularly useful in simplifying and abstracting web programming too, the right way!

    There are more features to FP than lambdas, although lambda functions, lexical scope (closures) and garbage collection are a central piece of FP, so all benefits of FP cannot be seen in PHP, unfortunately, since PHP is not a functional language. If you continue this way you will have to turn PHP into LISP🙂

    • In fact, FP is the ultimate goal, but as you mentioned, php doesn’t support some features that are necessary for FP. I might end up having to move completely to some other language like python.
      Personally, I am not a purist of any paradigm. I don’t mind using OOP when it makes sense or helps keep the code more organised. But I do like working with functions better because I think they are the real power in most languages.
      As to comparing my approach to some OO HTML generators, I think it would be a good exercise.😀

      • I’m not a purist either, but for sure you would win the comparison with any OO HTML generator🙂

        If you really have to switch to another language, why not considering Common Lisp or Racket Scheme? IHMO they are much powerful and easy than Python on the functional programming side. For Racket I would suggest this online tutorial:

        http://docs.racket-lang.org/continue/index.html

        while for Lisp this funny (but very serious) site can help:

        http://www.lisperati.com/

      • I’ve been curious about lisp for some time now. I will definitely check those resources out, and start considering lisp. Another option that comes to my mind is node, since I’m already a javascript programmer, but for some reason node has never quite caught my eye. Have you ever done anything in node?

Comments are closed.