D programming Language



Download 1.66 Mb.
Page46/47
Date08.01.2017
Size1.66 Mb.
#7507
1   ...   39   40   41   42   43   44   45   46   47

The D Style


The D Style is a set of style conventions for writing D programs. The D Style is not enforced by the compiler, it is purely cosmetic and a matter of choice. Adhering to the D Style, however, will make it easier for others to work with your D code and easier for you to work with others' D code. The D Style can form the starting point for a D project style guide customized for your project team.

White Space


  • One statement per line.

  • Two or more spaces per indentation level.

  • Operators are separated by single spaces from their operands.

  • Two blank lines separating function bodies.

  • One blank line separating variable declarations from statements in function bodies.

Comments


  • Use // comments to document a single line:

  • statement; // comment

  • statement; // comment



  • Use block comments to document a multiple line block of statements:

  • /*

  • * comment

  • * comment

  • */

  • statement;

  • statement;



  • Use nesting comments to 'comment out' a piece of trial code:

  • /+++++

  • /*

  • * comment

  • * comment

  • */

  • statement;

  • statement;

  • +++++/


Naming Conventions


General

Names formed by joining multiple works should have each word other than the first capitalized.

int myFunc();

Module


Module names are all lower case.

C Modules

Modules that are interfaces to C functions go into the "c" package, for example:

import c.stdio;

Module names should be all lower case.

Class, Struct, Union, Enum names

are capitalized.

class Foo;

class FooAndBar;

Function names

Function names are not capitalized.

int done();

int doneProcessing();

Const names

Are in all caps.

Enum member names

Are in all caps.

Meaningless Type Aliases


Things like:

alias void VOID;

alias int INT;

alias int* pint;

should be avoided.

Declaration Style


Since in D the declarations are left-associative, left justify them:

int[] x, y; // makes it clear that x and y are the same type

int** p, q; // makes it clear that p and q are the same type

to emphasize their relationship. Do not use the C style:

int []x, y; // confusing since y is also an int[]

int **p, q; // confusing since q is also an int**




Operator Overloading


Operator overloading is a powerful tool to extend the basic types supported by the language. But being powerful, it has great potential for creating obfuscated code. In particular, the existing D operators have conventional meanings, such as '+' means 'add' and '<<' means 'shift left'. Overloading operator '+' with a meaning different from 'add' is arbitrarilly confusing and should be avoided.

Hungarian Notation


Just say no.

Example: wc


This program is the D version of the classic wc (wordcount) C program. It serves to demonstrate how to read files, do array slicing, and simple symbol table management with associative arrays.

import stdio;

import file;
int main (char[][] args)

{

int w_total;



int l_total;

int c_total;

int[char[]] dictionary;
printf(" lines words bytes file\n");

for (int i = 1; i < args.length; ++i)

{

char[] input;



int w_cnt, l_cnt, c_cnt;

int inword;

int wstart;
input = File.read(args[i]);
for (int j = 0; j < input.length; j++)

{ char c;


c = input[j];

if (c == "\n")

++l_cnt;

if (c >= "0" && c <= "9")

{

}

else if (c >= "a" && c <= "z" ||



c >= "A" && c <= "Z")

{

if (!inword)



{

wstart = j;

inword = 1;

++w_cnt;


}

}

else if (inword)



{ char[] word = input[wstart .. j];
dictionary[word]++;

inword = 0;

}

++c_cnt;


}

if (inword)

{ char[] word = input[wstart .. input.length];

dictionary[word]++;

}

printf("%8lu%8lu%8lu %s\n", l_cnt, w_cnt, c_cnt, (char *)args[i]);



l_total += l_cnt;

w_total += w_cnt;

c_total += c_cnt;

}
if (args.length > 2)

{

printf("--------------------------------------\n%8lu%8lu%8lu total",



l_total, w_total, c_total);

}
printf("--------------------------------------\n");

char[][] keys = dictionary.keys;

for (int i = 0; i < keys.length; i++)

{ char[] word;
word = keys[i];

printf("%3d %.*s\n", dictionary[word], word);

}

return 0;



}

Compiler for D Programming Language


This is the D compiler for Win32.

Files


\dmd\bin\dmd.exe

D compiler executable

\dmd\bin\shell.exe

Simple command line shell

\dmd\bin\sc.ini

Global compiler settings

\dmd\lib\phobos.lib

D runtime library

\dmd\src\phobos\

D runtime library source

\dmd\src\dmd\

D compiler front end source under dual (GPL and Artistic) license

\dmd\html\d\

Documentation

\dmd\samples\d\

Sample D programs


Requirements


  • 32 bit Windows operating system

  • D compiler for Win32

  • linker and utilities for Win32


Download 1.66 Mb.

Share with your friends:
1   ...   39   40   41   42   43   44   45   46   47




The database is protected by copyright ©ininet.org 2024
send message

    Main page