5 ms·
> Copy.from('foo').to('bar') Funny, I don't think I've actually seen a fluent file API like this before. I guess most languages have file I/O built in to thei
by gregmac 7y ago
> Copy.from('foo').to('bar')
Funny, I don't think I've actually seen a fluent file API like this before. I guess most languages have file I/O built in to their standard libraries.
My immediate reaction to this, though, is that you could make it more general:
File('foo').CopyTo('bar');
You could then also have
.ReadAsText()
.MoveTo(target)
.SetOwner(owner)
.GetFullPath()
This is readable and solves the problem the original post is discussing, but it's also nice when learning because an IDE can give very useful help about possible operations, and all you have to remember is the File() function.
- anonytrary 7y ago> also nice when learning because an IDE can give very useful help about possible operations Why is chaining necessary for this? On-instance tab-autocomplete list-all on-tab accomplishes the same thing.
- skrebbel 7y ago> On-instance tab-autocomplete list-all on-tab I have no idea what you just said.
- nurettin 7y agoI think it means: (While editing code, position the cursor) on (a class) instance (and press) tab (to swith to) autocomplete (mode and) list all (members of that instance by clicking) on tab (again).
- coldtea 7y agoHe said that clicking TAB on an instance in an IDE (e.g. foo.[TAB]) can list all possible operations available on that instance -- in other words, you don't need a fluent interface for this.
- stouset 7y agoYour suggestion is basically Ruby’s `Pathname` class (sans convenient copy method).
- yawgmoth 7y agoPersonally, in C#, I don't enjoy debugging and maintaining Fluent APIs. An exception thrown in the chain is sometimes opaque and the necessary extension methods to make everything come together is not neat code. The former reason is why I also discourage use of object initializers.
- tluyben2 7y agoI like those API's much better for reading, but when there are issues with them, like you say, debugging is often a right pain compared to the other examples. Especially when a chain became a bit too long (over time) and there is suddenly a null somewhere (for whatever reason). VS indicates the error in the first line of the chain and debugging can become quite hairy. Is that better in other language runtimes/debuggers?
- lonelappde 7y agoThe proper place to pay the dev cost is once in the library, not in all its callers.
- GordonS 7y agoIf you want to be more opaque, I guess you could do: ```c# var file = File("source"); file.CopyTo("target"); ``` (apologies if this is formatted badly; no clue what HN supports here, and I'm on mobile)
- xeeeeeeeeeeenu 7y agoThat's how Perl's Path::Tiny[1] works, for example: path("/tmp/foo.txt")->copy("/tmp/bar.txt"); [1] - https://metacpan.org/pod/Path::Tiny https://metacpan.org/pod/Path::Tiny
- kragen 7y agoAmusingly, this has the same ambiguity problem the original post was trying to solve!