Knowing the approach to solve a D challenge
Chris
wendlec at tcd.ie
Fri Feb 16 11:17:11 UTC 2018
On Friday, 16 February 2018 at 09:44:27 UTC, aberba wrote:
> D has tone of features and library solutions. When you
> encounter a problem, how do you approach solving it in code?
>
> 1. Do you first write it in idiomatic D style or a more general
> approach before porting to idiomatic D?
As idiomatic as possible. But it depends on how big and/or
important the method/function is. The more important and/or
bigger, the more idiomatic. Sometimes a `foreach` will do in a
small function.
But D makes it easy to write idiomatic code straight away,
although sometimes it can be a bit annoying when you get messages
like "cannot implicitly convert x of type y to z". So you have to
keep track of what you are chaining.
> 2. Do you find yourself mostly rolling out your own
> implementation first before using a library function?
I usually start to roll out my own for a few minutes until I say
"Hold on, maybe the library already has a function that does
exactly that!" And it often does. That's because I get a better
understanding of what I need, when I roll my own for a while.
Using the library straight away can sometimes result in shooting
yourself in the foot. So it's a mix of both.
> 3. Do the use of generics come out of first try or a rewrite?
Depends on the problem at hand. I you know you will have to
handle several types down the road, it's a minimal generic that
accepts only one type to begin with but that can be extended to
others. It's a bit "play by ear". If I'm dealing with HTML and I
know that JSON or CSV will be needed later, I write the function
in a way that it can easily be extended. If a function returns
the user name as a `string`, I won't bother with generics. That's
just normal, I think.
> 4. What rough percentage of phobos knowledge is required for
> reasonable good problem solving efficiency?
You cannot know every function, of course, and new functions are
being added all the time. What you need to know is what Phobos
can do in general and where to look for it. The cheat sheet
should be the first port of call. You can save a lot of time by
skimming through the library regularly. Also, it's worth looking
at other people's code in the "learn" section. What often happens
to me, when I see a solution, I go "Jesus, that's clever, I'll
keep that in mind!" The learn section is good because it focuses
on one (small) problem at a time.
More information about the Digitalmars-d
mailing list