RSS Feed

Art of Programming

Just like “machine learning”, learning from different data and the machine will get the essential pattern(s) among the data. So, by experiencing various programming languages, different software libraries, and different frameworks, allows me to grow in my programming skills, and strengthen my philosophy of programming.

Programming is a combination of science and art. As science, the code can be replicated, experimented, and always produces same result. As art, the source code can be written according to an individual’s thinking, expression, and emotions.

Instead of using the blog post, I prefer to use “page” in this article, because as my knowledge and experience grow, the philosophy and concept will be changed a little by little.

Preface

To be strong in programming, you need to adopt 逍遥 (xiāo yáo).

Data structure and algorithm

This is the first thing I learnt during my bachelor degree (of Cognitive Science). My favourite lecturer taught us about algorithm and writing pseudocode and writing comments in the source code. This is the best thing. Because by writing algorithm, we are verbalising our thoughts into words. Whatever thing we can express in words, it can be written in programming language. (But this doesn’t mean it can work, because whether it can work is based on your logic!)  Conforming my belief, that is why the world is created by Word!

Data structure. As it is one of the most important UNIX philosophy, Pike (n.d.),

Data dominates. If you’ve chosen the right data structures and organized things well, the algorithms will almost always be self-evident. Data structures, not algorithms, are central to programming.

That is why, designing the data structure is so important that it will have a long term effect. A good design of data structure, allows a stable and mature file format, eg HTML DOM tree.

Object oriented

As C++, object oriented is most powerful and most influential programming paradigm. Almost all modern languages support object oriented, even they are multi-paradigmed programming language. PHP, Java, C#, Python, all of them can be written as object oriented. JavaScript though is prototyped language, object oriented concept can also be applied. Not only language, the C library like GObject allows you to create object in C programming too.

Object oriented, the most powerful features are inheritance and polymorphism. As a result, you can create your own project in rapid development using MVC framework. The framework provides base classes, and we only need to implement specific feature based on the project requirements.

Object oriented, it assumes everything as an object. You have a vehicle, it is an object. Specifically, a car, it is an object of vehicle category. A vehicle has capacity of passenger, it is an attribute/property of an object. It can move, can stop, and these are the methods. Whatever a vehicle can do, your car which is inherited from vehicle can also do. And it can do even more specific.

Therefore, object oriented is a concept, it makes you to view the world in a more logical sense, so that you can implement them into programming.

Encapsulation

Related to OOP, encapsulation hides some information from the developers, to avoid unnecessary manipulation of the properties. Providing everything to the developers doesn’t benefit, because not all functions or methods are needed. So, similar to encapsulation, we sometimes just needs to know how a function is used, instead of how a function works. For example, we never need to know how printf is implemented.

Comments

The beautiful thing in programming language is the ability to write comments. Yeah. Fully utilise this. This will help you to recall how did you come out the solutions, and also allows others to understand your code better. You can use it to write your pseudocode, to write some notes, to-do, or just anything you want to write. By following some documentation syntax like Doxygen, you can generate documentation of your library.

Recursive function

Whether you like if or not, recursive function is so useful that can solve problem with two steps: base step and recursive step. But depends on problem, not all problems can be solved by recursive function. Most prominent situation is the tree-based problem, such as pathfinding, yet not the NP-hard problem. You need it, as the rise of functional programming in the current age.

By using it, you can reduce the usage of do-while loop, for loop, or while loop.

Tree

Tree is my favourite data structure, just like Tree of Life. Scenegraph is a tree, HTML is a tree, XML is a tree, programming language is parsed into parse tree, pathfinding can be represented as search tree.

Pointer, linked list

Pointer is a wondrous concept, with the pointer, you can create your own linked list without library. The tree is based on pointer, JavaScript object works as pointer, mutable works as pointer.

If you know pointer, you can even create circularly linked list. With the pointer, you can create callback function in C.

Function

Write function if necessary, but not beyond the necessity. Write functions, as to reduce the length of a function, so that the reader can read within a single page. Longer page causes reader mental overload, cannot process such long information that requires mental processing, understanding and reasoning.

Too many functions produces too many function calls (call stack) and dependencies. Debugging becomes a depth-first search for a developer.

Balancing is important! That is why, encapsulation plays an important role.

State machine

State machine like OpenGL stores the current state of the machine. Everything is stored as the state. We just use the function to get the current state and perform further calculations.

Functional programming

Contrary to imperative programming, functional programming is a declarative programming paradigm. As the advancement of the technology today, FP emerges because it can eliminate the side effects, that is, the changes of the machine state. That means, it guarantees the same input will always produce the exact output, as in the mathematical function.

The language like JavaScript supports functional programming. Most modern languages are adopting FP characteristics, including C++11 with lambda function.

FP is an ideal solution to fit big data and parallel programming.

Concurrent programming

Perform a task one by one is slower than perform multiple tasks simulatneously. That is what we need in multithreading. But, the data may need to shared among the threads, thus, mutually exclusive and conditional variables are important to avoid race conditions.

Furthermore, because of the data from network, we sometimes cannot guarantee when we will receive the data. This is where futures and promises come in.

Do one thing and do it well

DOTADIW is one of the UNIX philosophy. It is the greatest idea I like. Instead of complicating the solution, focus on one thing, and make sure it works. Though program like busybox do various things, yet it is actually consists of various simple tools. Therefore, inside-out is better than outside-in. Do the planning from top to down; implement the solution from bottom-up.

KISS principle

Keep it simple, stupid! Yes, just keep everything as simple as possible. Do not complicate the problem, so does your solution.

RTFM

Yes. Read the f*cking manual. This is a self learning habit. Some people they like to ask the questions without thinking. They ask the questions without preparation. They didn’t go through the documentation or manual, or read enough examples, or do more testing. They just dropped out the questions to wait for others to help them to solve the problem. Even though they would like to ask the question, they need to trim down the problem as small as possible, and then ask the question. If you don’t RTFM, then people will tell you RTFM!

String and text

Graphical is not my way. The HTML is string, XML is string, JSON is string, HTTP protocol involves string, and commands are string. As UNIX philosophy, “Write programs to handle text streams, because that is a universal interface” (Salus, n.d.). Similarly, Tcl language treats every operation as command. Moreover, your source code is text. That is the text that gives semantics, that is the text that has grammar.

Regular expression and parser

Regular expression is one of the common features among the programming languages. The most common regular expression syntax is Perl, and also adopted by JavaScript, and even C++11. However, one needs to know, regular expression is not a parser. To parse a file with syntax, it needs to be parsed into parse tree. Regular expression is suitable for regular language only. In the format like XML or JSON, they are too complex that regular expression will have a problem of recursion. That is why, XML parser and JSON parser are the actual thing we need when we are doing the parsing.

Logging

As the string and text as a universal interface, logging is so important that can provide sufficient information to the developers to diagnose and debug. By using the log appropriately, the developers can analyse the state of the system through the log and find the bugs easily, without thousands of trials-and-errors.

Pipeline

Pipeline, a chain of data processing to process the input to an output. This is a very interesting solution, and it simplifies the process. For example, UNIX frequently uses pipelines to process the input to a specific output, like

cat myfile.txt | sort | gzip > sorted.txt.gz

Similarly, the technology like DirectShow and GSreamer are also using pipeline design, that from the input source, the data is filtered to produce the target output.

Therefore, instead of object oriented inheritance, we can also process the data in the linear way.

GUI

Though graphical is not my way, GUI is still important to simplify my works. You cannot use your mouse to write the text or do typing. But neither can you use your keyboard to perform a drag-and-drop.

So, other than learning HTML as a web UI, learning a widget toolkit like Qt and GTK+ let you understand the GUI other than web.

Error handling and exception handling

Our code may not cover all the possiblities, but it should cover as wide as possible, so that it can cope when something goes wrong. We need to take into account for these possiblities, and how to handle them. Either popup error message, logging, or deal with it!

TDD

Test driven development and behaviour driven development are the modern development methods, especially web development. Due to the dynamic data typing languages like JavaScript, PHP, Ruby, Python, Perl, the dynamic data type sometimes produce runtime errors because the unexpexted type of input is passed to a function. Therefore, to make the product more stable, proper testing is needed.

TDD is a development method, not a programming paradigm. We need to get the system requirements, then write the test cases, and expecting the desired result. Then only start implementing the code. Firstly implement the code that fails the test, to make sure it will fail, that is to identify the problem. Next, write the code that passes. Finally, refactor it.

It is different from our conventional development method. Conventionally, we implement the solutions, then we test on the solutions. But this may cause difficulty for the testing. Because the code is not written for the test!

Therefore, the code needs to be designed for testing purpose. So that, with the tests, your solutions are stable and robust to any error.

Futhermore, due to the large scope in web solution, which involves user, authentication, logging, email, admin, etc, solving one issue may produce another issue. That is why, running a test suite allows you to know the errors before production. Else delivering buggy products will bring you non technical problems.

MVC

In web development, Model-View-Controller makes development much more easy. The frameworks that provides object-oriented allows you to inherit the base class to start the routes and controllers. The controller works as API to interact with the model and view. Then you design your solution and database structure in the model. With the ORM (object-relational mapping), you need not to understand the low level data structure or using the SQL directly. This advantage allows you to port your model to any database theoretically (though practically not every database implemented with same standard). Finally, you present your solution through the view.

The most interesting part of MVC is the separation of model and view. This allows designer and software developer works separately.

Similarly, MVVM (model-view-view-model) is a recent architecture used by most client side JavaScript frameworks, such as AngularJS and Backbone. VM plays the role to process the model for the view.

Not only web development, widget toolkits like GTK+ and Qt also use model and view design, especially when working on the tree widget. So that, the view is updated according to the model, omitting the implementation of synchronising between the data and output throughout the application.

When dealing with the view, we must know template engine. Template engine should have minimal logic. All the logic should be happened in the model or controller.

Vector, matrix, and tensor

Matrix is the essential of computer graphics; vector is the essential of game physics; tensor is the essential of machine learning.

Game development

I believe most males like to play computer games. For those who play computer games, must have their dream games. Then, develop a game becomes a dream. Yeah. game development requires you to learn physics, computer graphics, multimedia, AI, etc.

Random

In game development and AI, random is a very powerful factor. Random can produce a uniform distribution. We cannot simply guess the outcome, because every event has same probability.

It is used in machine learning weight initialization, genetic algorithm’s selection, crossover, and mutation. It is used in various games.

I strongly believe that it is the crucial factor in artificial creativity.

GPGPU

If you need fast computation to do advance computing such as machine learning, AI research, then you need GPGPU. GPU is not just for gaming, it can be used for general purpose!

%d bloggers like this: