开发一致库的最佳实践是什么?

时间:2021-11-22 19:34:22

I am working on developing a pair of libraries to work with a REST API. Because I need to be able to use the API in very different settings I'm currently planning to have a version in PHP (for web applications) and a second version in Python (for desktop applications, and long running processes). Are there any best practices to follow in the development of the libraries to help maintain my own sanity?

我正在开发一对库来使用REST API。因为我需要能够在非常不同的设置中使用API​​,我现在计划在PHP中使用一个版本(用于Web应用程序),在Python中使用第二个版本(用于桌面应用程序和长时间运行的进程)。在图书馆的发展中是否有任何最佳实践可以帮助保持自己的理智?

6 个解决方案

#1


6  

So, the problem with developing parallel libraries in different languages is that often times different languages will have different idioms for the same task. I know this from personal experience, having ported a library from Python to PHP. Idioms aren't just naming: for example, Python has a good deal of magic you can use with getters and setters to make object properties act magical; Python has monkeypatching; Python has named parameters.

因此,在不同语言中开发并行库的问题在于,不同语言通常会对同一任务使用不同的习语。我从个人经验中了解到这一点,将一个库从Python移植到PHP。成语不仅仅是命名:例如,Python有很多魔法,你可以使用getter和setter来使对象属性变得神奇; Python有monkeypatching; Python已命名参数。

With a port, you want to pick a "base" language, and then attempt to mimic all the idioms in the other language (not easy to do); for parallel development, not doing anything too tricky and catering to the least common denominator is preferable. Then bolt on the syntax sugar.

有了端口,你想选择一种“基础”语言,然后尝试用其他语言模仿所有习语(不容易做到);对于并行开发而言,没有做任何过于棘手的事情并且迎合最小公分母是可取的。然后关注语法糖。

#2


2  

'Be your own client' : I've found that the technique of writing tests first is an excellent way of ensuring an API is easy to use. Writing tests first means you will be thinking like a 'consumer' of your API rather than just an implementor.

“做你自己的客户”:我发现首先编写测试的技术是确保API易于使用的绝佳方法。编写测试首先意味着您将被视为API的“消费者”,而不仅仅是实现者。

#3


2  

Try to write a common unit test suite for both. Maybe by wrapping a class in one language for calling it from the other. If you can't do it, at least make sure the two versions of the tests are equivalent.

尝试为两者编写一个通用的单元测试套件。也许通过用一种语言包装一个类来从另一个语言中调用它。如果你做不到,至少要确保两个版本的测试是等价的。

#4


0  

Well, the obvious one would be to keep your naming consistent. Functions and classes should be named similarly (if not identically) in both implementations. This usually happens naturally whenever you implement an API separately in two different languages. The big ticket item though (at least in my book) is to follow language-specific idioms. For example, let's assume that I were implementing a REST API in two languages I'm more familiar with: Ruby and Scala. The Ruby version might have a class MyCompany::Foo which contains method bar_baz(). Conversely, the Scala version of the same API would have a class com.mycompany.rest.Foo with a method barBaz(). It's just naming conventions, but I find it goes a long way to helping your API to feel "at home" in a particular language, even when the design was created elsewhere.

嗯,显而易见的是保持命名一致。在两个实现中,函数和类应该以相似的方式命名(如果不相同)。当您以两种不同的语言单独实现API时,这通常会自然发生。虽然这个大件物品(至少在我的书中)是遵循语言特定的习语。例如,让我们假设我用两种我更熟悉的语言实现REST API:Ruby和Scala。 Ruby版本可能有一个MyCompany :: Foo类,其中包含方法bar_baz()。相反,相同API的Scala版本将有一个类com.mycompany.rest.Foo,方法为barBaz()。它只是命名约定,但我发现它可以帮助您的API在特定语言中感受到“在家”,即使设计是在其他地方创建的。

Beyond that I have only one piece of advise: document, document, document. That's easily the best way to keep your sanity when dealing with a multi-implementation API spec.

除此之外,我只有一条建议:文档,文档,文档。在处理多实现API规范时,这是保持理智的最佳方式。

#5


0  

AFAIKT there are a lot of bridges from to scripting languages. Let's take e.g Jruby, it's Ruby + Java, then there are things to embed Ruby in Python (or the other way). Then there are examples like Etoile where the base is Objective-C but also bridges to Python and Smalltalk, another approach on wide use: Wrapping C libraries, examples are libxml2, libcurl etc etc. Maybe this could be the base. Let's say your write all for Python but do implement a bridge to PHP. So you do not have that much parrallel development.

AFAIKT有很多桥接到脚本语言。让我们来看看例如Jruby,它是Ruby + Java,然后有些东西可以用Python(或其他方式)嵌入Ruby。然后有像Etoile这样的例子,其中基础是Objective-C,但也是Python和Smalltalk的桥梁,另一种广泛使用的方法:包装C库,例子是libxml2,libcurl等等。也许这可能是基础。假设您为Python编写了全部内容,但确实实现了与PHP的桥接。所以你没有那么多平行的发展。

Or maybe it's not the worst idea to base that stuff let's say on .NET, then you suddenly have a whole bunch of languages to your disposal which in principal should be usable from every other language on the .NET platform.

或者也许最糟糕的想法是基于那些东西让我们说在.NET上,然后你突然有一大堆语言供你使用,原则上应该可以从.NET平台上的其他语言使用。

#6


0  

why not use python for web applications too? there are several frameworks available: django, web2py - similar to django but many say it's simpler to use, there is also TurboGears, web.py, Pylons

为什么不将python用于Web应用程序呢?有几个框架可用:django,web2py - 类似于django,但很多人说它使用起来更简单,还有TurboGears,web.py,Pylons

along the lines of bridging - you could use interprocess communication to have PHP and python application (in daemon mode) talk to each other.

沿着桥接线 - 你可以使用进程间通信让PHP和python应用程序(在守护进程模式下)相互通信。

#1


6  

So, the problem with developing parallel libraries in different languages is that often times different languages will have different idioms for the same task. I know this from personal experience, having ported a library from Python to PHP. Idioms aren't just naming: for example, Python has a good deal of magic you can use with getters and setters to make object properties act magical; Python has monkeypatching; Python has named parameters.

因此,在不同语言中开发并行库的问题在于,不同语言通常会对同一任务使用不同的习语。我从个人经验中了解到这一点,将一个库从Python移植到PHP。成语不仅仅是命名:例如,Python有很多魔法,你可以使用getter和setter来使对象属性变得神奇; Python有monkeypatching; Python已命名参数。

With a port, you want to pick a "base" language, and then attempt to mimic all the idioms in the other language (not easy to do); for parallel development, not doing anything too tricky and catering to the least common denominator is preferable. Then bolt on the syntax sugar.

有了端口,你想选择一种“基础”语言,然后尝试用其他语言模仿所有习语(不容易做到);对于并行开发而言,没有做任何过于棘手的事情并且迎合最小公分母是可取的。然后关注语法糖。

#2


2  

'Be your own client' : I've found that the technique of writing tests first is an excellent way of ensuring an API is easy to use. Writing tests first means you will be thinking like a 'consumer' of your API rather than just an implementor.

“做你自己的客户”:我发现首先编写测试的技术是确保API易于使用的绝佳方法。编写测试首先意味着您将被视为API的“消费者”,而不仅仅是实现者。

#3


2  

Try to write a common unit test suite for both. Maybe by wrapping a class in one language for calling it from the other. If you can't do it, at least make sure the two versions of the tests are equivalent.

尝试为两者编写一个通用的单元测试套件。也许通过用一种语言包装一个类来从另一个语言中调用它。如果你做不到,至少要确保两个版本的测试是等价的。

#4


0  

Well, the obvious one would be to keep your naming consistent. Functions and classes should be named similarly (if not identically) in both implementations. This usually happens naturally whenever you implement an API separately in two different languages. The big ticket item though (at least in my book) is to follow language-specific idioms. For example, let's assume that I were implementing a REST API in two languages I'm more familiar with: Ruby and Scala. The Ruby version might have a class MyCompany::Foo which contains method bar_baz(). Conversely, the Scala version of the same API would have a class com.mycompany.rest.Foo with a method barBaz(). It's just naming conventions, but I find it goes a long way to helping your API to feel "at home" in a particular language, even when the design was created elsewhere.

嗯,显而易见的是保持命名一致。在两个实现中,函数和类应该以相似的方式命名(如果不相同)。当您以两种不同的语言单独实现API时,这通常会自然发生。虽然这个大件物品(至少在我的书中)是遵循语言特定的习语。例如,让我们假设我用两种我更熟悉的语言实现REST API:Ruby和Scala。 Ruby版本可能有一个MyCompany :: Foo类,其中包含方法bar_baz()。相反,相同API的Scala版本将有一个类com.mycompany.rest.Foo,方法为barBaz()。它只是命名约定,但我发现它可以帮助您的API在特定语言中感受到“在家”,即使设计是在其他地方创建的。

Beyond that I have only one piece of advise: document, document, document. That's easily the best way to keep your sanity when dealing with a multi-implementation API spec.

除此之外,我只有一条建议:文档,文档,文档。在处理多实现API规范时,这是保持理智的最佳方式。

#5


0  

AFAIKT there are a lot of bridges from to scripting languages. Let's take e.g Jruby, it's Ruby + Java, then there are things to embed Ruby in Python (or the other way). Then there are examples like Etoile where the base is Objective-C but also bridges to Python and Smalltalk, another approach on wide use: Wrapping C libraries, examples are libxml2, libcurl etc etc. Maybe this could be the base. Let's say your write all for Python but do implement a bridge to PHP. So you do not have that much parrallel development.

AFAIKT有很多桥接到脚本语言。让我们来看看例如Jruby,它是Ruby + Java,然后有些东西可以用Python(或其他方式)嵌入Ruby。然后有像Etoile这样的例子,其中基础是Objective-C,但也是Python和Smalltalk的桥梁,另一种广泛使用的方法:包装C库,例子是libxml2,libcurl等等。也许这可能是基础。假设您为Python编写了全部内容,但确实实现了与PHP的桥接。所以你没有那么多平行的发展。

Or maybe it's not the worst idea to base that stuff let's say on .NET, then you suddenly have a whole bunch of languages to your disposal which in principal should be usable from every other language on the .NET platform.

或者也许最糟糕的想法是基于那些东西让我们说在.NET上,然后你突然有一大堆语言供你使用,原则上应该可以从.NET平台上的其他语言使用。

#6


0  

why not use python for web applications too? there are several frameworks available: django, web2py - similar to django but many say it's simpler to use, there is also TurboGears, web.py, Pylons

为什么不将python用于Web应用程序呢?有几个框架可用:django,web2py - 类似于django,但很多人说它使用起来更简单,还有TurboGears,web.py,Pylons

along the lines of bridging - you could use interprocess communication to have PHP and python application (in daemon mode) talk to each other.

沿着桥接线 - 你可以使用进程间通信让PHP和python应用程序(在守护进程模式下)相互通信。