03.12.2012 Views

C++ for Scientists - Technische Universität Dresden

C++ for Scientists - Technische Universität Dresden

C++ for Scientists - Technische Universität Dresden

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

58 CHAPTER 2. <strong>C++</strong> BASICS<br />

Now that the function is complete, we first run our test. Evidently, we have to uncomment<br />

part of the test because we only implemented one function so far. But it is worth to know if<br />

this first function already behaves as expected. It does and we could be now happy with it and<br />

turn our attention to the next task, there are still many. But we will not.<br />

Well, at least we can be happy to have a correctly running function. Nevertheless, it is still<br />

worth spending some time to improve it. Such improvements are called refactoring. Experience<br />

from practise has shown that refactoring immediately after implementation is takes much less<br />

time than later modification when bugs are discovered, the software is ported to other plat<strong>for</strong>ms<br />

or extended <strong>for</strong> more usability. Obviously, it is much easier now to simplify and structure our<br />

software immediately when we still know what is going on than in some week/months/years or<br />

when somebody else is refactoring it.<br />

First thing we might dislike is that something so simple as the initialization of a unit vector<br />

takes 5 lines. This is rather verbose. Putting the if statement in one line<br />

is badly structured.<br />

<strong>for</strong> (unsigned i= 0; i < n; ++i)<br />

if (i == k) e k[i]= 1.0; else e k[i]= 0.0;<br />

C ++ and even good ole C have a special operator <strong>for</strong> conditions<br />

<strong>for</strong> (unsigned i= 0; i < n; ++i)<br />

e k[i]= i == k ? 1.0 : 0.0;<br />

The conditional operator ‘?:’ usually needs some time to get used to but it results in a more<br />

concise representation. There are also situations where one cannot use an if but the ?: operator.<br />

Although, we have not changed anything semantically in the program and it seems obvious that<br />

the result will still be the same, it cannot harm to run our test again. You will see, how often<br />

you are sure that your program changes could never possibly change the behavior but still do.<br />

And the sooner you realize the better. And with the test we already wrote it only takes a few<br />

seconds and makes you feel more confident.<br />

If we would like to be really cool we could explore some insider know how. The expression<br />

‘i == k’ returns a boolean and we know that bool can be converted implicitely into int. In this<br />

conversation false results in 0 and true returns 1 according to the standard. This are precisely<br />

the values we want as double:<br />

e k[i]= double(i == k);<br />

In fact, the conversion from int to double is per<strong>for</strong>med implicitly and can be omitted:<br />

e k[i]= i == k;<br />

As cute as this looks, it is some stretch to assign a logical value to a floating point number. It is<br />

well-defined by the implicit conversion chain bool → int → double but it will confuse potential<br />

readers and you might end up explaining them what is happening on a mailing list or you add<br />

a comment to the program. In both cases you end up writing more <strong>for</strong> the explication than you<br />

saved in the program.<br />

Another thought that might occur to us is that it is probably not the last time we need a unit<br />

vector. So, why don’t writing a function <strong>for</strong> it?

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!