www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - TDPL notes, part 1

reply bearophile <bearophileHUGS lycos.com> writes:
I have finally received my copy of The D Programming Language :-)
This is a first post of notes that I am writing while I read this text for the
first time.

I'd like to know how many copies have being sold so far, it can give a starting
idea of how many people are interested in D2.

I have seen the PDF on the site, but I'd like to see colored code in the
electronic text. Source code colorization improves code readability (an offline
PDF version that allows copy&paste can allow me to give better quotations here).


About the cover of the book:
- My copy of the book is the authorless one. Despite being a "bug", it makes
this cover page strangely clean and elegant, because it removes a source of
noise from it :-)
- Strong contrasting colors are not elegant, shades (here of grey) are often
more elegant, but I think the uppercase D is the most important element of the
whole cover, so I think it's better written in a dark red color.
- The cover art is not bad, I don't mind it. But it's good to keep in mind that
such style of art is now about one century old, it's not contemporary art :-)


I like the prefaces, expecially the one written by Walter, that explains what
are hir original (as in almost-child ones) purposes. I hope Walter will be able
to create another engineering system in his future, like another language, etc.

---------------

CHAPTER 1:

P 3-4 (Page 3 and 4): I don't agree that omitting the type, and leaving just
"immutable" gives a program as safe. Conversion constants for units don't
change, but often hardcoded constants in programs do change.
If I have code like:
immutable foo = 12.2;
And later I have to change the value to 12, I can forget the dot:
immutable foo = 12;
This changes the type of foo. This can cause problems. It's better to write
code that is not fragile and doesn't cause problems if the programmer is a bit
distracted.
- Isn't it better to use 'enum' instead of 'immutable' in your program here? I
see no purpose in using the more wordy 'immutable' here. I don't like the
keyword 'enum' to define constants in D, but this is what we got.


P 5: clear writefln format string errors: even better, the D compiler in most
cases knows the format string statiacally, so most times it can perform static
typing, as GCC does:
http://d.puremagic.com/issues/show_bug.cgi?id=4458


P 6: Python-style indentation of D2 code: it's named Delight:
http://delight.sourceforge.net/
(Well, it's not a preprocessor, it's more like a variant of D2 (it introduces
few improvements, like nonnull references).


P 8, first program:
- Unsigned integers are dangerous in D, their usage must be discouraged as much
as possible, use them only when they are strictly necessary, they are premature
optimization, don't use them in the first few hundred pages of the book. And
assigning length to an uint loses precision anyway, because length is a size_t
that can be 64 bits too, so in this early example it is *much* better to use
just one int value.


P 8, second code:
- In Python default formatting rules (PEP 8) say that lists (arrays) can be
written as: [1, 2, 3]. I too prefer this formatting compared to the one you
use: [ 1, 2, 3 ].
- "new int[20];" I don't like this much. In D2 there are two different syntaxes
to define dynamic arrays:
new int[20];
new int[](20);
Having two different syntaxes to do the same thing is bad. I'd like the first
one (new int[20];) to be deprecated.
Later you can use syntax like:
new int[20][][10](20);
To define a 3D array where some axes are dynamic arrays and some other ones are
fixed sized ones :-)


P 10:
In this line of code:
while (!input.empty) {
There is not so much need of using an external function plus a negation:
while (input.length) {
If you use an IDE it's easy to see where a function comes from, but in a
printed book you can't. So in a book it's better to write:
import std.array: empty;
Instead of:
import std.array;


P 12 second program: any explanation of idup is missing, but this can wait
later.
The strip() there is useless, split ignores leading and trailing spaces, you
can see with this program:
import std.stdio, std.string;
void main() {
    string s = "  how are  you?  \t";
    auto a = split(s);
    foreach (ss; a)
        writeln(">", ss, "<");
}
It prints:
how<
are<
you?<
P 14: This is not a lambda function, it's a lambda function template: (a, b) { return freqs[a] > freqs[b]; } "D guarantees no indirect calls": in this example indirect calls are avoided because it's a lambda function template, if you use a normal lambda function dmd is not able to inline it yet. P 15: I miss a little the struct inheritance. But it's mostly an efficiency thing, not so important. P 16-17: I like the page 17 that reminds this bug-prone part. But if I use D to write 20-lines long scripts I really don't want to remember to dup all things (in D1 code I sometimes end up dupping too much, to be on the safe side). So I suggest a different API for the line reading: - stdin.byLineMutable() (or another similar name): for the current behaviour that avoids a memory allocation for each line read. This is faster but it's less safe. - stdin.byLine(): that allocates a new string for each line, this is safer, as in Python. D default design policy says that unsafe but faster things need to be asked for, and the default things must be less bug-prone. If I write small scripts I can use byLine(), with more hope to avoid bugs. If later I see it's too much slow, I can replace the byLine() with the other method and optimize the code, carefully. I'll continue in the next posts. I hope to find the time and energy to comment the whole book. Bye, bearophile
Jul 14 2010
next sibling parent BLS <windevguy hotmail.de> writes:
On 14/07/2010 14:28, bearophile wrote:
 I'll continue in the next posts. I hope to find the time and energy to comment
the whole book.
Man, you are really a maniac. Other people simply post a message on Amazon...Like it .. like it, but.. Whatever. But not You. You have to celebrate a dissection.
Jul 14 2010
prev sibling next sibling parent reply Tim Verweij <tjverweij gmail.com> writes:
On 14 July 2010 14:28, bearophile <bearophileHUGS lycos.com> wrote:

 (...)
 P 10:
 In this line of code:
 while (!input.empty) {
 There is not so much need of using an external function plus a negation:
 while (input.length) {
 (...)
I like writing: while (!input.empty) { To me, it better shows the meaning of the condition. Groet, Tim
Jul 15 2010
parent "Lars T. Kyllingstad" <public kyllingen.NOSPAMnet> writes:
On Thu, 15 Jul 2010 12:13:27 +0200, Tim Verweij wrote:

 On 14 July 2010 14:28, bearophile <bearophileHUGS lycos.com> wrote:
 
 (...)
 P 10:
 In this line of code:
 while (!input.empty) {
 There is not so much need of using an external function plus a
 negation: while (input.length) {
 (...)
I like writing: while (!input.empty) { To me, it better shows the meaning of the condition.
I completely agree. I have started using the range interface for arrays whenever possible. It makes code a lot more self-explanatory, easier to read, and easier to reason about. -Lars
Jul 15 2010
prev sibling parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 07/14/2010 07:28 AM, bearophile wrote:
 I have finally received my copy of The D Programming Language :-)
 This is a first post of notes that I am writing while I read this
 text for the first time.
Thanks. Though the effort is definitely to be appreciated, there is a high risk that such long streams of consciousness come and go and are forgotten. There are more persistent places for proposing changes to the language or the book: - for bug reports and enhancement proposals for D, use our Bugzilla repository (http://d.puremagic.com/issues) - for book errata use http://erdani.com/tdpl/errata
 I'd like to know how many copies have being sold so far, it can give
 a starting idea of how many people are interested in D2.
I receive a statement from the publisher every 6 months. My understanding is that there are significant seasonal variations (summer are good reviews.
 I have seen the PDF on the site, but I'd like to see colored code in
 the electronic text. Source code colorization improves code
 readability (an offline PDF version that allows copy&paste can allow
 me to give better quotations here).
Unfortunately my choice in the matter is pretty limited. For the publisher a different version than the one printed is a red flag - in theory there could be differences in content too. I'll see what I can do.
 P 8, first program: - Unsigned integers are dangerous in D, their
 usage must be discouraged as much as possible, use them only when
 they are strictly necessary, they are premature optimization, don't
 use them in the first few hundred pages of the book. And assigning
 length to an uint loses precision anyway, because length is a size_t
 that can be 64 bits too, so in this early example it is *much* better
 to use just one int value.
That's indeed a bug, I primed the errata on your behalf. Andrei
Jul 15 2010
parent reply bearophile <bearophileHUGS lycos.com> writes:
Andrei:

Though the effort is definitely to be appreciated,<
Oh, good :-) Usually the more I like something, the more comments/criticism I write about it :-)
there is a high risk that such long streams of consciousness come and go and
are forgotten. There are more persistent places for proposing changes to the
language or the book:<
I will add bits there after I have posted things here. From the start of my TDPL reading I have added some bugs to Bugzilla: http://d.puremagic.com/issues/show_bug.cgi?id=4463 http://d.puremagic.com/issues/show_bug.cgi?id=4458 http://d.puremagic.com/issues/show_bug.cgi?id=4474 http://d.puremagic.com/issues/show_bug.cgi?id=4475 http://d.puremagic.com/issues/show_bug.cgi?id=4476 But I often don't know what can be considered an error worth for the book errata corrige. Most of the things I write here are comments or enhancements, or a matter of taste and judgement, not true errors. In the part 1 of my notes I have said that having two syntaxes to allocate an array is bad, the second one is more general and enough (it's just 2 chars longer for 1D arrays and no longer for 1+ND arrays): new int[20]; new int[](20); But deprecating the first syntax doesn't look easy. So I don't know if this too is worth a Bugzilla report. I buy one or more books every week, but I also like a pdf version, that allows me to read it on a screen or to copy&paste quotations to friends, when I want to discuss with them about the book contents (even when they are not interested enough in the topic to buy the book themselves). I don't like to buy a book two times, in paper and pdf versions, just because I want to send a total of about 50-100 lines of text to a friend. This is not how the 'Republic of Books' is supposed to be :-) Bye, bearophile
Jul 16 2010
parent bearophile <bearophileHUGS lycos.com> writes:
Sorry, I have forgotten your last answer.

Andrei:

 That's indeed a bug, I primed the errata on your behalf.
I have given my best answers on this signed/unsigned topic in a recent discussion with Stewart Gordon and Michal Minich on D.learn (the links are about their posts too): http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D.learn&article_id=20349 http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D.learn&article_id=20350 http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D.learn&article_id=20356 http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D.learn&article_id=20357 http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D.learn&article_id=20360 http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D.learn&article_id=20362 http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D.learn&article_id=20369 http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D.learn&article_id=20371 http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D.learn&article_id=20407 Walter is currently busy with the 64 bit port, but I don't think it's easy for him to give good answers to my points there. Bye, bearophile
Jul 16 2010