tag:blogger.com,1999:blog-64871005815080984332024-03-14T02:11:34.187-07:00Kaisar's blogTechnologies, computing and programming are my passions. I talk about these passions here.<br>Kaisarhttp://www.blogger.com/profile/07878693490452605144noreply@blogger.comBlogger37125tag:blogger.com,1999:blog-6487100581508098433.post-28135846685963468922011-06-03T09:14:00.000-07:002011-06-03T09:14:15.085-07:00Linux running on JavaScript<div dir="ltr" style="text-align: left;" trbidi="on"><div style="text-align: left;">Suddenly came across this amazing project.. Linux running ‘on’ the web browser. It’s a virtual machine written in JavaScript, it can boot a small linux image supporting basic commands, file system, vi, and even.. a C compiler!<br />
<br />
URL: <a href="http://bellard.org/jslinux/">http://bellard.org/jslinux/</a><br />
<br />
You can edit, compile and run the sample hello.c program using the compiler called tcc.<br />
<br />
To compile: tcc -o hello hello.c<br />
To run: ./hello<br />
<br />
All that running on top of JavaScript!.. absolutely mind blowing!<br />
<br />
By the way, only the following browsers are supported:</div><ul style="text-align: left;"><li>Firefox 4.x</li>
<li>Chrome 11</li>
<li>Opera 11.11</li>
<li>Internet Explorer 9</li>
</ul></div>Kaisarhttp://www.blogger.com/profile/07878693490452605144noreply@blogger.com1tag:blogger.com,1999:blog-6487100581508098433.post-37688316843620653162011-04-24T14:38:00.000-07:002011-04-24T15:57:28.371-07:00C++: Is 'this' safe in the constructor?I've been working hard on something very interesting during the Easter holidays. Wanted to take a quick break, listen to some good music and make a blog post. Alright, my favorite Sony earbuds are on and queued the songs. Now here goes the quick post.<br /><br />Very often C++ beginners get confused about when and how 'this' pointer can be used. Most common confusion is probably:<br /><br />Is it safe to use 'this' in the constructor?<br /><br />Having the confusion, many amateurs avoid using 'this' pointer in constructor. Well, such confusion is harmful for the programmer, project and the company funding the project.<br /><br />The short answer to the question is: yes, 'this' is very safe in the constructor. All members (or objects) derived (from parent class in case of inheritance) and defined in the class 'will be' allocated and initialized before the constructor gets executed. So you can safely access the members of the class in the constructor.<br /><br />Now, that was a short answer. For a comprehensive one, we need to discuss in what order objects are created and destroyed and how to use the RAII (Resource Acquisition Is Initialization) idiom. I'll try to make this the topic of my next post.<br /><br />For a brief discussion on how objects are initialized, you can read this post:<br /><a href="http://kaisar-haque.blogspot.com/2008/06/c-avoid-using-assignments-in.html">http://kaisar-haque.blogspot.com/2008/06/c-avoid-using-assignments-in.html</a><br /><br />Talking about 'this' reminds me of the 'suicidal code'. Six years ago, I have seen the following use of 'this' in a very important server component:<br /><br />//now commit suicide<br />delete this;<br /><br />Needless to say that's an architectural disaster. Many of the times we see such disasters in codes of fresh grades. A good way to avoid this is to train your new team mates on idioms like RAII.<br /><br />Stay tuned for my next post :).Kaisarhttp://www.blogger.com/profile/07878693490452605144noreply@blogger.com4tag:blogger.com,1999:blog-6487100581508098433.post-35914752200666339272011-04-15T15:02:00.000-07:002011-04-15T16:02:35.008-07:00C++ is not CC++ compilers (g++ or visual c++ for example) can compile many C codes, but not necessarily all of them. Here is a simple but lame example:<br /><br /><pre><span style="color:Blue;">int</span> <span style="color:Blue;">class</span>, <span style="color:Blue;">template</span>;</pre><br />Above code compiles fine with a C code but not with C++, as 'class' and 'template' are keywords in C++. But again, it's really a lame example.<br /><br />A good example is the feature set of C99. Here are some codes that are valid in C but not in C++:<br /><br /><pre><font color='Blue'>int</font> vec[<font color='Maroon'>5</font>] = { [<font color='Maroon'>1</font>]=<font color='Maroon'>10</font>, [<font color='Maroon'>3</font>]=<font color='Maroon'>20</font> }; <font color='Green'>// designated initializers</font><br /><br /><font color='Blue'>typedef</font> <font color='Blue'>struct</font><br />{<br /> <font color='Blue'>char</font> name[<font color='Maroon'>20</font>];<br /> <font color='Blue'>int</font> ID;<br /> <font color='Blue'>int</font> age;<br />}Employee;<br /><br />Employee emp = {.ID = <font color='Maroon'>0</font>, .age = <font color='Maroon'>0</font>};<br /><br /><font color='Blue'>int</font> main()<br />{<br />}<br /></pre><br />The reason for not having the support for the C99 code in some major C++ compilers is that C++ was standardized in 98 and C99 standards came after that.<br /><br />Will try to post more on this later.Kaisarhttp://www.blogger.com/profile/07878693490452605144noreply@blogger.com1tag:blogger.com,1999:blog-6487100581508098433.post-50850134712444087912011-04-15T15:00:00.000-07:002011-04-15T16:03:03.617-07:00C++: WorldLightWordLight is a nice little add-in for Visual Studio 2008. When you select some text in the editor, it searches and highlights the same string in rest of the code.<br /><br />URL: <a href="http://code.google.com/p/wordlight/">http://code.google.com/p/wordlight/</a><br /><br />If you have VisualAssist, you may not need this. Otherwise, you'll enjoy this add-in.Kaisarhttp://www.blogger.com/profile/07878693490452605144noreply@blogger.com1tag:blogger.com,1999:blog-6487100581508098433.post-73001691306918945742011-04-15T14:56:00.000-07:002011-04-17T14:53:00.554-07:00Windows Directory StatisticsThe terabytes of hard drives in the market rarely makes us concerned about disk space utilization. However we don't get that luxury for solid state drives. I was running out of disk space with the 128 GB SSD in my work PC and needed to clean up a bit. This tool helped me to do that better.<br /><br />Windows Directory Statistics<br />URL: <a href="http://sourceforge.net/projects/windirstat">http://sourceforge.net/projects/windirstat</a>/<br /><br />It helps you to understand disk utilization and clean up space more easily. It shows disk, file and directory sizes in a treelist as well as graphically in a treemap.Kaisarhttp://www.blogger.com/profile/07878693490452605144noreply@blogger.com2tag:blogger.com,1999:blog-6487100581508098433.post-49466813244128671982010-06-21T04:17:00.000-07:002011-04-15T15:54:47.130-07:00C++: Convert string to int or vice-versastrinstream is very useful for many basic jobs with strings, for example parsing numeric data from a string, converting a string to int/float, converting int/float to string, etc.<br /><br />Here goes examples of how to use stringstream to convert to and from strings.<br /><br /><pre>#<span style="color:Blue;">include</span> <<span style="color:Blue;">iostream</span>><br />#<span style="color:Blue;">include</span> <sstream><br /><br /><span style="color:Blue;">using</span> <span style="color:Blue;">namespace</span> <span style="color:Blue;">std</span>;<br /><br /><span style="color:Blue;">int</span> main()<br />{<br /> stringstream ss;<br /> <span style="color:Blue;">string</span> s;<br /> <span style="color:Blue;">int</span> n;<br /><br /> <span style="color:Green;">//to convert int to string</span><br /> n = <span style="color:Maroon;">1024</span>;<br /><br /> ss.clear();<br /> ss << n; <span style="color:Green;">//write the int to stringstream</span><br /> ss >> s; <span style="color:Green;">//read back the value as a string</span><br /> <span style="color:Blue;">cout</span> << <span style="color:Maroon;">"string value: "</span> << s << <span style="color:Blue;">endl</span>;<br /><br /> <span style="color:Green;">//to convert int to string</span><br /> s = <span style="color:Maroon;">"2048"</span>;<br /><br /> ss.clear();<br /> ss << s; <span style="color:Green;">//write the string to stringstream</span><br /> ss >> n; <span style="color:Green;">//read back the value as an int</span><br /> <span style="color:Blue;">cout</span> << <span style="color:Maroon;">"int value: "</span> << n << <span style="color:Blue;">endl</span>;<br /><br />}<br /></pre><br />Have fun!Kaisarhttp://www.blogger.com/profile/07878693490452605144noreply@blogger.com3tag:blogger.com,1999:blog-6487100581508098433.post-19394311405065644512010-06-21T02:38:00.000-07:002010-06-21T03:21:23.663-07:00C++: Failed ConditionsAt ReliSource, we used to have a mailing list called CPP in which we submitted C/C++ related facts/questions (and solutions). I'll share some of those facts/questions in my blog.<br /><br />Here goes a quick C/C++ problem that I'm sure you will find interesting.<br /><br />// x is a numeric variable (of built in/intrinsic data type).<br /><br />if (x > 10)<br />{<br /> cout << "x is > 10\n";<br />}<br />else if (x <= 10)<br />{<br /> cout << "x is <= 10\n";<br />}<br /><br />For some reason, the program didn't output anything, none of the if or else-if was executed. What can be the reason?<br /><br />You shall find the answer in the comments after a week of this post (if someone already doesn't answer it).Kaisarhttp://www.blogger.com/profile/07878693490452605144noreply@blogger.com7tag:blogger.com,1999:blog-6487100581508098433.post-3567521178414802062010-02-11T22:15:00.000-08:002010-02-11T22:22:58.362-08:00An excellent article on interviewingFollowing is an excellent article on interviewing:<br /><br /><a href="http://www.joelonsoftware.com/articles/GuerrillaInterviewing3.html">http://www.joelonsoftware.com/articles/GuerrillaInterviewing3.html</a><br /><br />A colleague of mine referred to this article while we were discussing how to improve our interview process. It's a long one but quite worth reading. I felt like these are the words of my mind.<br /><br />Following are some excerpts for those who are feeling lazy and/or busy :).<br /><br />===Excerpts start===<br /><br />You’re going to see three types of people in your interviews. At one end of the scale, there are the unwashed masses, lacking even the most basic skills for this job. They are easy to ferret out and eliminate, often just by asking two or three quick questions. At the other extreme you’ve got your brilliant superstars who write lisp compilers for fun, in a weekend, in Assembler for the Nintendo DS. And in the middle, you have a large number of “maybes” who seem like they might just be able to contribute something. The trick is telling the difference between the superstars and the maybes, because the secret is that you don’t want to hire any of the maybes. Ever.<br /><br />---<br /><br />How do you detect smart in an interview? The first good sign is that you don’t have to explain things over and over again. The conversation just flows. Often, the candidate says something that shows real insight, or brains, or mental acuity. So an important part of the interview is creating a situation where someone can show you how smart they are.<br /><br />---<br /><br />The worst kind of interviewer is the blowhard. That’s the kind who blabs the whole time and barely leaves the candidate time to say, “yes, that’s so true, I couldn’t agree with you more.” Blowhards hire everyone; they think that the candidate must be smart because “he thinks so much like me!”<br /><br />The second worst kind of interviewer is the Quiz Show Interviewer. This is the kind of person who thinks that smart means “knows a lot of facts.” They just ask a bunch of trivia questions about programming and give points for correct answers. Just for fun, here is the worst interview question on Earth: “What’s the difference between varchar and varchar2 in Oracle 8i?” This is a terrible question. There is no possible, imaginable correlation between people that know that particular piece of trivia and people that you want to hire. Who cares what the difference is? You can find out online in about fifteen seconds!<br /><br />----<br /><br />What should you look for during the open ended questions?<br />One: Look for passion. Smart people are passionate about the projects they work on. They get very excited talking about the subject. They talk quickly, and get animated.<br /><br />---<br /><br />These softball questions seem too easy, so when I first started asking them, I had to admit that I really expected everyone to sail right through them. What I discovered was that everybody solved the problem, but there was a lot of variation in how long it took them to solve.<br /><br />---<br /><br />Serge Lang, a math professor at Yale, used to give his Calculus students a fairly simple algebra problem on the first day of classes, one which almost everyone could solve, but some of them solved it as quickly as they could write while others took a while, and Professor Lang claimed that all of the students who solved the problem as quickly as they could write would get an A in the Calculus course, and all the others wouldn’t. The speed with which they solved a simple algebra problem was as good a predictor of the final grade in Calculus as a whole semester of homework, tests, midterms, and a final.<br /><br />You see, if you can’t whiz through the easy stuff at 100 m.p.h., you’re never gonna get the advanced stuff.<br /><br />---<br /><br />15 years of experience interviewing programmers has convinced me that the best programmers all have an easy aptitude for dealing with multiple levels of abstraction simultaneously. In programming, that means specifically that they have no problem with recursion (which involves holding in your head multiple levels of the call stack at the same time) or complex pointer-based algorithms (where the address of an object is sort of like an abstract representation of the object itself).<br /><br />I’ve come to realize that understanding pointers in C is not a skill, it’s an aptitude. In first year computer science classes, there are always about 200 kids at the beginning of the semester, all of whom wrote complex adventure games in BASIC for their PCs when they were 4 years old. They are having a good ol’ time learning C or Pascal in college, until one day they professor introduces pointers, and suddenly, they don’t get it. They just don’t understand anything any more.<br /><br />For some reason most people seem to be born without the part of the brain that understands pointers. Pointers require a complex form of doubly-indirected thinking that some people just can’t do, and it’s pretty crucial to good programming.<br /><br />---<br /><br />A lot of the “script jocks” who started programming by copying JavaScript snippets into their web pages and went on to learn Perl never learned about pointers, and they can never quite produce code of the quality you need.<br /><br />---<br /><br />Sadly, despite the fact that I think that all good programmers should be able to handle recursion and pointers, and that this is an excellent way to tell if someone is a good programmer, the truth is that these days, programming languages have almost completely made that specific art unnecessary.<br /><br />---<br /><br />A lot of programmers that you might interview these days are apt to consider recursion, pointers, and even data structures to be a silly implementation detail which has been abstracted away by today’s many happy programming languages. “When was the last time you had to write a sorting algorithm?” they snicker.<br /><br />Still, I don’t really care. I want my ER doctor to understand anatomy, even if all she has to do is put the computerized defibrillator nodes on my chest and push the big red button, and I want programmers to know programming down to the CPU level, even if Ruby on Rails does read your mind and build a complete Web 2.0 social collaborative networking site for you with three clicks of the mouse.<br /><br />===end===Kaisarhttp://www.blogger.com/profile/07878693490452605144noreply@blogger.com0tag:blogger.com,1999:blog-6487100581508098433.post-79398125096384204472009-09-07T03:35:00.000-07:002009-09-07T04:00:02.054-07:00Maemo 5 - Linux at the core of next generation mobile OSIt's been long since I felt excited about any new mobile OS. Maemo 5 seems to be a very promising mobile OS for the next generation of mobile devices.<br /><br />Multiple desktops, multitasking, Mozilla based browser, desktop widgets, cool UI, speed all seems to be best of what other OS has to offer. And guess what, it runs on Linux. The Symbian OS 9.4, Windows Mobile 6.5 or iPhone OS 3.. all seem to be moving ahead somewhat slowly. Comparing to them, Maemo 5 seems to be a leap ahead. It seems to be the next best thing after iPhone OS 1 came out thousands of years ago :).<br /><br />Nokia will release N900 in a few months, the company's first phone running on Maemo 5. N900 itself has good spec with 600 MHz ARM processor, 256 MB RAM, 32 GB ROM, high-end cam, etc.<br /><br />You can check more about Maemo at: <a href="http://maemo.nokia.com/">http://maemo.nokia.com/</a> and more about N900 at <a href="http://maemo.nokia.com/n900/">http://maemo.nokia.com/n900/</a>.Kaisarhttp://www.blogger.com/profile/07878693490452605144noreply@blogger.com9tag:blogger.com,1999:blog-6487100581508098433.post-7994176044800705932009-02-18T12:12:00.000-08:002009-02-18T13:07:57.568-08:00C++: Pointer to membersPointer to member operators are not an everyday operators that we use but those can be useful if used in the correct way. Pointer to member operator ::* is used to declare a pointer that points to a member variable, pointer or function of a class. The operators .* and ->* are used to access it.<br /><br />Following is an example of how we can use Pointer to member function (also called member function pointer).<br /><br /><pre><span style="color:Teal;"> 1 </span>#<span style="color:Blue;">include</span> <<span style="color:Blue;">iostream</span>><br /><span style="color:Teal;"> 2 </span><span style="color:Blue;">using</span> <span style="color:Blue;">namespace</span> <span style="color:Blue;">std</span>;<br /><span style="color:Teal;"> 3 </span><br /><span style="color:Teal;"> 4 </span><span style="color:Blue;">class</span> A{<br /><span style="color:Teal;"> 5 </span><span style="color:Blue;">public</span>:<br /><span style="color:Teal;"> 6 </span> A(){<br /><span style="color:Teal;"> 7 </span> }<br /><span style="color:Teal;"> 8 </span> <span style="color:Blue;">int</span> Func(<span style="color:Blue;">int</span> a){<br /><span style="color:Teal;"> 9 </span> <span style="color:Blue;">return</span> a * <span style="color:Maroon;">2</span>;<br /><span style="color:Teal;"> 10 </span> }<br /><span style="color:Teal;"> 11 </span><br /><span style="color:Teal;"> 12 </span>};<br /><span style="color:Teal;"> 13 </span><br /><span style="color:Teal;"> 14 </span><span style="color:Blue;">int</span> main(){<br /><span style="color:Teal;"> 15 </span> <span style="color:Blue;">int</span> (A::*fp)(<span style="color:Blue;">int</span>) = &A::Func;<br /><span style="color:Teal;"> 16 </span><br /><span style="color:Teal;"> 17 </span> A a;<br /><span style="color:Teal;"> 18 </span> <span style="color:Blue;">cout</span> << (a.*fp)(<span style="color:Maroon;">10</span>) << <span style="color:Blue;">endl</span>;<br /><span style="color:Teal;"> 19 </span><br /><span style="color:Teal;"> 20 </span> <span style="color:Blue;">return</span> <span style="color:Maroon;">0</span>;<br /><span style="color:Teal;"> 21 </span>}<br /><span style="color:Teal;"> 22<br /><br /></span></pre>Following is another (almost random) example of how we can write generic classes to wrap pointer to member function to use with multiple classes.<br /><br /><pre><span style="color:Teal;"> 1 </span>#<span style="color:Blue;">include</span> <<span style="color:Blue;">iostream</span>><br /><span style="color:Teal;"> 2 </span><span style="color:Blue;">using</span> <span style="color:Blue;">namespace</span> <span style="color:Blue;">std</span>;<br /><span style="color:Teal;"> 3 </span><br /><span style="color:Teal;"> 4 </span><span style="color:Blue;">class</span> A{<br /><span style="color:Teal;"> 5 </span><span style="color:Blue;">public</span>:<br /><span style="color:Teal;"> 6 </span> <span style="color:Blue;">int</span> Func1(<span style="color:Blue;">int</span> a){<br /><span style="color:Teal;"> 7 </span> <span style="color:Blue;">return</span> a * <span style="color:Maroon;">2</span>;<br /><span style="color:Teal;"> 8 </span> }<br /><span style="color:Teal;"> 9 </span><br /><span style="color:Teal;"> 10 </span>};<br /><span style="color:Teal;"> 11 </span><br /><span style="color:Teal;"> 12 </span><span style="color:Blue;">class</span> B{<br /><span style="color:Teal;"> 13 </span><span style="color:Blue;">public</span>:<br /><span style="color:Teal;"> 14 </span> <span style="color:Blue;">int</span> Func2(<span style="color:Blue;">int</span> a){<br /><span style="color:Teal;"> 15 </span> <span style="color:Blue;">return</span> a / <span style="color:Maroon;">2</span>;<br /><span style="color:Teal;"> 16 </span> }<br /><span style="color:Teal;"> 17 </span><br /><span style="color:Teal;"> 18 </span>};<br /><span style="color:Teal;"> 19 </span><br /><span style="color:Teal;"> 20 </span><span style="color:Blue;">template</span> <<span style="color:Blue;">class</span> T><br /><span style="color:Teal;"> 21 </span><span style="color:Blue;">class</span> MemFnPtr<br /><span style="color:Teal;"> 22 </span>{<br /><span style="color:Teal;"> 23 </span><span style="color:Blue;">public</span>:<br /><span style="color:Teal;"> 24 </span> MemFnPtr(<span style="color:Blue;">int</span> (T::*fp)(<span style="color:Blue;">int</span>), T &t) : m_fp(fp), m_t(t){<br /><span style="color:Teal;"> 25 </span> }<br /><span style="color:Teal;"> 26 </span> <span style="color:Blue;">int</span> Fn(<span style="color:Blue;">int</span> n)<br /><span style="color:Teal;"> 27 </span> {<br /><span style="color:Teal;"> 28 </span> <span style="color:Blue;">return</span> ((m_t).*(m_fp))(n);<br /><span style="color:Teal;"> 29 </span> }<br /><span style="color:Teal;"> 30 </span> <br /><span style="color:Teal;"> 31 </span> <span style="color:Green;">//member vars</span><br /><span style="color:Teal;"> 32 </span> <span style="color:Blue;">int</span> (T::*m_fp)(<span style="color:Blue;">int</span>);<br /><span style="color:Teal;"> 33 </span> T &m_t;<br /><span style="color:Teal;"> 34 </span>};<br /><span style="color:Teal;"> 35 </span><br /><span style="color:Teal;"> 36 </span><span style="color:Blue;">int</span> main(){<br /><span style="color:Teal;"> 37 </span> A a_obj;<br /><span style="color:Teal;"> 38 </span> <br /><span style="color:Teal;"> 39 </span> MemFnPtr<A> fp_a(&A::Func1, a_obj);<br /><span style="color:Teal;"> 40 </span> <span style="color:Blue;">cout</span> << fp_a.Fn(<span style="color:Maroon;">10</span>) << <span style="color:Blue;">endl</span>;<br /><span style="color:Teal;"> 41 </span><br /><span style="color:Teal;"> 42 </span> B b_obj;<br /><span style="color:Teal;"> 43 </span><br /><span style="color:Teal;"> 44 </span> MemFnPtr<B> fp_b(&B::Func2, b_obj);<br /><span style="color:Teal;"> 45 </span> <span style="color:Blue;">cout</span> << fp_b.Fn(<span style="color:Maroon;">10</span>) << <span style="color:Blue;">endl</span>;<br /><span style="color:Teal;"> 46 </span><br /><span style="color:Teal;"> 47 </span> <span style="color:Blue;">return</span> <span style="color:Maroon;">0</span>;<br /><span style="color:Teal;"> 48 </span>}<br /><span style="color:Teal;"> 49 </span><br /><br /></pre>Kaisarhttp://www.blogger.com/profile/07878693490452605144noreply@blogger.com3tag:blogger.com,1999:blog-6487100581508098433.post-48618009039199143542009-02-18T10:43:00.000-08:002009-02-18T12:10:45.388-08:00C++ Problem: Define that macroI'm planning to post some C++ programming problems that I come across and shall post their solutions too (I'll try to post multiple solutions). Here goes the first one:<br /><br /><span style="font-weight: bold;">Problem:</span><br /><br />DO_REPORTING is MACRO which when called at the start of a function with appropriate parameter, prints the value of the parameter at the start of the function and also somehow prints the value before leaving the function. For the following code expected output would be:<br /><br />Before:2<br />After:4<br />Before:4<br />After:2<br /><br />How would you get this output without changing anything in class A and inside main function?<br /><pre><span style="color:Teal;"> 1 </span>#<span style="color:Blue;">include</span> <<span style="color:Blue;">iostream</span>><br /><span style="color:Teal;"> 2 </span><span style="color:Blue;">using</span> <span style="color:Blue;">namespace</span> <span style="color:Blue;">std</span>;<br /><span style="color:Teal;"> 3 </span><br /><span style="color:Teal;"> 4 </span><span style="color:Blue;">#define</span> DO_REPORTING(type,variable) ;<br /><span style="color:Teal;"> 5 </span><br /><span style="color:Teal;"> 6 </span><span style="color:Blue;">class</span> A{<br /><span style="color:Teal;"> 7 </span><span style="color:Blue;">protected</span>:<br /><span style="color:Teal;"> 8 </span> <span style="color:Blue;">int</span> val;<br /><span style="color:Teal;"> 9 </span><span style="color:Blue;">public</span>:<br /><span style="color:Teal;"> 10 </span> A(<span style="color:Blue;">int</span> v){<br /><span style="color:Teal;"> 11 </span> val = v;<br /><span style="color:Teal;"> 12 </span> }<br /><span style="color:Teal;"> 13 </span> <span style="color:Blue;">void</span> FuncMult(){<br /><span style="color:Teal;"> 14 </span> DO_REPORTING(<span style="color:Blue;">int</span>, val);<br /><span style="color:Teal;"> 15 </span> val *= <span style="color:Maroon;">2</span>;<br /><span style="color:Teal;"> 16 </span> }<br /><span style="color:Teal;"> 17 </span> <span style="color:Blue;">void</span> FuncDiv(){<br /><span style="color:Teal;"> 18 </span> DO_REPORTING(<span style="color:Blue;">int</span>, val);<br /><span style="color:Teal;"> 19 </span> val /= <span style="color:Maroon;">2</span>;<br /><span style="color:Teal;"> 20 </span> }<br /><span style="color:Teal;"> 21 </span> <span style="color:Blue;">int</span> GetVal(){<br /><span style="color:Teal;"> 22 </span> <span style="color:Blue;">return</span> val;<br /><span style="color:Teal;"> 23 </span> }<br /><span style="color:Teal;"> 24 </span>};<br /><span style="color:Teal;"> 25 </span><br /><span style="color:Teal;"> 26 </span><span style="color:Blue;">int</span> main(){<br /><span style="color:Teal;"> 27 </span> A a(<span style="color:Maroon;">2</span>);<br /><span style="color:Teal;"> 28 </span> a.FuncMult();<br /><span style="color:Teal;"> 29 </span> a.FuncDiv();<br /><span style="color:Teal;"> 30 </span> <span style="color:Blue;">return</span> <span style="color:Maroon;">0</span>;<br /><span style="color:Teal;"> 31 </span>}<br /><span style="color:Teal;"> 32 </span><br /><span style="color:Teal;"> 33 </span></pre><br /><span style="font-weight: bold;">Solution 1:</span><br /><pre><span style="color:Teal;"> 1 </span><span style="color:Blue;">#define</span> DO_REPORTING(type,variable) Report<type> r(val);<br /><span style="color:Teal;"> 2 </span><br /><span style="color:Teal;"> 3 </span><span style="color:Blue;">template</span> <<span style="color:Blue;">class</span> T><br /><span style="color:Teal;"> 4 </span><span style="color:Blue;">class</span> Report{<br /><span style="color:Teal;"> 5 </span> T &val;<br /><span style="color:Teal;"> 6 </span><span style="color:Blue;">public</span>:<br /><span style="color:Teal;"> 7 </span> Report(T &v):val(v){<br /><span style="color:Teal;"> 8 </span> <span style="color:Blue;">cout</span> << <span style="color:Maroon;">"Before:"</span> << val << <span style="color:Blue;">endl</span>;<br /><span style="color:Teal;"> 9 </span> }<br /><span style="color:Teal;"> 10 </span> ~Report(){<br /><span style="color:Teal;"> 11 </span> <span style="color:Blue;">cout</span> << <span style="color:Maroon;">"After:"</span> << val << <span style="color:Blue;">endl</span>;<br /><span style="color:Teal;"> 12 </span> }<br /><span style="color:Teal;"> 13 </span>};<br /><span style="color:Teal;"> 14 </span></pre>Kaisarhttp://www.blogger.com/profile/07878693490452605144noreply@blogger.com4tag:blogger.com,1999:blog-6487100581508098433.post-82941754439996403022008-08-18T02:17:00.000-07:002008-08-18T02:28:53.212-07:00Don't click it!It's been long since I got excited about a web site. I have to say, I'm very impressed with the idea, creativity, innovation and the intuitive interface of this web site/project. It's based on the idea that you don't need to click to navigate.. they strongly suggest that you don't click at all!<br /><br />Check it out here: <a href="http://www.dontclick.it/">http://www.dontclick.it/</a>. Check out the whole web site, all pages to get the real fun.<br /><br />I feel like pulling out the buttons of my mouse :P.Kaisarhttp://www.blogger.com/profile/07878693490452605144noreply@blogger.com6tag:blogger.com,1999:blog-6487100581508098433.post-56692340841164414262008-07-06T09:09:00.000-07:002008-07-07T03:24:42.799-07:00C++: What's the difference between class and struct?What's the difference between class and struct?<br /><br />A beginner would say:<br /><br />"There are a lot of differences, class is a C++ element and struct is a C one. We can have functions, virtual functions, inheritance, different access modifiers private, protected, public in a class but probably not in a struct. A struct generally holds member variables only."<br /><br />(Just to clarify, the above statement is completely wrong.)<br /><br />Intermediate and most advanced C++ programmers would say:<br /><br />"class and struct do not have any difference, except the fact that class members are by default private and struct members are by default public. The keywords class and struct are interchangeable."<br /><br />The above statement is correct to some extent and would satisfy most questioners, however, there are some special cases where class and struct are not interchangeable.<br /><br />Consider the following code which compiles well in a C++ complier:<br /><br /><span style="color: rgb(51, 0, 153);">template</span> <<span style="color: rgb(51, 0, 153);">class</span> T><br /><span style="color: rgb(51, 0, 153);">void</span> fn()<br />{<br />}<br /><br />Now replace the keyword 'class' with 'struct' and compile it..<br /><br /><span style="color: rgb(51, 0, 153);">template</span> <<span style="color: rgb(51, 0, 153);">struct</span> T><br /><span style="color: rgb(51, 0, 153);">void</span> fn()<br />{<br />}<br /><br />It gives a compiler error! According to MSDN's definition of the template keyword:<br /><br />"The <i>template-parameter-list</i> is a comma-separated list of template parameters, which may be types (in the form <b>class</b> <i>identifier</i>, <b>typename</b> <i>identifier</i>, or <b>template < </b><i>template-parameter-list</i><b> > class </b><i>identifier</i>) or non-type parameters to be used in the template body."<br /><br />So the template parameter list doesn't take the keyword 'struct'!<br /><br />The template mechanism itself was introduced in later phase of the C++ evolution, some years later than the first version of C++. The 'struct' keyword was probably ignored when defining template which was considered to be an advance feature of C++.<br /><br />"Another reasonable use of the C struct in C++, then, is when you want to pass all or part of a complex class object to a C function. This struct declaration serves to encapsulate that data and guarantees a compatible C storage layout. This guarantee, however, is maintained only under composition." said Stanley B. Lippman, author of several C++ books.Kaisarhttp://www.blogger.com/profile/07878693490452605144noreply@blogger.com7tag:blogger.com,1999:blog-6487100581508098433.post-47723508150731676902008-07-06T02:56:00.000-07:002008-07-14T02:53:34.389-07:00C++: Accessing the virtual table directlyThis post is not intended for beginners. To understand the content of this topic, you need to have basic understanding of what virtual functions are.<br /><br />We know that the run time binding or virtual function mechanism is implemented by a virtual table. If a class has at least one virtual function a virtual table will be created for that class. To be specific, 'only one' virtual table will be created for all of the instances/objects of that class. Each of the instances and objects will have a pointer to the virtual table.<br /><br />The same thing is true for a class hierarchy. Meaning, if class Z derives class Y and class Y derives class X, only one virtual table will be created for all instances/objects of class X, Y and Z. Each of the instances and objects of X, Y and Z will have a pointer to the virtual table.<br /><br />===============<br />Added on July 14, 2008:<br />The virtual tables for each of class X, Y and Z share common information but they are not necessarily the same table for each of these classes. The scenario is complex for multiple and virtual inheritance. I would like to discuss them in future posts.<br />===============<br /><br />A pointer is 32 bit/4 bytes in a 32-bit architecture and 64-bit/8 bytes in a 64-bit architecture. So all instances/objects of a class or class hierarchy, where we have a virtual table, will have additional 4 bytes in them and 8 bytes in case of a 64-bit architecture.<br /><br />This pointer is called virtual table pointer, sometimes 'vptr'. In VC++ compiler, the objects will have a pointer named '__vfptr' in them and in some other compiler it's '__vptr_X', where X is the class name.<br /><br />Now __vfptr is not directly accessible from your code. For example, if you write the following code you'll get a compiler error as the __vfptr is not available for your use.<br /><br /><pre class="codeHTML"><span class="lineNumber"> 1</span> <span class="othertext">X</span><span class="whitespace"> </span><span class="othertext">a</span><span class="punctuation">;</span><span class="whitespace"><br /><span class="lineNumber"> 2</span> </span><span class="predefined">cout</span><span class="whitespace"> </span><span class="unknownchar"><</span><span class="unknownchar"><</span><span class="whitespace"> </span><span class="othertext">a</span><span class="punctuation">.</span><span class="punctuation">_</span><span class="punctuation">_</span><span class="othertext">vfptr</span><span class="punctuation">;</span><span class="whitespace"><br /></span></pre><br />However, if you debug the code in VC++, you can see the 'a.__vfptr' in the variable watch windows. Interesting ha?<br /><br />Okay, now we'd like to see how we can access the virtual table even if the compiler doesn't want us to. Let's have class X with a virtual function fn() which simply prints a member variable and we want to access the virtual table of class X to call the function fn() using it. The following code does that.<br /><br /><pre class="codeHTML"><span class="lineNumber"> 1</span> <span class="punctuation">#</span><span class="predefined">include</span><span class="whitespace"> </span><span class="unknownchar"><</span><span class="predefined">iostream</span><span class="unknownchar">></span><span class="whitespace"><br /><span class="lineNumber"> 2</span><br /><span class="lineNumber"> 3</span> </span><span class="keyword">using</span><span class="whitespace"> </span><span class="keyword">namespace</span><span class="whitespace"> </span><span class="predefined">std</span><span class="punctuation">;</span><span class="whitespace"><br /><span class="lineNumber"> 4</span><br /><span class="lineNumber"> 5</span> </span><span class="lineComment">//a simple class</span><span class="whitespace"><br /><span class="lineNumber"> 6</span> </span><span class="keyword">class</span><span class="whitespace"> </span><span class="othertext">X</span><span class="whitespace"><br /><span class="lineNumber"> 7</span> </span><span class="punctuation">{</span><span class="whitespace"><br /><span class="lineNumber"> 8</span> </span><span class="keyword">public</span><span class="punctuation">:</span><span class="whitespace"><br /><span class="lineNumber"> 9</span> </span><span class="lineComment">//fn is a simple virtual function</span><span class="whitespace"><br /><span class="lineNumber"> 10</span> </span><span class="keyword">virtual</span><span class="whitespace"> </span><span class="builtinType">void</span><span class="whitespace"> </span><span class="othertext">fn</span><span class="punctuation">(</span><span class="punctuation">)</span><span class="whitespace"><br /><span class="lineNumber"> 11</span> </span><span class="punctuation">{</span><span class="whitespace"><br /><span class="lineNumber"> 12</span> </span><span class="predefined">cout</span><span class="whitespace"> </span><span class="unknownchar"><</span><span class="unknownchar"><</span><span class="whitespace"> </span><span class="stringliteral">"n = "</span><span class="whitespace"> </span><span class="unknownchar"><</span><span class="unknownchar"><</span><span class="whitespace"> </span><span class="othertext">n</span><span class="whitespace"> </span><span class="unknownchar"><</span><span class="unknownchar"><</span><span class="whitespace"> </span><span class="predefined">endl</span><span class="punctuation">;</span><span class="whitespace"><br /><span class="lineNumber"> 13</span> </span><span class="punctuation">}</span><span class="whitespace"><br /><span class="lineNumber"> 14</span><br /><span class="lineNumber"> 15</span> </span><span class="lineComment">//a member variable</span><span class="whitespace"><br /><span class="lineNumber"> 16</span> </span><span class="builtinType">int</span><span class="whitespace"> </span><span class="othertext">n</span><span class="punctuation">;</span><span class="whitespace"><br /><span class="lineNumber"> 17</span> </span><span class="punctuation">}</span><span class="punctuation">;</span><span class="whitespace"><br /><span class="lineNumber"> 18</span><br /><span class="lineNumber"> 19</span> </span><span class="builtinType">int</span><span class="whitespace"> </span><span class="othertext">main</span><span class="punctuation">(</span><span class="punctuation">)</span><span class="whitespace"><br /><span class="lineNumber"> 20</span> </span><span class="punctuation">{</span><span class="whitespace"><br /><span class="lineNumber"> 21</span> </span><span class="lineComment">//create an object (obj) of class X</span><span class="whitespace"><br /><span class="lineNumber"> 22</span> </span><span class="othertext">X</span><span class="whitespace"> </span><span class="punctuation">*</span><span class="othertext">obj</span><span class="whitespace"> </span><span class="unknownchar">=</span><span class="whitespace"> </span><span class="keyword">new</span><span class="whitespace"> </span><span class="othertext">X</span><span class="punctuation">(</span><span class="punctuation">)</span><span class="punctuation">;</span><span class="whitespace"><br /><span class="lineNumber"> 23</span> </span><span class="othertext">obj</span><span class="punctuation">-</span><span class="unknownchar">></span><span class="othertext">n</span><span class="whitespace"> </span><span class="unknownchar">=</span><span class="whitespace"> </span><span class="integerLiteral">10</span><span class="punctuation">;</span><span class="whitespace"><br /><span class="lineNumber"> 24</span><br /><span class="lineNumber"> 25</span> </span><span class="lineComment">//get the virtual table pointer of object obj</span><span class="whitespace"><br /><span class="lineNumber"> 26</span> </span><span class="builtinType">int</span><span class="punctuation">*</span><span class="whitespace"> </span><span class="othertext">vptr</span><span class="whitespace"> </span><span class="unknownchar">=</span><span class="whitespace"> </span><span class="punctuation">*</span><span class="punctuation">(</span><span class="builtinType">int</span><span class="punctuation">*</span><span class="punctuation">*</span><span class="punctuation">)</span><span class="othertext">obj</span><span class="punctuation">;</span><span class="whitespace"><br /><span class="lineNumber"> 27</span><br /><span class="lineNumber"> 28</span> </span><span class="lineComment">// we shall call the function fn, but first the following assembly code</span><span class="whitespace"><br /><span class="lineNumber"> 29</span> </span><span class="lineComment">// is required to make obj as 'this' pointer as we shall call</span><span class="whitespace"><br /><span class="lineNumber"> 30</span> </span><span class="lineComment">// function fn() directly from the virtual table</span><span class="whitespace"><br /><span class="lineNumber"> 31</span> </span><span class="punctuation">_</span><span class="punctuation">_</span><span class="othertext">asm</span><span class="whitespace"><br /><span class="lineNumber"> 32</span> </span><span class="punctuation">{</span><span class="whitespace"><br /><span class="lineNumber"> 33</span> </span><span class="othertext">mov</span><span class="whitespace"> </span><span class="othertext">ecx</span><span class="punctuation">,</span><span class="whitespace"> </span><span class="othertext">obj</span><span class="whitespace"><br /><span class="lineNumber"> 34</span> </span><span class="punctuation">}</span><span class="whitespace"><br /><span class="lineNumber"> 35</span><br /><span class="lineNumber"> 36</span> </span><span class="lineComment">//function fn is the first entry of the virtual table, so it's vptr[0]</span><span class="whitespace"><br /><span class="lineNumber"> 37</span> </span><span class="punctuation">(</span><span class="whitespace"> </span><span class="punctuation">(</span><span class="builtinType">void</span><span class="whitespace"> </span><span class="punctuation">(</span><span class="punctuation">*</span><span class="punctuation">)</span><span class="punctuation">(</span><span class="punctuation">)</span><span class="punctuation">)</span><span class="whitespace"> </span><span class="othertext">vptr</span><span class="punctuation">[</span><span class="integerLiteral">0</span><span class="punctuation">]</span><span class="whitespace"> </span><span class="punctuation">)</span><span class="punctuation">(</span><span class="punctuation">)</span><span class="punctuation">;</span><span class="whitespace"><br /><span class="lineNumber"> 38</span><br /><span class="lineNumber"> 39</span> </span><span class="lineComment">//the above is the same as the following</span><span class="whitespace"><br /><span class="lineNumber"> 40</span> </span><span class="lineComment">//obj->fn();</span><span class="whitespace"><br /><span class="lineNumber"> 41</span><br /><span class="lineNumber"> 42</span> </span><span class="keyword">return</span><span class="whitespace"> </span><span class="integerLiteral">0</span><span class="punctuation">;</span><span class="whitespace"><br /><span class="lineNumber"> 43</span> </span><span class="punctuation">}</span><span class="whitespace"><br /><span class="lineNumber"> 44</span> </span></pre>Please note, this code is compiler dependent and may only work on VC++ compilers and it'll work correctly when you'll run it in 'Release' mode. Here goes some explanation of the code.<br /><br />In line 26, we have:<br /><pre class="codeHTML"><span class="whitespace"><span class="lineNumber"> 26</span> </span><span class="builtinType">int</span><span class="punctuation">*</span><span class="whitespace"> </span><span class="othertext">vptr</span><span class="whitespace"> </span><span class="unknownchar">=</span><span class="whitespace"> </span><span class="punctuation">*</span><span class="punctuation">(</span><span class="builtinType">int</span><span class="punctuation">*</span><span class="punctuation">*</span><span class="punctuation">)</span><span class="othertext">obj</span><span class="punctuation">;<br /></span></pre>The virtual table pointer __vfptr is available in the first 4 bytes of the object. In this line, we get the value of the pointer __vfptr or the address of the virtual table as an integer pointer (say as a pointer to an integer array).<br /><br />The first entry of the virtual table is the function pointer of the virtual function 'fn'. We can access the first entry using vptr[0] (as this is just an array). So, in line 37, we just call the function using the function pointer. But wait, you might be asking why the following assembly line is there before that function call.<br /><pre class="codeHTML"><span class="whitespace"><span class="lineNumber"> 33</span> </span><span class="othertext">mov</span><span class="whitespace"> </span><span class="othertext">ecx</span><span class="punctuation">,</span><span class="whitespace"> </span><span class="othertext">obj</span><span class="whitespace"><br /></span></pre>If you take another look into the implementation of function fn(), you can see that it prints out the member variable 'n', which is only avaliable to object 'obj'. Inside the function fn(), 'obj' needs to be set as 'this' pointer, to give the function fn() access to all it's members.<br /><br />When we call the function fn() in this way: obj->fn(), the compiler does the job for us and sets 'obj' as 'this' before calling the function. But in line 37, we couldn't specify anything to the function fn() saying it is called for the object 'obj', so the function won't find out where to get the value of 'n' from. This is why we expicitly need to set the 'obj' as 'this' before we call the function fn() in line 37. We did that in line 33, in the assembly code. This line is again VC++ specific. In VC++, 'this' pointer is set in the register 'ECX'. Some other compiler may handle that differently.<br /><br />If we had more virtual function, we could have access them using next indexes of vptr: vptr[1], vptr[2], etc.<br /><br />We have learned some interesting facts about the virtual functions and the virtual table. We may not have any use of this kind of code where we need to directly access the virtual table in our general applications but this helps when you want to know more about C++ internals.<br /><br />Enjoy!<br /><br />July 12, 2008:<br />We assumed here that the vptr is placed in the beginning of the class object. here's a note on that:<br /><br />Traditionally, the vptr has been placed after all the explicitly declared members of the class. More recently, it has been placed at the beginning of the class object. The C++ Standard allows the compiler the freedom to insert these internally generated members anywhere, even between those explicitly declared by the programmer.Kaisarhttp://www.blogger.com/profile/07878693490452605144noreply@blogger.com33tag:blogger.com,1999:blog-6487100581508098433.post-7250682991162676712008-06-28T03:00:00.000-07:002008-07-06T05:17:09.090-07:00C++: Avoid using assignments in constructorsIn my post <a href="http://kaisar-haque.blogspot.com/2008/03/coding-better-c.html">Coding better C++</a>, I've suggested a list of techniques to improve C++ coding. I shall try to explain them one at a time in the future posts. In this post, I've talked about "4. Avoid using assignments in constructors, use initializer list to initialize members".<br /><br />The initialize list is used to avoid double construction of a contained object. Take the following example:<br /><span style="color: rgb(51, 0, 153);"></span><pre class="codeHTML"><span class="lineNumber"> 1</span> <span class="whitespace"><br /><span class="lineNumber"> 2</span> </span><span class="othertext">Class</span><span class="whitespace"> </span><span class="othertext">Student</span><span class="whitespace"><br /><span class="lineNumber"> 3</span> </span><span class="punctuation">{</span><span class="whitespace"><br /><span class="lineNumber"> 4</span> </span><span class="keyword">public</span><span class="punctuation">:</span><span class="whitespace"><br /><span class="lineNumber"> 5</span> </span><span class="othertext">Student</span><span class="whitespace"> </span><span class="punctuation">(</span><span class="whitespace"> </span><span class="keyword">const</span><span class="whitespace"> </span><span class="builtinType">char</span><span class="whitespace"> </span><span class="punctuation">*</span><span class="punctuation">_</span><span class="othertext">name</span><span class="punctuation">)</span><span class="whitespace"><br /><span class="lineNumber"> 6</span> </span><span class="punctuation">{</span><span class="whitespace"><br /><span class="lineNumber"> 7</span> </span><span class="othertext">name</span><span class="whitespace"> </span><span class="unknownchar">=</span><span class="whitespace"> </span><span class="punctuation">_</span><span class="othertext">name</span><span class="punctuation">;</span><span class="whitespace"><br /><span class="lineNumber"> 8</span> </span><span class="punctuation">}</span><span class="whitespace"><br /><span class="lineNumber"> 9</span> </span><span class="keyword">private</span><span class="punctuation">:</span><span class="whitespace"><br /><span class="lineNumber"> 10</span> </span><span class="predefined">string</span><span class="whitespace"> </span><span class="othertext">name</span><span class="punctuation">;</span><span class="whitespace"><br /><span class="lineNumber"> 11</span> </span><span class="punctuation">}</span><span class="punctuation">;</span><span class="whitespace"><br /></span></pre>We create an object of the Student, for example, in the following way.<br /><pre class="codeHTML"><span class="whitespace"><span class="lineNumber"> </span></span><span class="othertext">Student</span><span class="whitespace"> </span><span class="othertext">s</span><span class="whitespace"> </span><span class="punctuation">(</span><span class="punctuation">“</span><span class="othertext">Abc</span><span class="punctuation">”</span><span class="punctuation">)</span><span class="punctuation">;</span><span class="whitespace"><br /></span></pre> The following executions take place:<br /><p>1. string name is initialized<br /> a. string::string() function is called<br />2. body of the Student::Student(const char *) constructor is called<br />3. the line name = _name is executed<br /> a. string::operator=(const char *) function is called</p> A note to experts: we’ve ignored memory allocation steps and detailed assembly steps of the function calls to keep things simple.<br /><br />As you can see the result of the step 1.a, string::string(), is discarded by step 3.a, string::operator=(const char *), the step 1.a is therefore redundant.<br /><br />We can optimize this with the initializer list, in the following way:<br /><span style="color: rgb(51, 0, 153);"> </span><pre class="codeHTML"><span class="lineNumber"> 1</span> <span class="othertext">Class</span><span class="whitespace"> </span><span class="othertext">Student</span><span class="whitespace"><br /><span class="lineNumber"> 2</span> </span><span class="punctuation">{</span><span class="whitespace"><br /><span class="lineNumber"> 3</span> </span><span class="keyword">public</span><span class="punctuation">:</span><span class="whitespace"><br /><span class="lineNumber"> 4</span> </span><span class="othertext">Student</span><span class="whitespace"> </span><span class="punctuation">(</span><span class="whitespace"> </span><span class="keyword">const</span><span class="whitespace"> </span><span class="builtinType">char</span><span class="whitespace"> </span><span class="punctuation">*</span><span class="punctuation">_</span><span class="othertext">name</span><span class="punctuation">)</span><span class="whitespace"> </span><span class="punctuation">:</span><span class="whitespace"> </span><span class="othertext">name</span><span class="whitespace"> </span><span class="punctuation">(</span><span class="punctuation">_</span><span class="othertext">name</span><span class="punctuation">)</span><span class="whitespace"><br /><span class="lineNumber"> 5</span> </span><span class="punctuation">{</span><span class="whitespace"><br /><span class="lineNumber"> 6</span> </span><span class="punctuation">}</span><span class="whitespace"><br /><span class="lineNumber"> 7</span> </span><span class="keyword">private</span><span class="punctuation">:</span><span class="whitespace"><br /><span class="lineNumber"> 8</span> </span><span class="predefined">string</span><span class="whitespace"> </span><span class="othertext">name</span><span class="punctuation">;</span><span class="whitespace"><br /><span class="lineNumber"> 9</span> </span><span class="punctuation">}</span><span class="punctuation">;</span><span class="whitespace"><br /><span class="lineNumber"> 10</span> </span></pre>In this way, the string 'name' is initialized only once and with the value of _name and it calls the string::string(const char *) function directly.<br /><br />Many optimizations require some kind of a trade-off. You often trade speed for clarity, simplicity, re-usability, or some other metric. But in this example, optimization requires no sacrifice at all. This constructor will generate the exact same Student object with the exception of improved performance.Kaisarhttp://www.blogger.com/profile/07878693490452605144noreply@blogger.com3tag:blogger.com,1999:blog-6487100581508098433.post-25488256415377474462008-06-21T22:00:00.000-07:002008-06-27T13:09:17.665-07:00Project management thoughts - 3. Parallel development issuesThis is a continuation of the "Project management thoughts" series. Please read the previous posts if you haven't already.<br /><br />In this post, I shall talk about why the parallel development of prototype and real application is not a good idea in a case like ours.<br /><br /><span style="font-weight: bold;">Why prototype and real development should not be in parallel</span><br /><br />The purpose of the prototype is to try out different brainstorming results of the client and solidifying requirements from it. Now, the scope of the new requirements and 'change requests' to already solidified requirements have no limits, officially.<br /><br />Let's take a break and do some basic maths of project management. A project has three main key factors: Time (T), Material (M) and Resources (R). Time is proportional to material and inversely proportional to resources. This can be represented in the following way.<br /><br /><div style="text-align: center;">T = M / R<br />or T x R = M<br /></div><br />Meaning: 1. If your time is fixed and if you increase your material, you also need to increase your resources, 2. If your resources are fixed and if you increase your material, you also need to increase your time, 3. If you material is fixed and if you change your resources, your time will get changed, and so on.<br /><br />I'm sure most of us know this in one form or another and I only described in details for the newbies.<br /><br />Now back to the prototype, if the prototype continuously gives us new requirements through out the whole project time line, we need to have time open ended, according to the simple math that we done above, but that not an option in the first place. This is fixed time project.<br /><br />Besides new requirements, the prototype will also yield a list of small and large changes (large, in terms of task volume). These changes need to get reflected in the already built features of the real application. The changes, of course, are materials (M) which requires more time (T).<br /><br />The changes imposed from the prototype will virtually have no limits. If we have already implemented, for example, 20 features in the real application that were previously solidified, those may require complete or partial reworks in a very short time. Adjusting major changes in the requirements makes a solid codebase messy, making it more vulnerable to major bugs. Re-structuring, re-designing (the software design) and re-implementing are probably the last things a development team wants to do. If requirements get changed every now and then, which we cannot guarantee, may end up with unexpectedly weak software design and codebase.<br /><br />There are also other factors of why it doesn't seem to be a good idea to run prototype and real application development in parallel. One of them is the time it takes to solidify requirements from the prototype. If we assume that we shall get requirements solidified from the prototype at a constant rate (or at a good rate) then it would be very wrong in real life. In practical scenarios we may get some or many of requirements solidified from the prototype and we may also need to wait a significant amount of time to get something solidified as it is very much dependent on client's decisions. 'Waiting' on a dependency for uncertain period of time for a 'fixed time and resources' project like ours (or in any project), is just one of the failure factors.<br /><br /><span style="font-weight: bold;">The better solution</span><br /><br />Our proposed solution was to complete the prototype application first, with the effort of the full development team, solidifying requirements from it, and then working on the real application, again with the effort of the full development team.<br /><br />This solves a lot of problems, the ones that I mentioned above. The only concern with this way is, can we really complete the prototype with all ideas of the client in time and then can we finalize requirements from the prototype in time? Well, it requires effort from both of the side, the team and the client. Both parties need to work aggressively towards one goal, which is to identify requirements within a given time frame.<br /><br /><span style="font-weight: bold;">To be continued.</span><br /><br />Coming up soon: The team formation, the technologies, the tools used, the prototype phase, the software design phase..<br /><br />[I shall continue to talk about my project management experiences and thoughts about this project in regular posts. I hope you shall find them interesting or useful].Kaisarhttp://www.blogger.com/profile/07878693490452605144noreply@blogger.com1tag:blogger.com,1999:blog-6487100581508098433.post-16534496186340165412008-06-20T13:06:00.000-07:002008-06-20T15:05:22.812-07:00Project management thoughts - 2. The initial team and tasksThis is a continuation of the "Project management thoughts" series. Please read the previous posts if you haven't already.<br /><br />In this post, I shall mostly talk about the initial tasks of the project TP and my thoughts about it.<br /><br /><span style="font-weight: bold;">The initial team</span><br /><br />Even though the project now have 20 engineers, it began only with 2 engineers. The primary goals of the initial team were to understand high level requirements of the project and to estimate required resources for the project. The initial team members were:<br /><ol><li>A senior engineer with around 2 years of experiences, skilled in C#, Java and PHP</li><li>Myself</li></ol>The initial team worked around for 3 weeks to capture high level requirements of the project, business goals of the client, preferred development process of the client, preferred technologies of the client and the time line of the project. The detailed requirements specification was not defined and one of the first goals of the project is to identify and solidify the requirements.<br /><br /><span style="font-weight: bold;">The initial plan</span><br /><br />The client wants to identify the detailed requirements with a prototype project. The prototype project will follow agile development method and needs to have a weekly build on which the client would like to try out a few different ideas and solidify detailed requirements as the builds go on. Basically the prototype project will be a brain storming ground for the client and requirements capturing ground for us (ReliSource).<br /><br />The client wants a separate development project to run in parallel to the prototype project and the solidified requirement sets should be sent to the development project team for real implementation.<br /><br />The idea is to test different things on the prototype, solidifying them and to build them in the real application. Both of the prototype development and real application development needs to go in parallel and the project, actually the first release, needs to be completed within six months.<br /><br />Sounds doable? Well, not exactly. I shall post about 1. why not, 2. how we proposed a different strategy which was doable and also made the client happy, in my next post.Kaisarhttp://www.blogger.com/profile/07878693490452605144noreply@blogger.com2tag:blogger.com,1999:blog-6487100581508098433.post-62901971836164784912008-06-13T13:15:00.000-07:002008-06-13T14:18:01.628-07:00Project management thoughts - 1. IntroductionI'm currently working with a team of size 20. I'm playing a dual role of project manager and technical lead at the same time. I'll try to share my experiences with this relatively large team and my thoughts on project management. The age of the project is two and half months and is currently scheduled for four more months.<br /><br />I'm planning to blog about this live project as it goes on which I believe is a courageous step. I shall try to maintain the privacy and not to disclose sensitive project information here.<br /><br /><span style="font-weight: bold;">The team</span><br />The team has 20 engineers, including me, with the following distribution.<br /><ul><li>14 developers</li><ul><li>9 developers have 1 year of experiences on average</li><li>5 developers have 2 years of experiences on average<br /></li></ul><li>4 SQAs</li><ul><li>1 release engineer/SQA lead, with 3+ years of experiences<br /></li><li>3 engineers with 1 year of experiences of average<br /></li></ul><li>1 graphics designer</li><ul><li>2+ years of experiences<br /></li></ul><li>1 project manager</li><ul><li>that's me with 5+ years of experiences<br /></li></ul></ul>All engineers are computer science graduates, mostly from BUET (Bangladesh University of Engineering and Technologies), NSU (North South University) and EWU (East West University). The designer is a graduate of Dhaka University.<br /><br />All development engineers are good problem solvers, strong in C++ or C# or Java, strong in OOP and are dedicated. One of them even went to ACM world finals.<br /><br />So, overall, it's a brilliant team.<br /><br /><span style="font-weight: bold;">The project</span><br />The project is a medium scale web application. I cannot disclose the description of the project but the type of the project is to build and manage networks between two groups of people.<br /><br />The project is fresh development from the scratch where we do not need to work on existing codebase. Let's call the project 'The Project' or TP in short ;).<br /><br />TP has two sub projects, let's call them:<br /><ul><li>TPX - which is for the first user group</li><li>TPY - which is for the 2nd user group</li></ul>TP also has another sub project for a group of people who need to administrate the whole application. Let's call it:<br /><ul><li>TPZ - which is for administrator users</li></ul>Three of the sub-projects TPX, TPY and TPZ will serve different target user groups as mentioned above.<br /><br />The project is targeted towards massive user groups, say several thousand from the first user group and hundreds of thousands from second user group will use the application within a year of lunching the application. The project will have rigorous database transactions for most of the features and functionalities.<br /><br />Professional and rich user experience is very important in this project. The project will have good looking UI components, for which we have a dedicated graphics designer.<br /><br /><span style="font-weight: bold;">To be continued.</span><br />[I shall continue to talk about my project management experiences and thoughts about this project in regular posts. I hope you shall find them interesting or useful].Kaisarhttp://www.blogger.com/profile/07878693490452605144noreply@blogger.com6tag:blogger.com,1999:blog-6487100581508098433.post-89129544446885669322008-06-13T13:03:00.001-07:002008-06-13T14:21:18.150-07:00Empowerment of SQAThis has probably become a hot topic recently. The 'scope of the empowerment' really needs to be defined. What 'powers' does empowerment include and what not? Whatever we say it 'includes', does it apply to all projects? even of different types?<br /><br />SQA team, if empowered, needs definition of what they can do and what not. There can be some serious do's. For example, can they stop a software release which has many bugs? If so, can your team, your company or the client effort it? What is the real intention of this action, better quality even if it hampers project timeline and the business of the client?<br /><br />There can be some less serious do's of SQA empowerment, for example: SQA team suggests to improve usability (a simple example of this: a numeric textbox takes all characters and validates and shows error later on if the input is not a number. SQA team suggests that the textbox should allow only number keystrokes in the first place). Now this sort of details may not be mentioned in the spec and so, in general, the developers argue with the SQA (may be because they feel lazy). According to empowerment do's, should the SQA be able to insist the developers to follow the suggestion?<br /><br />If you want to empower SQA and try to draw some lines on their do's and don'ts then you're most likely to rediscover a simple fact.<br />In most of the companies, SQA teams are so much dominated by the development team that they (SQA) cannot raise their voices even if they see something is terribly wrong. It's the problem with a developer's perspective of the SQA team. In general, a developer feels much superior to a similar ranking SQA and feels he (developer) know much more and his work is the most important thing in the project's success.<br /><br />So, now the empowerment is needed to make the developers feel that SQAs have some power too (!!) and a developer cannot just overlook their suggestions(?). Is this the main reason behind empowerment of SQA? if so, then let's look at the origin of the issue again. Is it again a developer's mindset and his careless or superior attitude towards a SQA? Can this be fixed? And is the officially declared 'SQA empowerment' the only way?<br /><br />In Bangladesh, most of the SQA engineers cannot do programming or understand mature coding and the 'white box' is just too much for them. Some seriously lack knowledge of latest technologies, techniques and advance skills (for example, only few SQAs have ever built and installed a software on Linux command prompt, unless it's a project-specific task). So, SQAs, in general, fall behind technical capabilities of developers. This should be addressed and SQAs need much improvement in the technical areas to make a good impression with rest of the team.<br /><br />Developers should learn the importance of others' comments. One developer sometimes doesn't value the suggestions of other good developers, let alone the SQAs. Horribly some seniors do that as well! This issue needs to be handled by the Project Manager or above. Developers should receive a 'clear message' saying: not to overlook other's suggestions and comments and to strongly collaborate with the SQA team.<br /><br />If you can handle the root of the problem, 'empowerment' or other 'forceful' ideas might not be needed. Peaceful solutions are good for the project health and the forceful ones might not be so, rather they can introduce chaos.Kaisarhttp://www.blogger.com/profile/07878693490452605144noreply@blogger.com0tag:blogger.com,1999:blog-6487100581508098433.post-15600480611285265082008-05-30T00:36:00.000-07:002008-05-30T01:09:43.309-07:00Black boxes are not blackAs the name itself suggests, 'Black boxes' in commercial airplanes are black by most people's assumptions, as the case was with me. But I recently learned that black boxes are not black, rather they are bright orange or red in most commercial airplanes.<br /><br />Interestingly, in Boeing 747, the black boxes (there are two) are bright red and all other aviation electronic boxes are black :). The unique bright color helps locating the 'black box' quickly in case of an accident.<br /><br />You can verify at: <a href="http://www.howstuffworks.com/black-box.htm">http://www.howstuffworks.com/black-box.htm</a>Kaisarhttp://www.blogger.com/profile/07878693490452605144noreply@blogger.com0tag:blogger.com,1999:blog-6487100581508098433.post-10929458813424708112008-03-16T09:19:00.000-07:002008-03-16T11:15:04.510-07:00Coding better C++I've prepared a short guideline for writing better codes in C++. I've prepared the list from my C++ experiences and studies. Each item of the list needs good amount of explanation which is outside of the scope of this post. I'll try to explain them in the future posts in my blog. For now, if you need more information on an suggested item, you can look up on the internet or in the books.<br /><br />Here it goes:<br /><ol><li>C++ is not C. Stop mixing C and C++ unless it is required. Start thinking in C++.</li><li>Have a C++ coding standard defined or follow the coding standard defined for your project or organization.<br /></li><li>Design from outside, design the interfaces first.<br /></li><li>Avoid using assignments in constructors, use initializer list to initialize members.</li><li>Know the need of copy constructor and define when needed.<br /></li><li>Use virtual destructor when your class is used as a base class.</li><li>Never call a destructor (unless you're using a placement new). If you do, you may need to revise your software design.</li><li>Avoid using placement new.</li><li>Avoid using Macros for functions. Use Inline functions instead.</li><li>Avoid using Array, unless you have to. Use standard library containers like Vector, etc.</li><li>Avoid C style cast, use the C++ casts.</li><li>Use references when you can and pointers when you have to.</li><li>Use composition when you can and private inheritance when you have to.</li><li>Use const for function parameters when you can.<br /></li><li>Be careful about static initializations, you might get unexpected behavior if the static variables/objects has dependencies.<br /></li><li>Be careful about multiple inheritance. Understand the 'dreaded diamond'.</li><li>Be careful about floats and doubles. They may give you unexpected behaviors.</li><li>Avoid constant literals in the code. Separate them in static inline functions or macros.</li></ol>Each suggestion, mentioned above, will take time to get used to (if you're not already). For example, item 1 says "C++ is not C. Stop mixing C and C++ unless it is required. Start thinking in C++.". Now if you were a C programmer, it is not easy to stop using C, for example: it may become difficult for you to stop using stdio functions and using iostream instead. It'll take time and practice. (Please note, stdio functions might have advantages over iostream but that's not the point here).<br /><br />Once you get used to all of the suggested points, you'll find that your code is much much better than many others and of course, more optimized and less error prone. Let me emphasize, 'these are golden suggestions, try to follow them'.<br /><br />Happy coding :).Kaisarhttp://www.blogger.com/profile/07878693490452605144noreply@blogger.com4tag:blogger.com,1999:blog-6487100581508098433.post-74573448120150325822008-03-16T08:27:00.000-07:002011-06-19T03:58:19.135-07:00How to begin web programming with PHP<div dir="ltr" style="text-align: left;" trbidi="on">I am often asked, 'how to begin web programming with PHP?'. Following is a response that I had given few weeks ago. Hope this will be useful for anyone willing to start learning PHP.<br />
<br />
<b>Things you need to know to begin web programming with PHP:<br />
</b> <br />
<ul><li>Good knowledge of HTML</li>
<li>Some knowledge of CSS</li>
<li>Some knowledge of JavaScript</li>
<li>Good knowledge of MySQL Database, SQL (as most php applications are database driven)</li>
<li>Good knowledge of PHP</li>
<li>Using and configuring Apache</li>
</ul><b>Tutorial Links:<br />
</b><br />
Here are the links to tutorials that may help you to accelerate your learning process.<b><br />
</b> <br />
<ul><li><b>HTML:</b></li>
<ul><li>Very good short tutorial: <a href="http://www.w3schools.com/html/html_intro.asp" target="_blank">http://www.w3schools.com/html<wbr></wbr>/html_intro.asp</a></li>
<li>Good tutorial: <a href="http://www.w3.org/MarkUp/Guide/" target="_blank">http://www.w3.org/MarkUp/Guide/</a></li>
<li>Advanced tutorial: <a href="http://www.w3.org/MarkUp/Guide/Advanced.html" target="_blank">http://www.w3.org/MarkUp/Guide<wbr></wbr>/Advanced.html</a></li>
</ul><li><b>CSS:</b></li>
<ul><li>Very good short tutorial: <a href="http://www.w3schools.com/css/css_intro.asp" target="_blank">http://www.w3schools.com/css<wbr></wbr>/css_intro.asp</a></li>
<li>A good tutorial: <a href="http://www.w3.org/Style/Examples/011/firstcss" target="_blank">http://www.w3.org/Style<wbr></wbr>/Examples/011/firstcss</a></li>
<li>A detailed tutorial: <a href="http://www.html.net/tutorials/css/" target="_blank">http://www.html.net/tutorials<wbr></wbr>/css/</a></li>
</ul><li><b>JavaScript:</b></li>
<ul><li>Very good short tutorial on JavaScript: <a href="http://www.w3schools.com/js/js_intro.asp" target="_blank">http://www.w3schools.com/js/js<wbr></wbr>_intro.asp</a></li>
<li>Detailed tutorial: <a href="http://www.webdeveloper.com/javascript/javascript_js_tutorial.html" target="_blank">http://www.webdeveloper.com<wbr></wbr>/javascript/javascript_js<wbr></wbr>_tutorial.html</a></li>
</ul><li><b>PHP:</b></li>
<ul><li>Very good short tutorial on PHP: <a href="http://www.w3schools.com/php/php_intro.asp" target="_blank">http://www.w3schools.com/php<wbr></wbr>/php_intro.asp</a></li>
<li>Very good detailed tutorial on PHP: <a href="http://devzone.zend.com/node/view/id/627" target="_blank">http://devzone.zend.com/node<wbr></wbr>/view/id/627</a> (go through all the parts)</li>
</ul></ul><b>Recommended Setup:<br />
</b> <br />
<ul><li>Wamp Server (<a href="http://www.wampserver.com/en/download.php">http://www.wampserver.com/en/download.php</a>) which includes Apache web server, PHP and MySQL.</li>
<li>Editor: Notepad++ (<a href="http://notepad-plus-plus.org/download">http://notepad-plus-plus.org/download</a>).</li>
<li>HTML Editor: DreamWeaver, FrontPage. </li>
<li> References (must have): php manual.chm, mysql manual.chm, javascript referenece books.</li>
</ul><b>Recommendations:<br />
</b> <br />
<ul><li>Keep the references handy/near to you.</li>
<li>Search in Google for more tutorials</li>
<li>Look into well known PHP frameworks (search for: top php frameworks).<br />
</li>
<li>Look into open source projects to learn how real life applications are done.</li>
<li>Practice well :)</li>
</ul></div>Kaisarhttp://www.blogger.com/profile/07878693490452605144noreply@blogger.com6tag:blogger.com,1999:blog-6487100581508098433.post-3827563737642230092008-01-10T10:38:00.000-08:002008-03-16T08:50:31.681-07:00C# never made me anymore happier than today!I wasn't anymore happier than today in ... hmm... in the last few months!<br /><br />I have been working with a project where I need to convert a VB.Net project into C# one. The whole project contains 40 other C++ projects which are mostly libraries and plug-ins. The full project is huge.. only the source files (.cpp, .h, .vb) are probably 50+ MB.<br /><br />Now there are freeware tools, both web based and desktop ones, to convert a chunk of VB.Net code to C# ones. There are some good articles on that as well. SharpDevelop, the open source C# development tool has a built in feature to convert VB.Net codes/solutions to C#. Some web based free conversion services are based on SharpDevelop. SharpDevelop does a average job in real life applications (like the one I'm working with) but it's far from being 'good'. It produces numerous amount of errors which of course we need to fix manually. It's really poor for converting VB's "implicit type conversion", "globals" and many other common stuff!<br /><br />As our client suggested, I used a commercial conversion tool, it's from vbconversions.net. This one was not as fool as the open source or freeware ones but it yielded a lot of errors too. One funny thing with this tool is that it horribly masses up name spaces and some function parameters (which are not of intrinsic type or has name space), and of course it fails handling VB's implicit conversions too.<br /><br />After fixing thousands of errors for almost a week we finally got our project compiling! Then the final moment arrived, we wanted to run it! We did.. and (as to my expectation).. it crashed horribly!<br /><br />After one more week of debugging and tracing down the almost untraceable, horrible and ugly crashes.. I almost began to loose patients!<br /><br />But today, some horrible thread related bugs were solved dramatically.. and guess what.. finally .. we got what we wanted... we got it running today! The project loads and shows the 3D models the way it should do.. well, even though there are a lot of bugs we need smash!<br /><br />When the project ran and gave me the same output of the VB.Net project, it was a beautiful thing to see and a very interesting moment. It made me really happy.. :)<br /><br />Now, it gives a lot of inspiration and stamina to fix the rest of bugs. I'll have a formal QA first on this stage of the project and then start fixing. So rest of the bugs, be aware.. here I come.. :)<br /><br />By the way, the client of the project I am working for is one of the leading game development companies in the world, everyone knows their name and it's interesting to work for them.Kaisarhttp://www.blogger.com/profile/07878693490452605144noreply@blogger.com1tag:blogger.com,1999:blog-6487100581508098433.post-14298517891883310672007-12-17T03:06:00.000-08:002007-12-17T03:35:42.718-08:00Upcoming Google Knol - a Wikipedia killer?Google has just announced their latest upcoming application called 'Google Knol'. It's a tool for posting and sharing information in the form of articles. Much like Wikipedia, Google Knol just seems to be more social and focusing more on the author's name, and of course its 'Google' branded. 'Google branded' means Knol will have more higher rank results than the Wikipedia, answers.com or other sites in search results on general topics.<br /><br />Users can share, rate, suggest edit, add contents to articles. There can be more than one article on a topic from different authors, unlike Wikipedia. In Wikipedia, all aurthors edit/enhance one article per topic. Wikipedia is 'topic centric' where as Google Knol will be 'author centric'.<br /><br />Here's the intro at Google's official blog:<br />"The web contains an enormous amount of information, and Google has helped to make that information more easily accessible by providing pretty good search facilities. But not everything is written nor is everything well organized to make it easily discoverable. There are millions of people who possess useful knowledge that they would love to share, and there are billions of people who can benefit from it. We believe that many do not share that knowledge today simply because it is not easy enough to do that. The challenge posed to us by Larry, Sergey and Eric was to find a way to help people share their knowledge. This is our main goal."<br /><br />So is this a Wikipedia killer? In my point of view, it's not gonna be an easy task for Google to takeover the popularity of Wikipedia anytime soon. They need something more jaw breaking (a feature, functionality or an idea) to do so. Well only time can tell the 'actual' story.. :).<br /><br />More on Google's Blog:<br /><a href="http://googleblog.blogspot.com/2007/12/encouraging-people-to-contribute.html">http://googleblog.blogspot.com/2007/12/encouraging-people-to-contribute.html</a><br /><br />Constructive comments by Roger Ehrenberg, who's not so optimistic about the success of Knol, is here:<br /><a href="http://seekingalpha.com/article/57457-google-knol-not-setting-the-world-on-fire.">http://seekingalpha.com/article/57457-google-knol-not-setting-the-world-on-fire.</a>Kaisarhttp://www.blogger.com/profile/07878693490452605144noreply@blogger.com1tag:blogger.com,1999:blog-6487100581508098433.post-7965449620041143942007-12-11T17:56:00.000-08:002007-12-11T18:54:12.853-08:00Visual Studio.Net for freeYes, many of us do not know this fact but there are free editions of the mighty Visual Studio IDE, the languages: Visual C++, Visual C#, Visual Basic and SQL Server. It's called the 'Express Editions'.<br /><br />Visual Studio Express Editions (2005, 2008, etc) are free versions of Visual Studio professional editions with limitations on some professional and enterprise features. Targets of Express Editions are mostly beginners, hobbyist and students.<br /><br />This very good strategy of Microsoft is definitely targeted to reach the mass market and make more programmers divert to or become Visual Studio developers. Open source tools, languages and operating systems are causing Microsoft to loose ground at an alarming rate over the last few years. The free editions are one of few and good steps of Microsoft against it.<br /><br />Whatever the reasons for the express editions are, it's a great news for beginners, students and hobbyists. I've used C# express edition (2005) for a moderate sized application earlier this year and hardly noticed any significant changes. You can easily switch between the Visual Studio professional and express editions too. So, if you begin developing an application in Express Edition and want to move to professional edition later, it's a piece of cake!<br /><br />Web site & downloads: <a href="http://www.microsoft.com/express/">http://www.microsoft.com/express/.</a>Kaisarhttp://www.blogger.com/profile/07878693490452605144noreply@blogger.com1