Templates for DRY code

codephantom me at noyb.com
Sat Jan 6 03:38:35 UTC 2018


On Saturday, 6 January 2018 at 03:08:19 UTC, Ali Çehreli wrote:
> It's hard to find a balance between fully explicit and fully 
> automatic. I find myself going back and forth between those two 
> extremes.
>
> Ali

Code is something that humans write and read (and read far more 
than write).

So I prefer to optimise for mental processing ;-)

By this, I mean reducing the amount of information I need to 
chunk, to make sense of something (whether that be writing, or 
reading).

e.g

double[] a;
string s = "1.2";
a.append(s)

requires me to go off and discover what append is doing, since 
'append' is clearly not correctly describing what is actually 
going on here, in this little chunk.

So, now I have to go off and discover the extra chunk I need, in 
order to make sense of this little chunk. Suddenly, the chunk has 
become a lot larger than it needed to be.

so yeah, a simple rename of append would help..

or even..

a.append( s.to!ConvertToElementType(a) );

That's not valid code of course, but the semantics are all 
contained in that single chunk.



More information about the Digitalmars-d-learn mailing list