Wish: Variable Not Used Warning
Markus Koskimies
markus at reaaliaika.net
Thu Jul 10 19:53:23 PDT 2008
On Thu, 10 Jul 2008 12:51:47 -0700, Walter Bright wrote:
> Robert Fraser wrote:
>> In a final release, unused things are signs of errors. When writing
>> code, unused variables (perhaps they were used in a commented-out
>> section?) are a dime a dozen.
>
> Yes, that's why I find the warning to be a nuisance, not a help.
I have thought this issue more closely now about few hours. I'll try
write a more comprehensive answer to my home pages, but something now
quickly thought;
* What is the purpose of the source code? There is probably lots of
answers, but I will present you just one; the source code is aimed for
human readers & writers.
Certainly this does not mean, that the source would be readable by John
Doe - certainly, you need to learn the practices used by one programming
language and get familiar with that before you can say anything about the
practicality of one programming language.
An ultra-short history; I have been programming about 30 years, done it
for my living about 20 years. My "brain damage" is that I have been
programmed mostly embedded systems, DSP, MCU and microcontrollers, so
people more familiar with Win/Linux programming can tell where I'm wrong.
I have never be a programming language purist. In fact, most of my
colleges think that I'm a misuser of OOP, since I see nothing wrong of
using goto's, large switch-cases or God-objects, if they just work.
I think, that source code is not for the compiler. Compilers can deal
with languages not having any kind of syntactic salt, they does not
require comments, and they generally give a shit about indentation. No,
source code is not meant to be compiler-friendly (although it is very
good that it is one, for completely different reasons), instead it is
aimed to be read by a human - it is the bridge between informal humans
and formal compilers.
How could you improve your source code specification? I think that there
is just one answer, which D at the moment follows; the more complex
programs you can understand by reading the source (and being familiar
with that specific language), the better it is. I know that that sounds
something humanists, but really - if the source is not meant to be
understood by limited capability of humans, why the hell we are using (1)
modular languages, (2) high level languages, (3) _indentation_, (4)
__COMMENTS?!?__?!?
Tell me, that source code is something else that fundamentally aimed to
be understood by a human, which is familiar with the language, and I'll
reconsider the following.
---
Since source code is aimed for humans, to be able to understand even more
complex structures, what is the purpose of warnings?
Many of us D-users have a long experience with C, C++ and Java. Many of
us are well aware about the problems in those languages. There is a very
solid reason that I - and we - nowadays use D for my/us freetime
activities, and why I'm advocating C++ people to give D a try (I'm very
sad, that being a few years out from the community, there is currently a
big fight between DMD-GDC-Tango-Phobos; that is something we all need to
solve, to really make D as the future programming language - what it can
be IMO!)
But those warnings? I know that some of the readers think that warnings
are something that indicate inadequates in the design of the programming
language. But - referring to the previous parts - what is the sole reason
of source language? All of you that think that D does not need any more
warnings, please answer to this question:
There is two source codes, and lets assume that both of them are valid D;
1)
class A
{
int thing;
int getThing() { return thing; }
};
2)
class A
{
private void addThatThing() { ... }
int a = 0;
void *getThatProperty() { ... }
template A(T) { ... }
int getThing()
{
template A(int) { possiblyReturn(1); }
if(itWasNot!(int()) return somethingElse();
return module.x.thatThing();
}
}
Which one is easier to understand?
Yes, I know that the examples are very extreme, but think about this; if
you allow the intermediate code to exists, does it really make the
language better? Consider, that both of the examples are made by a very
experienced D programmer. From compiler point of view, what is the big
difference? Nothing. As long as the complex thing follows the correct
syntax, compiler is happy; but does it mean, that the source is really
understandable by other readers? Does it really follow good programming
practices - and more importantly, do we need to aim people to follow good
programming practices?
Please, say me that complex source code is less error-proof, and I will
stay silent from this issue to the rest of my life!
---
For me, the answer is very clear; yes, we need to guide people to follow
good practices. From human point of view (and I have lots of experience
of reading other people sources), there is a big difference writing an
infinite loop in following examples;
1)
for(;;) { ... }
2)
int a = -1;
int b = (a - 2);
for(uint i = 0; (a*2) + (b*3) < i; i++) { ... }
The first one you recognize in less than a microsecond to an infinite
loop. The second one requires you careful examination, let's say that a
minute, which is 60,000 times more than the first one.
>From the point of compiler, they make no difference. Compiler easily
detects the inifinite loop and drops all unnecessary parts.
Could you resolve the situation with adding more syntactic salt? Like I
said earlier, I think that there is a certain limit for adding that. It
is not anymore useful for telling everything with reserved keywords to
compiler, instead the compiler could strive you to use common good
programming practices - those are called warnings; they does not
necessarily prevent compiler to generate code, but they do not follow the
guidelines of human understandability.
In this end, I know that there is a great resistance against C/C++ in the
D community (I undersand it, but I'm not signing it). What I am asking
for is to follow the principles stated in the "D Overview", and the
common sense; the language is not meant for language purists, instead it
is meant for us trying to write something useful, as easily it can
achieved. That is the real power of D; and it really DOES NOT mean that D
compilers would not have warnings!
More information about the Digitalmars-d
mailing list