为与路径一起使用的函数创建一个良好的接口
我有将文件路径作为输入参数的函数。该功能是跨平台的。函数支持 unicode 和常规文件路径。这个函数的最佳接口是什么,我有 2 个选择:
- 为每个函数制作两个版本
FunctionW
和FunctionA
,如WinAPI
中所示。 - 制作一个版本,它将获取
char *
作为输入参数,但该字符串必须采用UTF8
格式。
哪一个更好?
提前致谢!
I have functions which get file path as their input argument. This functions are cross platform. Functions support both unicode and regular file paths. What is the best interface for this functions, know I have 2 chooses:
- make two version of each function
FunctionW
andFunctionA
as inWinAPI
. - make one version which will get
char *
as input argument, but this string must be inUTF8
format.
Which one is better?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这实际上取决于您的其余代码以及您将如何使用它们。这里没有正确的答案 - 尝试估算您编写、使用和维护每个选项所需的时间,并尝试选择更容易的选项。
您还应该考虑
FunctionA
和FunctionW
之间的区别。如果差异不大,那么您可能可以使用它们都将调用的单个内部辅助函数,因此编写和维护第二个函数的额外时间是最少的。如果是,请考虑一下将字符串转换为您提供的第二个选项的UTF8
有多困难(如果有的话)。This really depends on the rest of your code and how you're going to use them. There is no correct answer here - try to approximate the time it will take you to write, to use and to maintain each one of the options, and try to take the one where it's easier.
You should also consider the difference between
FunctionA
andFunctionW
. If the difference isn't big, then you can likely use a single inner helper function that both of them will call, and so the extra time for writing and maintaining a second function is minimal. If it is, consider how tough it would be (if at all) to convert strings toUTF8
for the 2nd option you presented.