Idiomatic D?
bearophile
bearophileHUGS at lycos.com
Mon Jun 18 09:54:29 PDT 2012
Matt Diesel:
> Firstly, is there any good resource on what idiomatic D usage
> is? For instance Go has a huge page called Effective Go which
> tells you how you should use features, rather than what they
> are.
I don't know any such page, but it looks like an interesting
addition for the online docs.
> And secondly, I would be grateful if anyone could take a look
> at this very simple program and comment on how I'm using the
> language (ignoring the fact it's really simple and the lexer is
> rubbish). Just stuff like interface, class and variable naming
> and program structure.
Some notes:
- In D method names start with a lowercase.
- For multi-line ddoc comments there is also /** ... */
- Consider the usage of some structs, where appropriate. Structs
are much more used in D compared to C#.
- Consider adding pure/nothrow/const/in/mmutable (and even @safe
if you want) tags to methods, functions, arguments, and even
normal variables, where possible.
- this.input.length == 0 is better written this.input.empty,
where empty for arrays (and strings) is in std.array.
- Consider the usage of lambdas, to replace function int(int
left, int right) { return left + right; } with (left, right) =>
left + right;.
- Instead of free assert() consider using preconditions,
postconditions and class/struct invariants.
- I suggest to add some unittest{} blocks.
- Instead of using Exception() consider creating one exception
class for your code, or use one of the few standard ones... But I
don't know how many standard ones there are in Phobos.
- Overall you code looks well formatted, well commented, well
written (I have not run it), it's a good start.
Bye,
bearophile
More information about the Digitalmars-d-learn
mailing list