std.digest: can we get rid of start() functions?

H. S. Teoh hsteoh at quickfur.ath.cx
Wed Oct 10 08:47:52 PDT 2012


On Wed, Oct 10, 2012 at 05:16:38PM +0200, Marco Leise wrote:
> Am Wed, 10 Oct 2012 16:28:36 +0200
> schrieb Johannes Pfau <nospam at example.com>:
> 
> > There's makeDigest which calls start for you, so you can write code
> > like this:
> > 
> > auto sha1 = makeDigest!SHA1();
> > //calculate hash
> > sha1.start(); //start again
> > //calculate hash...
> 
> Just this point, I think it adds mental bloat. It is
> still easier to remember and shorter to use:
> 
> SHA1 sha1;
> sha1.start();
> //calculate hash
> sha1.start();
> //calculate hash...
[...]

What makes it so difficult that we cannot do this:

	struct MyHash {
		bool is_init = false;
		...

		void init() {
			...
			is_init = true;
		}
		void computeHash(...) {
			if (!is_init)
				init();

			// Continue as usual
			...
		}
	}

Then you could just write:

	SHA1 sha1;
	// calculate hash
	sha1 = SHA1.init;
	// calculate hash
	...

Fits in better with the language, and no need to remember to call
start(), which adds mental load 'cos you have to remember it's called
"start" and not something else, and it's also is error-prone 'cos people
will forget to do it.


T

-- 
Famous last words: I wonder what will happen if I do *this*...


More information about the Digitalmars-d mailing list