Sunday, June 23, 2013

Evolutionary Design

    A few days ago, I started to write the TextAnalisysTool.Net alternative in linux, which I have done a simple one just by python and grep only for console. This time I want to make it in C++ with qt. But the first step is also to write one thing running in the console also. 

    The 1st problem is that how to design it, it bothered me a few days. How to design the constructor function, how to design the filter function? It just couldn't come up anything in my mind. I even just want to write in pure C, since it's very straight forward. Then I drew my idea like Figure A. Why did I draw something onto a paper, I think because my brain is very small, do not have much image memory, I am seriously. 

    If I can implement Figure A in C, then it's enough. But my requirement in the future is that I want to drag a file into the application, so I need to keep the 'regular expression'. Ha~, then I have to use C++, make a whole thing as an instance, which has member variables and member method.

    I am very pleased that I made this achievement. Yes, you can laugh at me. But I got another thought. If we treat functionA as an object same as the whole item in Figure B, we can say it just evolved. We can design every complex thing from a primitive one, making it evolving by adding requirement to survive.

   After I got this original idea, I think somebody must got this idea long time ago. Then search in the Google, some articles about "Evolutionary Design" came up.  Now I don't have time to read those papers, but I will put it on my agenda.



 Figure A

Figure B

Wednesday, June 5, 2013

Why bother to use 4 types of casting in C++?

Why bother to use 4 types of casting in C++?

Even in The Language C++ programming 3ed does not organize these 4 types casting together.
I am trying to figure out why C++ bothers to use these 4 types of casting. Why 4 types are complete?

By exploring why there are 4 types of casting, then we may know when we can use which one.

The first step I have done is to Google like "why C++ introduce 4 types of casting"?
I could not find a complete answer, most of the tutorials or articles just drop a few lines, then directly jump to teach you how to use these 4 types of casting.

The answer
"The ailments of C-style cast have been known for years. C++ offers a superior alternative in the form of new cast operators. They document the programmer's intent more clearly while preserving the compiler's ability to catch potential bugs like the ones shown above. C++ has four new cast operators" [5] http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=134

Using the new style casts clarifies the programmer’s intent to the compiler and enables the
compiler to catch casting errors.[1]


I don't feel above answer is persuasive, until I read below references. 
==================================================================

Most helpful tutorial of this topic is: 
[1]Appendix I: C++ Casts and Run-Time Type Identification(In the book: starting out with c++ early objects)
It gives me almost complete answer.


These 2 gives an idea is that  A c-style cast can cover all 3 cast operators(const_cast, static_cast, reinterpret_cast), and dynamic_cast gives new function which C-style dose not give.(Probably this idea originally comes from somewhere, but below 2 links did not mention)

[2]http://stackoverflow.com/questions/28002/regular-cast-vs-static-cast-vs-dynamic-cast
[3]http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=134

This one you can verify you thought, when you think you have solid idea of C++ casting:
[4]http://www.cplusplus.com/doc/tutorial/typecasting/

Definition of complete object refers to: http://msdn.microsoft.com/en-us/library/x9wzb5es(v=vs.80).aspx

I just search in the google, there must be some other resources where we can get a complete answer, otherwise in the reference I mentioned above, the authors couldn't present their illustration.

========================================================================
Then I got my summary. I think it can help you also.
Please read all the reference above, or I don't think you can understand my summary. It may takes a few hours, but totally worth to do.

(c-style, dynamic_cast) = ( [const_cast, static_cast, reinterpret_cast],  dynamic_cast)

1. static_cast and reinterpret_cast are distinguished by if there are information at compile time.
    Say class Child derived from class Father, object child can static_cast to object father, vice versa. That's because when we define class Child, we give some information. And in char, long, float ..., the compiler know the information how to cast, so they can apply static_cast to each other.
    But if class Mother and Class Father do not have any relationship of inheritance, then reinterpret_cast is your only choice. This may help if Mother and Father have some similar data arrangement in the class definition, otherwise I don't any sense. This is quite a low level manipulation. When we use it, we must have a bad design.

2. const_cast is only used to cast const variable to non-const. quite simple and not related to other cast operators

3. dynamic_cast is relative to static_cast, they are a pair of Yin-Yang.
static_cast is used to check error at compile time, but dynamic check error at run-time.

With these 3 points and the formula, it's easy to decide when to use a certain cast operator.

========================================================================

There are some other differences between static_cast/dynamic_cast. (don't read this if you fell it's mouthful)

    You can static_cast base object to derived object, or vice versa, but you will see run-time error(eg. use a base pointer to call a member function only defined in derived class ).
    You only can dynamic_cast derived object to base object, unless base class is polymorphic.

    When base class is polymorphic, dynamic_cast<CDerived*>(pbb) will be 0, if pbb not actually point to CDerived object. That is why we use dynamic_cast. With the 0 being returned, we can know if the object derived from the class <CDerived*>. Why is that helpfull and for details, check the reference[1][4].




Friday, May 10, 2013

TextAnalysisTool.Net alternative in GNU/Linux

TextAnalysisTool.Net alternative in GNU/Linux

TextAnalysisTool.Net is a great tool that can filter text line by line with some keywords, by a GUI interface. The greatest part is that you can setup different colors for different keywords. When you want to analyze log with different keywords, that would be very helpful and friendly. But can not find a GNU/Linux alternative, quite bothers me. Though I can use 'grep' and with different colors for keywords, that command line can be very long and tedious. So, I made a python script to do the same thing, which also is made by reusing 'grep', but with a friendly syntax.

Example:


See, for different keyword, it has different color to distinguish. with very simple syntax.

It can be downloaded here, I hope someone can help also:

But now it's only with command line version, no GUI support.

I have made a c++ version, hope you like it. https://github.com/Ning-Zhou/LogAnalysisToolCPP

But you need to include boost regex library, and also Qt 5.0 to build.  I use Qt 5.0 because I will make a GUI version with Qt, so I am using the unit test framework of Qt now.


Saturday, April 27, 2013

Divide one 'big' commit into small ones - Splitting Commit

Generally, you can check http://git-scm.com/docs/git-rebase , go to item SPLITTING COMMITS.
And better, read following thread as reference also, especially the answer gets the most votes http://stackoverflow.com/questions/1440050/how-to-split-last-commit-into-two-in-git

Play around, you will get it.
//TODO, make a example here



That is quite handy, when you forget to save you changes incrementally. 
I will tell a story to show how useful it can be.

In the beginning
    You write a lot of code, test, debug, finally all test case passed. What's the next thing you are going to do, usually save all the changes into a safe place, in case you would delete it by accident - or you just commit it.

But what if someone report a bug to this commit?
    You don't have any idea why this issue can happen. But you want to figure out which statement cause this issue. You might revert your changes 50%, if problem still there, revert your changes 25%, otherwise 75%. With this way, you can narrow down the scope of the code which contains the bug. Without version control tool, you might save the code for (25%, 50%, 75%, 100%) into different directories. Classic! right ? And you still need to copy this changed files into your workspace, that you can build a binary.
    Git save you in this case, you can split one commit into several small ones as you want. When you want to test these small commits, just 'git checkout'.  Safe and clean!

Handy GNU/Linux commands

1. Display size of directory
    du -sh *

2. List files sorted by time
    ls -lt

3.

Saturday, April 20, 2013

when to create a new member to base class?


when to create a new member to base class?
Thinking the thing you are making is evolving, when you find out that some property or behavior is same as other things, it's the time.

Write code incrementally


Write code incrementally

write a bit code - ->  test ->  if passed - - - - - - - - - - - - - - - - - >  commit->write comment->...
                                          \                                                     /
                                             -> if failed->fix it->if passed  -->

'A bit' is quite important, that you can find the code with problem very quickly.
When you use 'git diff', you will not get massive changes showing in the display.

My experience is that even you are working on embedded software domain, which always takes time of 10-15 mins to load the binary to your board/hardware,  still it's worth to do in this way. Or we can say it's worth more than .
So, try GIT, that's absolute a tool you need on hand to commit a little bit.