Academic people should git and TeX

Mr Torvalds created two amazing things: Linux and Git. Former is an OS kernel; latter is a version control system. Unluckily, none is prevailing in Malaysia.

When I was a lecturer, creating a new programme with various courses is truly exhaustive. The worst case is recording the changes of the documents for the government agency’s accreditation. If you are systematic, you will backup the files. But backing up the files does not tell you what are the changes you had made. Unless you create another note for each changes you made. But that will be double works. If you say you can use Microsoft Word’s feature to compare the documents and see the changes, it is totally impractical if the two documents are big and there have a vast changes.

What is the best solution? In practice, you need to ask your boss to step down and change all your colleagues 😉, because your boss doesn’t understand your solution, he and your colleagues will treat you as idiot.

In the condition above, the best solution is using TeX and Git. TeX allows you to create your document in plain text. Plain text is so important for Git. Git allows you to keep track the changes you have made, and you can see the difference of the changes line by line in text format.

Git allows you to work collaboratively with your team members to work on same project by preventing the conflicts. Preventing conflicts doesn’t mean there will have no conflict, but you will detect the conflicts earlier and need to resolve the conflicts manually.

TeX, unlike WYSIWYG application software, such as Microsoft Word or LibreOffice Writer (I don’t think Malaysians use LibreOffice), we create the document using markup language and setup the paper style through some complex TeX statements. Though the setup may be exhaustive and TeX has a steep learning curve, the results can sustain for long-term. The document style can be re-used for the whole institution, especially if the students are provided with the thesis format in TeX form. Moreover, the TeX skill is useful to publish papers to the journals and conferences. You can easily port your content to another TeX style such as IEEE conference paper style.

Sadly, most people take easy learning tools to do the complex tasks, yet feel proud and not open minded to learn useful skills.

An academic institution should offer training to the staff, lecturers, and students to learn TeX. It would be even better to offer Git training, Linux and command-line. Open source software can reduce students financial burden, avoid pirated software, and prevent virus infection.

However these are not implemented in most academic institutions. As a result, the users are spending hours to edit the document style, generating the table of contents, or even preparing table of contents manually. That is sad to create table of contents manually. Any change on the page, you will have to edit the table of contents. However, if you are using TeX, you will focus on the content, instead of the styling.

Lastly, because the culture focuses on the outlook such as styling of the document, instead of the quality of the contents, that is why implementing Git and TeX is just an unrealistic approach. Great Microsoft Word, you are a legend.

PHP programming

PHP was a great programming language in web development. It surpasses the VBscript for ASP and Perl for CGI. It is favoured because of the syntax based C and C++. It supports procedural programming paradigm and object-oriented paradigm. A lot of functions resemble C functions such as printf, fprintf, sprintf, fopen, etc. Similarly, it can work directly to the C library such as expat, zlib, libxml2, etc. A lot of great content management systems (CMS) are written in PHP, such as Drupal, WordPress, Joomla, etc.

However, a lot of new programming language emerges and surpassing it.

Taken from http://skillprogramming.com/top-rated/php-best-practices-1234

Array is passed by value

Because PHP syntax is very similar to C and C++, it can use “&” reference operator and pass the parameter by reference in a function. But this will be very different from other languages such Python and JavaScript. Python and JavaScript function parameters are passed by value for all primitive data types, such as integer, float, string, and boolean; complex data type like object and array are passed by reference, meaning they are mutable, including Date object in JavaScript.

function change_array($arr) {
    $arr[0] = 100;
}

function change_object($obj) {
    $obj->value = 100;
}

function change_many_objects($arr) {
    $arr[0]->value = 100;
}

function change_object_array($obj) {
    $obj->array[0] = 100;
}

class MyObj {
    var $value;
    var $array;
}

function main() {
    $arr = [1, 2, 3];
    $obj = new MyObj();
    $obj->value = 10;

    change_array($arr);
    change_object($obj);

    echo $arr[0], "\n"; // still 1, not changing
    echo $obj->value, "\n"; // changed to 100

    $arr_obj = [ new MyObj(), new MyObj(), new MyObj() ];
    $arr_obj[0]->value = 10;
    change_many_objects($arr_obj);
    echo $arr_obj[0]->value, "\n"; // changed to 100

    $obj_arr = new MyObj();
    $obj_arr->array = [1, 2, 3];
    change_object_array($obj_arr);
    echo $obj_arr->array[0], "\n"; // changed to 100

    $obj_a = new MyObj();
    $obj_a->value = 10;
    $obj_b = $obj_a;
    $obj_b->value = 20;
    echo $obj_a->value, "\n"; // 20
    echo $obj_b->value, "\n"; // 20

    $obj_c = &$obj_a;
    $obj_c->value = 30;
    echo $obj_a->value, "\n"; // 30
    echo $obj_b->value, "\n"; // 30
    echo $obj_c->value, "\n"; // 30
}

main();

In the example above, the function change_array() will not modify the array that being passed, this is because it is passed by value. Unless we use the “&” reference operator.

The function change_object() will change the object that being passed.

One of the key-points of PHP 5 OOP that is often mentioned is that “objects are passed by references by default”. This is not completely true. […]

(from PHP manual)

So, basically, the function parameters are passed by value, even though it is an array. But the object will be dealt differently. We can treat it as a pointer, if you are familiar with C++ “new” operator. In C++, “new” operator will create an instance and return a pointer to the instance. If we understand this concept, then this is how it works in PHP (according to what I know).

Consequently, the function change_many_objects() though the argument is for an array, and an array is passed into it, but the function changes the value of the object within the array. This is because the array stores the pointer to the object instances. The function does change the instance is pointed by the “pointers” stored in the array.

In summary, PHP deals array as value, which is different from Python, JavaScript, and even C and C++. However, PHP deals object as pointer, that is why object is mutable when it is passed to a function.

Other limitations

PHP was created before the technology of RESTful API. PHP focuses on the GET and POST, but not like PUT and DELETE. The HTTP methods are server dependent. As a result, the HTTP server such as Apache requires some extra configurations for PHP to work. Unlike Node and Ruby on Rails, Node itself has a HTTP module; Ruby on Rails has WEBrick HTTP server.

Comparing to the language like Python, Node with JavaScript, Ruby, Lua, it lacks of REPL (read-eval-print loop). Interactive shell is different from REPL. With REPL, the function to print the result in the console is omitted. REPL will print the result whenever the function returns value.

Closure

In JavaScript, we can create closure like this,

var foo = (() => {
  let x = 0;
  return () => {
    return x++;
  }
})();

for (var i = 0; i < 10; i++) {
  var x = foo();
  console.log(x);
}

But translating above code into C++, it cannot work as expected for the variable, unless the variable is outside the lambda function.

// In a function
int x; // variable here
auto foo = [&x]() {
  x = 0;
  return [&x]() {
    return x++;
  };
}();
for (int i = 0; i < 10; i++) {
  int x = foo();
  cout << x << endl;
}

This is because C++ variable can be only accessed in the function scope. After the function return the value, the variable is not accessible anymore.

Similarly, Python can also return the function, but it cannot work as closure like JavaScript. However, Python can create non-primitive variable such as object or list so that the variable will be accessed by reference.

def _foo():
    x = [0]
    def increment():
        x[0] += 1
        return x[0]
    return increment

foo = _foo()
for i in range(10):
    x = foo()
    print(x)

Prayer Clock GTK3

My first open source project, Prayer Clock, I moved from SourceForge to GitHub recently. Yeah! Everyone should git!!!

And today I just made some changes, and updated to GTK3.

With GTK 3, I removed the title bar. But not yet successfully moving the menu bar to the icon like Evince or Nautilus.

I plan to convert the right hand panel to WebKitGtk. But this will not be the priority yet.