我应该制作自己的框架吗?

时间:2022-10-19 22:32:44

Should I make my own framework by wrapping up the STL classes and/or Boost libraries so that if I need to change the implementation of the string, vectors, lists, etc. or I need to write functions that MFC, other other libraries or even other platforms would need to use their format, I can easily mutate them to meet the criteria. this is what I'm thinking.

我应该通过包装STL类和/或Boost库来创建自己的框架,这样如果我需要更改字符串,向量,列表等的实现,或者我需要编写MFC,其他其他库甚至函数的函数其他平台需要使用他们的格式,我可以很容易地改变它们以符合标准。这就是我的想法。

// In my framework:
namespace MyFX { 
    typedef std::string String;
};

// Port specific (MFC in this case)
CString ToCString(const MyFx::String &str) { /* magic */ }

// Port specific (.NET specific)
System::String^ ToManagedString(const MyFx::String &str) { /* magic */ }

Am I reinventing the wheel too much?

我是否过多地重新发明*?

I would use the MyFx::String in UI interfaces between the UI and the other layers.

我会在UI和其他层之间的UI界面中使用MyFx :: String。

6 个解决方案

#1


It seems to me like there won't be a lot of benefit to this; in my experience, the point of using these frameworks is so that you don't go reinventing the wheel. If you find that you need to write a new string class or a new vector class, you should think really hard about it, and make sure you're not just doing something else wrong. I'm not saying there's never a reason to write your own string class, I'm just saying it's rare. Given that, I would suggest just using the desired frameworks directly.

在我看来,这样做不会有很多好处;根据我的经验,使用这些框架的目的是让你不再重新发明*。如果你发现你需要编写一个新的字符串类或一个新的矢量类,你应该认真考虑它,并确保你不仅仅是做错了什么。我不是说从来没有理由写自己的字符串类,我只是说它很少见。鉴于此,我建议直接使用所需的框架。

Regarding the conversion functions, I believe the compiler won't see your ToCString function any differently than it would see this:

关于转换函数,我相信编译器不会看到你的ToCString函数与它看到的不同:

CString ToCString( const std::string & ) {...}

This is because a C++ typedef does not create a new type, just an alias to an existing type.

这是因为C ++ typedef不会创建新类型,只是现有类型的别名。

Further Thoughts

I think the concern you voice here is a very natural one, and I know it has come up in my team several times. However, I think the answer is still as stated above.

我认为你在这里表达的关注是非常自然的,我知道它已经在我的团队中出现了好几次。但是,我认为答案仍然如上所述。

While the STL classes are probably not perfect, they were designed by very smart people, who put quite a lot of thought into the task. Thus, the odds of you needing to write a full replacement string class are very small. Furthermore, and without intending any slight, it would take you (or me) a very long time to implement a robust general-purpose string class that could suitably replace std::string.

虽然STL课程可能并不完美,但它们是由非常聪明的人设计的,他们在这项任务中投入了大量的精力。因此,您需要编写完整替换字符串类的几率非常小。此外,并且无需任何轻微的意义,您(或我)需要很长时间来实现一个强大的通用字符串类,它可以适当地替换std :: string。

Another possible way to think about it would be this: would you consider "replacing" the String class in Java or C#? I think the answer there is clearly "no", although there may be occasional limited areas where you use something other than a String to represent a sequence of characters. Same thing goes here: std::string is as close as C++ gets to a built-in string class, and you almost assuredly don't need to replace it.

考虑它的另一种可能方式是:你会考虑用Java或C#“替换”String类吗?我认为那里的答案显然是“不”,尽管偶尔会有一些限制区域,你使用String之外的其他东西来表示一系列字符。同样的事情在这里:std :: string和C ++一样接近内置的字符串类,你几乎肯定不需要替换它。

#2


"Am I reinventing the wheel too much?" - yes. Don't do it.

“我是否过多地重新发明*?” - 是的不要这样做。

#3


Should I make my own framework?

我应该制作自己的框架吗?

Uh... no.

I wouldn't really worry about replacing std::vector until there is a business need to do so, because YAGNI.

我不会真的担心替换std :: vector,直到有业务需要这样做,因为YAGNI。

#4


"it depends" - if you think it is likely that you may in the future change to some other library/libraries, (due to porting to other platforms, for example) then make a framework to suit your needs, but make it a facade, as thin and simple as possible

“它取决于” - 如果您认为将来可能会更改到其他库/库(例如,由于移植到其他平台),那么建立一个框架以满足您的需求,但使其成为一个外观,尽可能薄而简单

this is a time/risk trade-off decision which only you can make

这是一个时间/风险权衡决定,只有你可以做出

#5


See also: https://*.com/questions/22795/when-to-notionally-build-your-own-compiler, and perhaps more interestingly Joel's article that triggered the question: In Defense of Not-Invented-Here Syndrome.

另见:https://*.com/questions/22795/when-to-notionally-build-your-own-compiler,也许更有趣的是Joel的文章引发了一个问题:捍卫未发明的 - 这里的综合症。

But the short answer is: not without a damn good reason.

但简短的回答是:不是没有一个该死的理由。

#6


The biggest benefit will be the learning experience you'll gain.

最大的好处是你将获得的学习经验。

#1


It seems to me like there won't be a lot of benefit to this; in my experience, the point of using these frameworks is so that you don't go reinventing the wheel. If you find that you need to write a new string class or a new vector class, you should think really hard about it, and make sure you're not just doing something else wrong. I'm not saying there's never a reason to write your own string class, I'm just saying it's rare. Given that, I would suggest just using the desired frameworks directly.

在我看来,这样做不会有很多好处;根据我的经验,使用这些框架的目的是让你不再重新发明*。如果你发现你需要编写一个新的字符串类或一个新的矢量类,你应该认真考虑它,并确保你不仅仅是做错了什么。我不是说从来没有理由写自己的字符串类,我只是说它很少见。鉴于此,我建议直接使用所需的框架。

Regarding the conversion functions, I believe the compiler won't see your ToCString function any differently than it would see this:

关于转换函数,我相信编译器不会看到你的ToCString函数与它看到的不同:

CString ToCString( const std::string & ) {...}

This is because a C++ typedef does not create a new type, just an alias to an existing type.

这是因为C ++ typedef不会创建新类型,只是现有类型的别名。

Further Thoughts

I think the concern you voice here is a very natural one, and I know it has come up in my team several times. However, I think the answer is still as stated above.

我认为你在这里表达的关注是非常自然的,我知道它已经在我的团队中出现了好几次。但是,我认为答案仍然如上所述。

While the STL classes are probably not perfect, they were designed by very smart people, who put quite a lot of thought into the task. Thus, the odds of you needing to write a full replacement string class are very small. Furthermore, and without intending any slight, it would take you (or me) a very long time to implement a robust general-purpose string class that could suitably replace std::string.

虽然STL课程可能并不完美,但它们是由非常聪明的人设计的,他们在这项任务中投入了大量的精力。因此,您需要编写完整替换字符串类的几率非常小。此外,并且无需任何轻微的意义,您(或我)需要很长时间来实现一个强大的通用字符串类,它可以适当地替换std :: string。

Another possible way to think about it would be this: would you consider "replacing" the String class in Java or C#? I think the answer there is clearly "no", although there may be occasional limited areas where you use something other than a String to represent a sequence of characters. Same thing goes here: std::string is as close as C++ gets to a built-in string class, and you almost assuredly don't need to replace it.

考虑它的另一种可能方式是:你会考虑用Java或C#“替换”String类吗?我认为那里的答案显然是“不”,尽管偶尔会有一些限制区域,你使用String之外的其他东西来表示一系列字符。同样的事情在这里:std :: string和C ++一样接近内置的字符串类,你几乎肯定不需要替换它。

#2


"Am I reinventing the wheel too much?" - yes. Don't do it.

“我是否过多地重新发明*?” - 是的不要这样做。

#3


Should I make my own framework?

我应该制作自己的框架吗?

Uh... no.

I wouldn't really worry about replacing std::vector until there is a business need to do so, because YAGNI.

我不会真的担心替换std :: vector,直到有业务需要这样做,因为YAGNI。

#4


"it depends" - if you think it is likely that you may in the future change to some other library/libraries, (due to porting to other platforms, for example) then make a framework to suit your needs, but make it a facade, as thin and simple as possible

“它取决于” - 如果您认为将来可能会更改到其他库/库(例如,由于移植到其他平台),那么建立一个框架以满足您的需求,但使其成为一个外观,尽可能薄而简单

this is a time/risk trade-off decision which only you can make

这是一个时间/风险权衡决定,只有你可以做出

#5


See also: https://*.com/questions/22795/when-to-notionally-build-your-own-compiler, and perhaps more interestingly Joel's article that triggered the question: In Defense of Not-Invented-Here Syndrome.

另见:https://*.com/questions/22795/when-to-notionally-build-your-own-compiler,也许更有趣的是Joel的文章引发了一个问题:捍卫未发明的 - 这里的综合症。

But the short answer is: not without a damn good reason.

但简短的回答是:不是没有一个该死的理由。

#6


The biggest benefit will be the learning experience you'll gain.

最大的好处是你将获得的学习经验。