Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / HTML

A New Web Programming Language

4.60/5 (9 votes)
8 Oct 2014CPOL8 min read 20.5K  
Are the programming languages we use for web design today really adapted well enough to fit our needs?

Introduction

Many different programming languages are used for web development. This article's target is to show the difference between P* (P-star) and other languages widely used for this purpose.

Contents

Designers VS Geeks

Essentially, what web development is all about, is to generate HTML-markup dynamically based on data stored somewhere and on user input.

A web page may be divided into two parts: The code part, which takes care of database and user input, and the HTML-part which takes care of the design.

Sometimes, a single person might do both the coding and the HTML, and this person will be comfortable with having these two mixed. However for larger and more complicated web pages, we need proper geeks to do the coding and artistic people to do the design.

Since designers don't understand much programming, they tend to mess up complicated code written by the geeks. This problem forces us to separate the HTML from the program code, and this is solved by only having very simple code (loops, conditions and variables) in between the HTML code which the designers can understand without much effort. Database handling and user input processing is kept by itself out of reach of the designers, and variables are prepared here for the designers to use later.

This technique is called templating, and different languages have different ways of handling this.

What Template Engine Should I Use?

On Wikipedia, there's a list of almost a hundred template engines for many different languages. This indicates that there's a real need for templating out there. It also tells us that the languages themselves do not implement templating natively; the template engine is usually an external module.

Languages used for web development typically don't have any special functionality to handle this important part of web design, the developers are left on their own to sort it out. Java, Perl and C# are well known general purpose languages who are also used to generate dynamic HTML-markup.

The widely used language PHP also has to be mentioned. It differs from the other languages in the way that it almost exclusively is used for web design while having no in-language support for anything related to web design apart from the ability to be interleaved in HTML code. PHP provides nothing more than a huge, messy pile of built-in functions which there is no exact count for. Some estimates say around 4500 functions.

Example of a Program using a HTML Template

The lack of in-language template handling is the reason why P* was created in the first place, P* has native support for templating, it is part of the language's syntax.

HTML_TEMPLATE document {
    <!DOCTYPE HTML>
    <html>
    <head>
    <title>{@title}</title>
    </head>
    <body>
    </body>
    </html>
}

SCENE main {
    string title = "P* Web Programming Language test page";

    #CONTENT_TYPE text/html;
    #HTML_TEMPLATE document;
}

Prepared SQL Statements

Boilerplate code is equal copies of code that you have to repeat many times in a program to make it work. This includes stuff like checking return values from library functions and printing error messages, or including modules and header files at the top of a program.

MySQL Prepared Statements in Java

One of the worst time-consuming copy-paste-editing you will ever come across is creating a prepared statement. Given below is an example in the Java-language. Notice that the word PreparedStatement is used seven times, and that you have to deal with the member methods setString and setInt of this object for every single variable.

Class.forName("com.mysql.jdbc.Driver");
connect = DriverManager.getConnection("jdbc:mysql://localhost/animal_kingdom?" + "user=dog&password=i_hate_cats");

// It is extremely boring work to set all this fields
preparedStatement = connect.prepareStatement("INSERT INTO animal_kingdom.persons VALUES (?,?,?,?,?)");
preparedStatement.setString(1, "Jo");
preparedStatement.setString(2, "Lene");
preparedStatement.setString(3, "jolene@hotmail.com");

// Sometimes we need to store strange properties of a person
preparedStatement.setInt(4, 135);
preparedStatement.setInt(5, 4);
preparedStatement.executeUpdate();            

You begin to wonder why on earth you have to tell Java that "Jo" is a string type. You also have the manual work of counting all the question marks and setting the variables in correct order. This work, of course, becomes really fun when you also have to deal with column names in the query and you have 20 columns of different types to insert.

The P* author, also referred to as 'someone' in this article, has once been forced to write a wrapper class just to keep track of column names, values and question marks. We don't want to spend time doing this, we want to create web pages! Let's have some more meat.

MySQL Prepared Statements in P*

Next comes the same example in P*, let's just specify the variables first instead of inlining, like you would normally do:

string name = "Jo";
string surname = "Lene";
string email = "jolene@hotmail.com";
int weight = 135;   /* In kilograms */
int width = 4;      /* In feet */

SQL sql {
    INSERT INTO animal_kingdom.persons VALUES (
        {@name}, {@surname}, {@email}, {@weight}, {@width}
    )
}

MYSQL mysql->connect("localhost", "dog", "i_hate_cats");
mysql->select_db("animal_kingdom");

MYSQL_STMT stmt = mysql->new_statement();
stmt->prepare(sql);
stmt->execute();            

Common Reactions to this Example

  • Wait a minute...You said you wouldn't inline the variables, but you did?
  • You idiot!!! This is SQL -- IN -- JEC -- TION -- !!!!

This way of specifying a prepared statement is P* magic. We would not normally, and shouldn't either, put variables inside the SQL-string. However, because we are lazy (and to avoid the horrible and error-prone code you see in the first example), we might be tempted to do just that. With P*, you can put the variables inside the SQL-string WITHOUT them being concatenated.

The whole SQL-object is a chain of strings and value pointers, and P* does the dirty work of passing each variable to MySQL with the correct type set. The variables are replaced with question marks before being passed to MySQL, making it look like the Java-example.

Human Languages and Computer Languages

Different programming languages are basically different ways for us to talk to computers, telling them what to do. Human languages are different methods for humans to talk to each other. Human- and computer languages do of course differ in many ways.

One notable difference is that human languages are much more dynamic than computer languages. It's probably a fact that no human being knows all the words of any language, which are usually tens of thousands in numbers. Humans know the words they need in their daily lives.

A physician probably knows more words in medicine than an average person. When a physician could have used a single word to describe a condition, an average person might have needed to use many words to do the same. The physician's language is therefore efficient to use when talking about medicine.

The same applies to programming languages. You typically want the programming language you use to have special words to help you speed up your coding and make your programs easier to read and maintain. A language used for web development should provide words and tools specialized for the task you're solving.

P* tries to make it easy to talk web design to your computer by providing special syntax for SQL-queries and HTML-templates, doing the dirty work behind the scenes.

Read more about the P* SQL syntax in the reference manual.

Source Code Organizing

When you start a new project, you need to organize your code somewhere, that is the different files containing the program code. For application projects, some tools used are CMake, Autotools and Visual Studio.

In a web project, the files are usually placed inside the root of the web server. The web server finds them there and calls an appropriate compiler to execute the code.

Some implementations have more complicated solutions, like JSP (Java Server Pages). Here, you often need to precompile classes and put them in special directories for the Java engine to find. The configuration of a JSP environment is somehow complicated, whith lots of new terminology to learn besides the actual language.

P* cannot be precompiled, and you just use the #INCLUDE-pragma to load other files. Everything is compiled automatically at first run or when the files are modified. This makes development very easy.

It is also easy to learn P* if you already know other languages like Java, C, C++, PHP or Perl. To configure a web server to execute P* is very easy, as P* does not require any configuration itself.

P* programs can usually be run both inside the web server and outside of it using the command line version. This is useful to check for syntax errors while writing your code. You can also fake user input by setting CGI environment variables.

Is P* Fast?

If speed is your only concern, you should write your web page in C and compile it as a web server module. However, speed is not only about making a program run fast, it's also about writing fast. The focus of P* is on the latter.

P* internals are written in C++ which is a bit slower than it's daddy, C. Only critical sections are optimized, like text output to the web server. P* also caches programs internally so that the scripts only are parsed once.

Arithmetics, like plus and minus, is slower in P* than in other languages. However, you will only notice this if you make a loop run a billion times, in which P* will be a couple of seconds slower than PHP and Perl.

The P* engine is not based on anything else out there, it's completely written from scratch. The only thing P* has in common with other languages is the C-like syntax.

What To Do Next?

P* is in a very early stage of development, but it will run on Linux both standalone or as an Apache module. There is a Windows build available, but it is rarely updated.

Homepage: p-star.org

Everything is on GitHub!

GitHub: github.com/P-star/P-star/

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)