Pages

Thursday, December 25, 2008

Make it work or Make it fast/optimized?

Most of time this issue struck my brain during my coding. Should i make it work first? Or should i optimized along the way?

It is easy to think that everything should be made to worked first, then optimized. It's normal (and thinkable, of course). We have to, since anything that we do is because we want to see it work. Otherwise it'll not be useful at all.

But do not forget that programming/coding involved a lot of works. Normally we are not writing 2-10 lines of codes but hundreds. Or maybe thousands (millions? are you writing another Vista?). So to review all these codes for optimization might take us several hours or days. And this if our working code can go along the optimization process.

But what if they not? What if we are writing the code using algorithm A, and suddenly we found that the optimization require algorithm B?! Isn't that would be burdening, redundant work, since we have to make it using B, make it work, and then optimize?

So these thing make my head spin fast, trying to solve these work-or-optimize issue ...

hmmmm.... but for now i just simply make it worked first (since i'm an ordinary programmer)...

Wednesday, December 24, 2008

Regular Expression (RE) as PL ?

Just a duplicate of my ol' blog posting in http://www.regexadvice.com :

Regular expression as a programming language. Is it possible?

I began to learn about regular expression about 3 years ago. I was at that time, never heard about, or even expect to know such a thing. I was asked to learn Perl to solve some Bioinformatics work, and in my mind i don't have any regex knowledge, except mathematical statements like x = { y| y is subset of z}, or y = {1,2,3}, and some basic knowledge of Z-language. Although the math statements did not closely resembles the regular expression statements that are used in programming language, its foundation is still a 'regular expression' (since regular expression is about stating the regular behavior of the item that we want to specify). We assure y = {1,2,3} by /[1-3]/. Anyway, from that point of time, i began to learn Perl, and slowly i was introduced to m// operator, s/// and tr/// (text processing requires a massive use of these operators).

What i like about regular expression is its compactness. Techniques for simplifying codes have been explored long time before. One approach is by using function (which is eventually another concept that come from math). Using function (or some use the word subroutine), we manage to reduce codes, and simplify them just by calling their name instead of rewriting the same codes. Almost with a similar purpose in mind, we use regex to simplify complex requirements, which is by representing a set of rules within a simple statement, i.e. /a-z/. One should realize that we are representing many lines of codes within a single statement. Just imagine, using regular expression as programming language, a million lines of codes can be turned into just several lines of codes (or symbols).

I'm also believe that regular expression can possibly be a language that is easier to remember, and can be written faster. This because in regular expression we use simple symbols to represent (possibly) complex rules. It have been proved that our brains can (easily) remember things that we see visually compared to the things that are written or touched. Also human brains will capture things in graphical form. Based on this fact, isn't it possible that we can remember some simple symbols more faster than to remember huge amount of text? Also since we only need to write symbols, it will not take us a long time to write the codes in regular expression (unless for complex rules).

Regular expression is specified using a finite set of symbols, such as '?' to represent existence, '+' to represent repetition etc, make it looks more encrypted. Programming language was created to bring computer language (machine language) more closer to the natural language, so that it will become easier for people to write codes to be computer programs. Based on this fact, it seem impossible for encrypted code like regular expression to be accepted as one of high programming language.However, it is not an excuse. Even most of programming language today require some comments to clarify its purpose, or explain what the code does. People might claims that some high level language is already self-explained (the codes explains its purpose). However many of us will found that this statement is not true for all cases. When a section of codes becomes so complex, even the most proclaimed self-explanatory language require at least few comments to describe the codes. Some of todays implemented regular expressions allows comments to be included in the regular expression statement. So it is not encrypted at all when the regular expression are combined with some extra comments. In implementation, no different in code size since comments will be ignored.

I'm just writing the general ideas of how regular expression can possibly be a programming language here. There's still a lot of things that need to be considered, studied and experimented with. But I'm still hoping for this idea to become true.

Guess what? Randal Schwartz (the Perl hacker) posted a comment on it! :)
He said:
Abigail uses regex to compute prime numbers. Ovid created a prolog-like problem solver using the regex engine. The regex engine is quite versatile, especially with built-in back-tracking.
And then later i added more updates:

I'm thinking about the advantage of natural (written) language over mathematical notations (and vice versa). Since (most) natural language statements can be simplified using mathematical notation e.g. like 'one plus two is equal to three' and '1+2=3'. But somehow there's a situation where mathematical notations gets longer than natural language. For example (unfortunately i can't show the mathematical expression), when we need to describe the relations of elements between sets, i found it is more convenient to describe it using simple sentence. But i arrive at a conclusion that, this 'advantages' over another is simply because there are no simple notation in either languages (neither natural nor mathematical) to describe the semantics of another language. If we need to describe certain aspect of a language (X), in another language (Y), we must define a notation (in Y) which describe the semantic described by other language (X) in similar fashion, which is as precise and understandable (same complexity in interpretation) as the original (X). So when we convert into the target language (Y), we gets a 'similar' complexity with the original (X), but with a new notation.

This idea basically was applied in regex to natural language, except with a different complexity. This complexity increased because regex tends to describe a pattern (many characteristics and semantics in a minimum notation) of natural text, not directly one-to-one interpretation (i mean the whole expression, not the symbol). If we want to allow regex to be able to describe a semantic of program (or as programming language), there should be many (not all) one-to-one correspondence between regex statement and computer program command.

Well, that's all. I haven't found anymore idea than this (yet!)

Yeah, that's all (since i never updated it). The last update is on June 12th, 2008.

Saturday, December 20, 2008

Monday, December 15, 2008

Perl on news

Hmmm... i just read on latest news about Perl, and i found this interesting quote:

As noted by many Perl fans (like I am) you do not write in Perl - you think in Perl. It is language without any artificial barriers between you and resources you need to accomplish your task.


well... not entirely agree, but agree.

-EOF

Friday, December 05, 2008

On 'inline' in C++

Another question asked, 'what and when to use inline?'

Again, IMHO, inline in C++ was supposed to replace macro function in C. Basically inline support 'macro'ing function with syntactical check on formal parameter. So it is better than creating macro (#define).

Any occurrence of inlined function call, will be replaced automatically with the whole body of the inlined function. This is done automatically during compilation. The advantage - improve speed. Disadvantage - size increased.

The advantage of inline is for runtime optimization. Since during coding or writing code, there's no difference of inlined and normal function, other than the word 'inline'.

When to use?
When our program prioritize runtime performance, use inline. But the disadvantage, it might increase your program's size. But if the only thing matters is runtime storage, don't use inline, since our program tend to be in minimal size without inline.

Rule-of-thumb (ask Mr Stroustrup):
no matter what, if the function body exceeds than 5 (major) lines, never use inline. Use normal function call instead. Since the performance of executing instructions within that size might be comparable to using normal function call (in fact might be slower). So why bother to increase program size without gaining any advantage in performance (or worse, make them slow)?

use it wisely.

-EOF

About 'const' in object-oriented paradigm

Last week, my colleague ask me, 'what is the purpose of const in enforcing least privilege in C++?' (referring to formal parameter of function)

At first, i'm quite surprised to heard this term 'least privilege'. But then after googling i found on WIKI that this refers to 'limiting access to resources' or known as 'principle of least privilege'.

So IMHO, in C++, the used of const on formal parameter is to :
1) avoid the function from modifying its parameter's (argument's) value
2) as good indication to simplify programming in object-oriented, since we don't have to go through the whole function to verify that the function don't modify the argument

There are more uses of const in C++. Check your documentation for details. But i lists some of them here:

1) declare constant variable.
2) const on (global) function declaration is to allow the function to access constant variable.
3) const on class method's declaration is to indicate that the function (at least) used class' property(s), but never modifies any of it.

-EOF

Thursday, December 04, 2008

Modularity and refactoring is a law?

Modularity and refactoring relate with modular programming, which break our program into modules. Refactoring (break into small factors/parts) refers to the technique, and modularity refers to the property (IMHO, high modularity means low dependency).

Normally this is advised when we do structured or OO programming. Kind of rule-of-thumb in software development. The benefits:
1) reduce complexity
2) simplified program structure
3) etc.

In my case, this paradigm was kind of golden egg!

Last night while i was writing my C program, i found that somehow this is not an option. In my program, I want to represents my data using 2 bytes of char, which contains a row of bits, like follows:

[ 7 6 5 4 3 2 1 0 ][ 7 6 5 4 3 2 1 0 ]
--------1----------------2--------

Each bit represent a flag (1-16). To set a flag, i assign a number e.g. 4 for 4th flag in (3rd in 1), and 15 for 15th flag (6th in 2).

Luckily, i use modules. Module A to allocate appropriate flag (bit), and module B to split value into bytes. However, to calculate the correct bit index, i have to delegate the job to module A, since i can't put the calculation to B. Although it might be possible, it may becomes too complex.

Why bother to have A or B? Why not just one? (someone might ask)
Function A (calculate bit index) repeated multiple times. And called in B. My point is, the calculation needs to be put in appropriate place (in A, and not B). More important, the formulae is easily revised and traced ('cause i remember it in A). So modularity saves the day (or night)!

ps: btw (if u might ask), i'm developing a transition table for transducer.

-EOF

First - include (not prelude)

Welcome to my world of programming. I try to make my entries as simple as possible. And sorry if i don't.

Here I'm going to jot down my experience during my next programming's journey for my own future reference, and might be for u too. Note, posting here won't reflect any programming language, unless regarding technical issues on certain language. But i do program using C, C++, Perl. Others seldom (HTML, Basic, Javascript, Java) or none.

programming-by-xp means by experience, not XP! (i'm not biased towards any OS). Stay neutral. All for any languages and any languages for all.

Enjoy.

p/s:
-Include is the first thing to do in C programming
-Ah, btw, i rethink my decision. Some of the posts might biased to C, since i'm doing lots of C programming lately.

-EOF