Today I found a pretty annoyance when using STL string class. The find function returns the index if it find the string, and string::npos if it does not. For example,
std::string str("joeliscrazy");
index = str.find("joes");
The find returns a type of string::size_type, which is size_t in most implementations. Now since size_type is unsigned, you no longer can write the following statement
if ( index < 0 ) { }
Unless you define index as a signed type, such as int.
Other string libraries (java, .Net and MFC) use signed type as index, thus avoid this problem. Since most modern platforms provide 32-bit integer, who on the earth require a unsigned int as string index?
Wednesday, August 29, 2007
Tuesday, April 10, 2007
C++ != OO
When debating programming philosophies, many associate specific C++ features, such as data encapsulation with OO. Because of these bla bla, they argued, OO philosophy is superior to functional programming.
Well, there are certain features from C++ are not OO. For example, template and generic programming are not OO. What I think about OO is that this thinking assumes that everything should be repsented with classses and objects.
In the early days in the OO theory, advocates borrowed heavily from data analysis in the databasae theory. The thinking went like that you should treat everything as objects, with their private data as properties, and actions performed as methods. This sounds very well, however, there are at least two problems: a class can virtually have unlimited number of members and methods. Human beings have limits on learning things. When a class grows so big it is difficult to expand and maintain. Another problem is more fundamental. Our programming is to solve real problems, not to model the world. We only need a small part of data and concern on how the data is processed into another digital form. We realy do not care about how many objects we need to have in the analysis.
Well, there are certain features from C++ are not OO. For example, template and generic programming are not OO. What I think about OO is that this thinking assumes that everything should be repsented with classses and objects.
In the early days in the OO theory, advocates borrowed heavily from data analysis in the databasae theory. The thinking went like that you should treat everything as objects, with their private data as properties, and actions performed as methods. This sounds very well, however, there are at least two problems: a class can virtually have unlimited number of members and methods. Human beings have limits on learning things. When a class grows so big it is difficult to expand and maintain. Another problem is more fundamental. Our programming is to solve real problems, not to model the world. We only need a small part of data and concern on how the data is processed into another digital form. We realy do not care about how many objects we need to have in the analysis.
Monday, February 26, 2007
Valid characters for ID and NAME attributes
The W3C defines ID and NAME requirement as -
ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").
So colons is a valid character used inside an ID.
Why do I mention this? We want to find a way to specify unique ID names, even we do not know what the outside HTML will look like. Say we are building a component containing some HTMLs. This component is going to be embeded into other pages. We may consider add a prefix for example
wasterpack::id_error
is a valid ID.
ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").
So colons is a valid character used inside an ID.
Why do I mention this? We want to find a way to specify unique ID names, even we do not know what the outside HTML will look like. Say we are building a component containing some HTMLs. This component is going to be embeded into other pages. We may consider add a prefix for example
wasterpack::id_error
is a valid ID.
Sunday, February 25, 2007
Tricking Microsoft Script Editor to edit PHP files
Microsoft Script Editor is a great tool to edit HTML pages and debug client javascripts. However, it only recognize file with several extensions, and show unformatted pages to .php and .tpl files. It seems that Microsoft does not want you to use this tool to edit PHP and template files. Many people want this feature and have to change the file extension back and forth. Needlessly to say, this is a tedious work and prone to errors.
Today I found that you can hardlink a file under Windows XP, just like the command "ln" on UNIX. A hard linked file looks like a regular file, but points to another file. If you change the contents of one file, the another changes as well. Let's say that we need to edit file index.php, we can create a hardlink file called index.html. Now every changes made to index.html go into index.php. In this way we can edit file contents in Frontpage or Script Editor, but test it with index.php.
The command to create a hardlink file is
fsutil hardlink create index.html index.php
Note that the new file name is before the old file name.
Today I found that you can hardlink a file under Windows XP, just like the command "ln" on UNIX. A hard linked file looks like a regular file, but points to another file. If you change the contents of one file, the another changes as well. Let's say that we need to edit file index.php, we can create a hardlink file called index.html. Now every changes made to index.html go into index.php. In this way we can edit file contents in Frontpage or Script Editor, but test it with index.php.
The command to create a hardlink file is
fsutil hardlink create index.html index.php
Note that the new file name is before the old file name.
Thursday, February 15, 2007
Ternary Operator - multiple branches
I use ternary operator ?: in C++ a lot. Unfortunately I never went further to use it on multiple branches. Today I found that it is legal to write
char* str = size < 10 ? "small" :
size < 20 ? "medium" :
size < 30? "large" :
"extra large";
It is equivalent as:
if ( size < 10 )
str = "small";
else if (size <20)
str = "medium";
else if (size < 30)
str = "large";
else
str = "extra large";
Nicer, uuuhhh
char* str = size < 10 ? "small" :
size < 20 ? "medium" :
size < 30? "large" :
"extra large";
It is equivalent as:
if ( size < 10 )
str = "small";
else if (size <20)
str = "medium";
else if (size < 30)
str = "large";
else
str = "extra large";
Nicer, uuuhhh
Sunday, February 11, 2007
ATL Server is much faster than Script
Today I spent about six hours rewriting an ASP page with C++. The existing page was too slow - it took a whoopee 10 seconds to load. It is an ASP page written in VBScript. A lot of database code inside. Theoretically we could optimize the database queries to make it faster; but I decided to rewrite in C++ instead.
It turns out much better result than I expected. I am a C++ programmer. C++ is a much better language in many tasks, but I never thought that it is also useful for creating a robust web application.
An ATL server project includes two parts - a SRF file and a web application DLL. This arrangement makes the separation of presentation (UI) and logic possible. I have not thought about this before; but I can also build logic blocks and UI blocks as well. The SRF syntax is a little bit limited; but it does support the include syntax which allows you to include a static HTML file or another SRF.
It turns out much better result than I expected. I am a C++ programmer. C++ is a much better language in many tasks, but I never thought that it is also useful for creating a robust web application.
An ATL server project includes two parts - a SRF file and a web application DLL. This arrangement makes the separation of presentation (UI) and logic possible. I have not thought about this before; but I can also build logic blocks and UI blocks as well. The SRF syntax is a little bit limited; but it does support the include syntax which allows you to include a static HTML file or another SRF.
Subscribe to:
Posts (Atom)