Allegro.cc - Online Community

Allegro.cc Forums » Off-Topic Ordeals » the D programming language

This thread is locked; no one can reply to it. rss feed Print
the D programming language
Thomas Fjellstrom
Member #476
June 2000
avatar

I might use recordCount if I was coding C++/Qt.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Dustin Dettmer
Member #3,935
October 2003
avatar

recordCount or numRecords here. Though to be honest, if the variable was just a count of the records it'd likely be short-scoped. Short-scoped variables I just name generic crap like count or num.

bamccaig
Member #7,536
July 2006
avatar

Dustin Dettmer said:

Short-scoped variables I just name generic crap like count or num.

:o Count of what!? What num!? :'( Personally I was taught to use descriptive variable names at all times. I prefer to use descriptive names so that my code and variables explain adequately what should be happening... And comments fill in the rest. Otherwise, somebody needs to figure out where count or num are coming from and what they represent.

It's also messy if another count or num is required in the same scope because then you need to rename the other variables or work around it with the new variables. Do I hear a count2/num2? :P

Thomas Fjellstrom
Member #476
June 2000
avatar

Quote:

:o Count of what!? What num!? :'(

Context my man. If its a really small if or loop, you know what its a count of. its like using i for a loop var.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Jakub Wasilewski
Member #3,653
June 2003
avatar

Quote:

it's like using i for a loop var.

You mean intI? :)

---------------------------
[ ChristmasHack! | My games ] :::: One CSS to style them all, One Javascript to script them, / One HTML to bring them all and in the browser bind them / In the Land of Fantasy where Standards mean something.

bamccaig
Member #7,536
July 2006
avatar

Thomas Fjellstrom said:

Context my man. If its a really small if or loop, you know what its a count of. its like using i for a loop var.

I always declare variables at the top of the file, function, or method. So the smallest scope my variables have is function/method-level. I think the only exception is for loop control variables (see below ;)).

Jakub Wasilewski said:

You mean intI? :)

This is where it gets a little bit awkward using Hungarian notation in C/C++ (or C-like languages)... I like using i in those languages, but then it changes naming conventions... In VB-like languages I usually use intIndex. In C-like languages, I usually use i, j, k, l, etc... :-/

I can't even find any C/C++ code that I've written lately that uses Hungarian notation AND has any need for iteration counters or array indexes... :-/ I don't know what I do!? :'(

Actually, the simple chat server/client programs that I wrote to demonstrate sockets programming used Hungarian notation and just used i as a for loop control variable. :) I'm happy with that for now...

Thomas Fjellstrom
Member #476
June 2000
avatar

Quote:

You mean intI? :)

Kill me now.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Dustin Dettmer
Member #3,935
October 2003
avatar

Quote:

So the smallest scope my variables have is function/method-level.

My functions tend to be small enough to be small-scoped themselves :D.

God I love small and well documented functions. Why must I always be forced to interface functions without defined purpose?

bamccaig
Member #7,536
July 2006
avatar

Dustin Dettmer
Member #3,935
October 2003
avatar

bam said:

Prove it. ;D

Oh ok sure. Pick a method:

1 // Updates the target mice. Should be called at a constant rate.
2 // Primarily handles removing the mice when its time for them to go away.
3 void think();
4 
5 // Used to specify a ball postion for hitsMouse
6 struct Ball { int x, y, radius; };
7 
8 // Returns a TargetMouse instance if 'ball' hits one of the target mice on
9 // the screen. If 'ball' hits none of the target mice on the screen hitMouse
10 // returns 0. The caller may destroy the returned TargetMouse if they so please
11 // using TargetMouse methods but may *NOT* call delete on the pointer.
12 TargetMouse* hitsMouse(Ball ball);
13
14 // Calculates if its time to spawn a new TargetMouse and if nessicary does so
15 // 'circles' represents the current level, its used to determine where the
16 // mice will land.
17 //
18 // If 'force' is false there is some chance that a mouse will be spawned.
19 void calculateSpawns(Circles &circles, bool force = false);
20
21 // Returns the number of mice that are onscreen
22 int numberMice();
23 
24 // The number of mice that had the 'isDead' flag enabled when they were erased.
25 // isDead is set when the mice are killed by the user (as opposed to simply expiring
26 // because of old age).
27 int numberKilledMice;
28 
29 // The number of mice that did not have the 'isDead' flag enabled when they were
30 // erased. isDead is set when the mice are killed by the user (as opposed to simply
31 // expiring because of old age).
32 int numberExpiredMice;
33 
34 // Gets rid of all the mice.
35 void clear();
36
37 // Draws all the target mice to the screen.
38 // 'circles' represents the current level, its used to determine how much
39 // the level has rotated so the mice can be rotated the correct amount.
40 // If eyesAllowedInCenter is true there is a slight probability that blinking mouse eyes
41 // will be drawn on top of the center hole.
42 void draw(bool eyesAllowedInCenter, Circles &circles, const Tint &tint = Tint());

nonnus29
Member #2,606
August 2002
avatar

Wow, you guys argue about some stupid sh*t.

::)

MiquelFire
Member #3,110
January 2003
avatar

It's bam's fault! He's normally the one to start it.

---
Febreze (and other air fresheners actually) is just below perfumes/colognes, and that's just below dead skunks in terms of smells that offend my nose.
MiquelFire.red | +Me
Windows 8 is a toned, stylish, polished professional athlete. But it’s wearing clown makeup, and that creates a serious image problem. ~PCWorld Article

Bob
Free Market Evangelist
September 2000
avatar

Matthew said:

In one file:

That doesn't seem to solve anything beyond C. C does it in exactly the same way.

1class Foo
2{
3public:
4 class Bar* b;
5
6 void foo()
7 {
8 std::cout << "Foo";
9 }
10 
11 void bar()
12 {
13 b->bar();
14 }
15};
16 
17class Bar
18{
19public:
20 class Foo *f;
21 
22 public void foo()
23 {
24 f->foo();
25 }
26 
27 public void bar()
28 {
29 std::cout << "Bar";
30 }
31};
32 
33int main()
34{
35 Foo *f = new Foo();
36 Bar *b = new Bar();
37 
38 f->b = b;
39 b->f = f;
40 
41 f->foo();
42 f->bar();
43 b->foo();
44 b->bar();
45 
46 delete f; // Ok, so you need to manage memory manually...
47 delete b;
48 
49 return 0;
50}

--
- Bob
[ -- All my signature links are 404 -- ]

Matthew Leverton
Supreme Loser
January 1999
avatar

Dustin's example was fairly trivial... all I was doing was translating it to D.

Bob
Free Market Evangelist
September 2000
avatar

This is why I asked if D has stack-allocated non-POD types. If so, then you should be able to do away with your 'new' calls. If not, then the point is moot because C++ also supports the same feature. You just have to remember to use pointers explicitly (as opposed to implicitly, as in D).

--
- Bob
[ -- All my signature links are 404 -- ]

Lars Ivar Igesund
Member #8,746
June 2007

You can allocate on the stack by doing

scope MyObj mystackinstance = new MyObj;

axilmar
Member #1,204
April 2001

Quote:

I think that when people use a language so verbose as C++, over time they tend to believe that all of that complexity is needed in order to be efficient (in terms of runtime speed) and non-ambiguous. So when a language like D comes along and challenges the very notion that such complexity offers nothing, it's a bit of a culture shock.

I think you overstating things a bit. D is quite similar to C++: it's an imperative object-oriented programming language, very much like C++, C# and Java.

If you program anything in Haskell/Lisp/Oz, then you could say it's a culture shock.

For me, D is not real progress in the domain of programming languages, for the following reasons:

1) D does not help really cut down the amount of code it has to be written for a specific task. D might eliminate some of the tedious typing of C++ (namely: headers, pointers, the operator -> etc) but that is not where the bulk of work is in a program. Most algorithms in C++ are exactly the same in D anyway.

2) most bugs in a C++ program would be bugs as well in a D program, even if the consequences in the latter are less catastrophic: null pointers, out of bounds indexes, wrong order of operations etc.

What I would like to see is a truly advanced programming language that really minimizes development time:

-more declarative programming, less imperative programming.
-catches logic errors.
-allows for interactive compilation.
-allows for incremental development.

In the end, a D program can be as spaghetti as a C++ program. For me, there is no real reason to migrate to D, especially when the libraries are of high quality (Qt for example) and the job gets done with minimal fuss in C++.

Lars Ivar Igesund
Member #8,746
June 2007

Quote:

1) D does not help really cut down the amount of code it has to be written for a specific task. D might eliminate some of the tedious typing of C++ (namely: headers, pointers, the operator -> etc) but that is not where the bulk of work is in a program. Most algorithms in C++ are exactly the same in D anyway.

DMDScript, which is a fairly direct C++ -> D port of DMCScript, have 30% fewer lines of code, much because of the lack of tedious typing of explicit memory allocation (yes, I know the next revision of C++ will have optional garbage collection). Several bugs where also caught by the index checking and similar features. It may not be revolution, but a rather huge step in evolution.

As for incremental development, this is much easier to do in D, if only because it is so much faster to compile.

HoHo
Member #4,534
April 2004
avatar

Quote:

As for incremental development, this is much easier to do in D, if only because it is so much faster to compile.

I can turn off all sorts of fancy optimization flags when compiling my programs and have stuff compiled in a blink of an eye, for multiple files having high-end multicore system also helps quite a bit.

Though there is one thing that often takes quite a bit of time: linking. Is D compiler any faster than C/C++ compilers (gcc) in that area?

__________
In theory, there is no difference between theory and practice. But, in practice, there is - Jan L.A. van de Snepscheut
MMORPG's...Many Men Online Role Playing Girls - Radagar
"Is Java REALLY slower? Does STL really bloat your exes? Find out with your friendly host, HoHo, and his benchmarking machine!" - Jakub Wasilewski

Lars Ivar Igesund
Member #8,746
June 2007

The object files are the same, so the linking usually takes the same amount of time (not that I have been bothered by that). Compilation and linking takes a few seconds for larger projects. The linker that comes with the windows version of DMD is blindingly fast though.

On linux I see a notable slow down when using GDC instead of DMD as the compiler (gcc is used for linking in both cases). This is mainly due to GDC not yet being able to process all files at once.

Templates can slow down compilation a bit, but is still quite a bit faster than C++ afaik.

axilmar
Member #1,204
April 2001

Quote:

DMDScript, which is a fairly direct C++ -> D port of DMCScript, have 30% fewer lines of code, much because of the lack of tedious typing of explicit memory allocation (yes, I know the next revision of C++ will have optional garbage collection). Several bugs where also caught by the index checking and similar features. It may not be revolution, but a rather huge step in evolution.

It all depends in the code you write. In Qt, for example, memory management is almost implicit: you never have to delete objects yourself...similar code as in Java Swing.

Quote:

As for incremental development, this is much easier to do in D, if only because it is so much faster to compile.

It may be slightly easier, but not as interactive as I would like. I have played with Common Lisp a little, and interactive development is so much better than the cycle edit-compile-debug.

Lars Ivar Igesund
Member #8,746
June 2007

Quote:

It may be slightly easier, but not as interactive as I would like. I have played with Common Lisp a little, and interactive development is so much better than the cycle edit-compile-debug.

But then you are talking about interpreted languages, which is quite a different beast (even if it in theory is possible to interpret D).

torhu
Member #2,727
September 2002
avatar

About the speed of DMD. I posted this in another thread, which is compiling C code, but DMD uses the same code generator as DMC does. Building allegro with dmc is supported in the 4.2 svn branch.

Quote:

Command: 'gmake lib STATICLINK=1 ALLEGRO_USE_C=1'

GCC: 107 seconds
msvc6: 55 seconds
DMC: 42 seconds

I did this a few times and got similiar timings. Of course, this is C, not C++.

Comparing GDC to DMD is not entirely fair, since like Lars Ivar said, GDC compiles one file at a time, while DMD accepts all the files at once. Building a static link library version of DAllegro takes 0.5 secs with dmd and 11 secs when using gdc's gdmd frontend (gdmd is a perl script). That difference means you don't need a makefile with dmd in this case.

It's also perfectly reasonable to use dmd for script-like tasks. On unix, just put '#! /bin/dmd -run' at the top of the file, and it's compiled to a temporary file and run. This might sound silly, but it makes sense when you need some special processing done during the build process of a D library, etc. Windows doesn't come with a usable scripting language, so it's a way to avoid the need for extra tools. But I admit it's more of a gimmick sometimes. What I mostly use -run for is to compile and run small test programs etc. No executable or object files are left when the program exits.

axilmar
Member #1,204
April 2001

Quote:

But then you are talking about interpreted languages, which is quite a different beast (even if it in theory is possible to interpret D).

You can develop in an interpreted language environment, and when you are satisfied with the result, you can produce an executable.

Lars Ivar Igesund
Member #8,746
June 2007

Yes, but since D compiles so fast and compiled code probably will execute so much faster than interpreted D, the environment you want would probably be better implemented by making a somewhat more innovative IDE that appears to look like an interpreter by wrapping the compiled executables.



Go to: