www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - baseName(path).startsWith(something)

reply Martin Nowak <code dawg.eu> writes:
Neither this

if (path.baseName.startsWith(something))
     doThis();

nor this

if (startsWith(baseName(path), something))
     doThis();

but a combination of the two

if (baseName(path).startsWith(something))
     doThis();

make it possible to write readable sentences.

-Martin
Jan 09 2014
parent reply "monarch_dodra" <monarchdodra gmail.com> writes:
On Thursday, 9 January 2014 at 18:53:58 UTC, Martin Nowak wrote:
 Neither this

 if (path.baseName.startsWith(something))
     doThis();

 nor this

 if (startsWith(baseName(path), something))
     doThis();

 but a combination of the two

 if (baseName(path).startsWith(something))
     doThis();

 make it possible to write readable sentences.

 -Martin
...is that a question? I find the first is most readable. Well, (IMO) with added parens too: if (path.baseName().startsWith(something)) doThis(); The "standard" one: startsWith(baseName(path), something) looks just plain horrible (IMO). The last one looks... intermediate.
Jan 09 2014
next sibling parent reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Thu, Jan 09, 2014 at 07:06:18PM +0000, monarch_dodra wrote:
 On Thursday, 9 January 2014 at 18:53:58 UTC, Martin Nowak wrote:
Neither this

if (path.baseName.startsWith(something))
    doThis();

nor this

if (startsWith(baseName(path), something))
    doThis();

but a combination of the two

if (baseName(path).startsWith(something))
    doThis();

make it possible to write readable sentences.

-Martin
...is that a question? I find the first is most readable. Well, (IMO) with added parens too: if (path.baseName().startsWith(something)) doThis(); The "standard" one: startsWith(baseName(path), something) looks just plain horrible (IMO). The last one looks... intermediate.
I agree the standard one looks horrible. I actually prefer the last one, though. It makes it clear what you're passing to startsWith. And AFAICT, is supported by the current dmd. T -- Why waste time learning, when ignorance is instantaneous? -- Hobbes, from Calvin & Hobbes
Jan 09 2014
parent "Martin Nowak" <code dawg.eu> writes:
On Thursday, 9 January 2014 at 20:39:11 UTC, H. S. Teoh wrote:
 And AFAICT,
 is supported by the current dmd.
Yes it is, isn't that amazing?
Jan 09 2014
prev sibling parent reply "Martin Nowak" <code dawg.eu> writes:
On Thursday, 9 January 2014 at 19:06:19 UTC, monarch_dodra wrote:
 ...is that a question?
I just watend to share the discovery. I always wondered what bothers me about some UFCS overuse and it's the sentence part that matters.
 I find the first is most readable. Well, (IMO) with added 
 parens too:

 if (path.baseName().startsWith(something))
     doThis();
But how do speak that out in your head? The other one is, if the basename of path starts with something do this.
Jan 09 2014
next sibling parent reply "Vladimir Panteleev" <vladimir thecybershadow.net> writes:
On Thursday, 9 January 2014 at 21:01:05 UTC, Martin Nowak wrote:
 if (path.baseName().startsWith(something))
    doThis();
But how do speak that out in your head?
It sounds OK if you use possessive for noun properties: "If path's base name starts with something, do this".
Jan 09 2014
parent "Peter Alexander" <peter.alexander.au gmail.com> writes:
On Thursday, 9 January 2014 at 21:17:33 UTC, Vladimir Panteleev 
wrote:
 On Thursday, 9 January 2014 at 21:01:05 UTC, Martin Nowak wrote:
 if (path.baseName().startsWith(something))
   doThis();
But how do speak that out in your head?
It sounds OK if you use possessive for noun properties: "If path's base name starts with something, do this".
If read it as "if the path base name starts with something", which makes sense (at least to me!)
Jan 09 2014
prev sibling parent reply "monarch_dodra" <monarchdodra gmail.com> writes:
On Thursday, 9 January 2014 at 21:01:05 UTC, Martin Nowak wrote:
 On Thursday, 9 January 2014 at 19:06:19 UTC, monarch_dodra 
 wrote:
 ...is that a question?
I just watend to share the discovery. I always wondered what bothers me about some UFCS overuse and it's the sentence part that matters.
 I find the first is most readable. Well, (IMO) with added 
 parens too:

 if (path.baseName().startsWith(something))
    doThis();
But how do speak that out in your head? The other one is, if the basename of path starts with something do this.
I guess you have to have a "lingual" thinking pattern? *Personally*, that's not really my case. I grew up learning 3 different languages, so that could have helped? If you think of it more in terms of "ideas" than "words", I find the first version makes more sense. You start with a "subject", and you apply a series of "verbs" to said subject. -Start with "path". -Take it's "baseName". -Does that "start with something"? I see that as "three atomic sentences". I understand your version more like: -start with the "basename of path" or start with "path's basename". -Does that "start with something"? Kind of the same think, but the subject is now a complex operation in itself. I guess it depends on *what* you want to put the emphasis on, and how you relate to the objects you are operating on. -------- At the end of the day though, I guess it's a matter of style. For me, UFCS is really about being able to chain operations left to right: subject=>verb=>verb. If anything, I hate it when the first function takes several arguments, but the sum of all arguments are all "subjects" in their own right. For example "iota(low, high).array()". For me, the "subject" is the range "(low, high)". I really wish I could write that as: (low, high).iota().array(); I find it makes sense that way... Also, I think "UFCS abuse" (which is nothing but *style*) is nothing compared to some of the "functional" abuse I've seen when when users to write an entire program as a 1-liner.
Jan 09 2014
parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Thu, Jan 09, 2014 at 10:06:43PM +0000, monarch_dodra wrote:
 On Thursday, 9 January 2014 at 21:01:05 UTC, Martin Nowak wrote:
On Thursday, 9 January 2014 at 19:06:19 UTC, monarch_dodra wrote:
...is that a question?
I just watend to share the discovery. I always wondered what bothers me about some UFCS overuse and it's the sentence part that matters.
I find the first is most readable. Well, (IMO) with added parens
too:

if (path.baseName().startsWith(something))
   doThis();
But how do speak that out in your head? The other one is, if the basename of path starts with something do this.
I guess you have to have a "lingual" thinking pattern? *Personally*, that's not really my case. I grew up learning 3 different languages, so that could have helped? If you think of it more in terms of "ideas" than "words", I find the first version makes more sense. You start with a "subject", and you apply a series of "verbs" to said subject. -Start with "path". -Take it's "baseName". -Does that "start with something"? I see that as "three atomic sentences".
FWIW, I grew up learning 4 languages (and now I'm on my 5th), and I still prefer baseName(path).startsWith(something). :-) [...]
 Also, I think "UFCS abuse" (which is nothing but *style*) is nothing
 compared to some of the "functional" abuse I've seen when when users
 to write an entire program as a 1-liner.
<shameful confession> I wrote a (winning!) IOCCC entry once, where the entire program consists of a single *boolean* expression returned from main(). The ?: operator was not used; branching was achieved by exploiting short-circuit evaluation of && and ||, and looping was achieved by recursively calling main(). That was only the tip of the iceberg of its abusiveness, though, because there are absolutely no variables whatsoever in the program except for the two arguments to main(), and malloc is never called (directly or indirectly), yet the program manipulates arrays and other non-existent variables without crashing. This is done by smashing its own stack upon startup to "allocate" memory, using argv to point to the "allocated" memory, and argc to keep track of what it's supposed to be doing since main() recursively calls itself. :-P Of course, the code itself has the usual IOCCC icing: y'know, the usual abusive #define's, overuse of typecasts, and what-not, that causes the C pretty printer to choke and crash when the would-be decipherer tries to understand the code. But those are small feats compared with the boolean expression stack-smashing memory allocator. :-P </shameful confession> T -- There is no gravity. The earth sucks.
Jan 09 2014