What is better? some advices about designing a goode API

本文探讨了良好的API设计应具备的特点,包括易于维护、易于重用、依赖性小、适应性强及行为安全可预测等。提出了API设计需遵循重构性、可扩展性和防御性编码的原则,并介绍了实现这些原则的方法。
There are some good articles that introduced the API designing. I read them and collected them here: In my definition, good code must embody qualities like the following:
  • It is easy to maintain
  • It is easy to reuse in other contexts
  • It has minimal external dependencies
  • It is adaptable to new problems
  • Its behavior is safe and predictable
This list can be further distilled into the following three categories
  • It must be refactorable
  • It must be extensible
  • It must be written defensively
Methodology of designing API
You should carry out the top down design, coz the beneifits is the following:
  • you get solid API design early on.
  • you are assured that all the components will fit together.This often makes for less reengineering than needed in the bottom-up model.
Writing code that is easy to refactor is central to having reusable and maintainable code. So how do you design code to be easily refactored? These are some of the Key:
  • Encapsulating Logic in function: A key way to increase code reusability and manageability is to compartmentlize the logic in function. The tiny bit of a logic into a function.
  • Keeping Classes and Function Simple : An individual function or method should perform a single simple task. Simple functions are then used by other functions. which is how complex tasks are completed. This methodology is preferred over writing monolithic fuctions because it promoteds reuse.
  • Namespacing : This is absolutely critical in any large code base. Using namespacing provides the following benefits: 1) It encourages descriptive naming of functions. 2) It provides a way to find the physical location of a function based on its name. 3)It helps avoid naming confilicts. You can autherticate many things: site members, administrative users. and credit cards, for instance. member_authenticate(), admin_user_authenticate(), and creditCard_authenticate() make it clear what you mean.
  • reducing coupling : Coupling occors when one function, class, or code entity depends on another to function correctly. You need to be conscious of the causality: Stable code is not nessarily highly coup0led. but highly coupled code must be stable. If you have classed that you know will be core or foundation classes. make sure you invest time in getting their APIs right early, before you have so much code referencing them that a redesign is impossible. further reading: Say no when facing coupling
  • Defensive Coding : This is the practice of eliminating assumptions in the code. especially when it comes to handing information in and out other routines. there aer tow keys to effective defensive coding in PHP: 1) Establishing coding standards to prevent accidental sytax bugs. 2) Using sanitization techniques to avoid malicious date. further reading: wiki: defensive programming
  • Establishing Standard Conventions : The easy way to make sure other developers use your code correctly is to make sure that all your dode follows standards for argument order and returens। Return values should be similarly well defined and consistent. For Boolean functions , this is simple: Return true on success and false on failure.
Design Practices
 These are some practices came form the website newbeans.org that help the writer and maintainer to achieve the general suggestions and rules.
Do not expose more than you want

Obviously the less of the implementation is expressed in the API, the more flexiblility one can have in future. there are some tricks that one can use to hide the implementation, but still deliver the desired functionality. they are the following:
  • Method is better than field: It is better to use methods(usually getters and setters) to access fields than to expose them directly. The first of reason is that a call to a method can do a lot of additional things, but in contrast an access to a filed can only read or write the value.
  • Factory is better than constructor: It is more flexible to expose a factory method than to expose constractor. the factory method is usually a static method that takes the same arguments as the constructor and return instace of the same class, but some subclass--allows to use polymorphism and possibley clean the code.
  • Make everything final : If you are writing an API and you explicitly do not want people to subclass or implement your interfaces, it is better to disallow that. simplest solution is to make your class final. other tricks inclde non-public constructors(One shall do it anyway in favor of facotory methods) or making all method private.
  • Allow access only from a friend code : It is a useful technique to not expose too much in API is to give access to certain functionality just to "friend" code.
Interface vs. abstract classes
The class are better for clent API and interfaces for service provider API, So, if your know that most of users of an API will just make calls to it. it is better to use classes (and the best thing is to make them unsubclassable, that way one prevents accidental subclasses at all). If you want people just to subclass, then choose interfaces, they are more safe and eay to used when subclassing.
【SCI复现】含可再生能源与储能的区域微电网最优运行:应对不确定性的解鲁棒性与非预见性研究(Matlab代码实现)内容概要:本文围绕含可再生能源与储能的区域微电网最优运行展开研究,重点探讨应对不确定性的解鲁棒性与非预见性策略,通过Matlab代码实现SCI论文复现。研究涵盖多阶段鲁棒调度模型、机会约束规划、需求响应机制及储能系统优化配置,结合风电、光伏等可再生能源出力的不确定性建模,提出兼顾系统经济性与鲁棒性的优化运行方案。文中详细展示了模型构建、算法设计(如C&CG算法、大M法)及仿真验证全过程,适用于微电网能量管理、电力系统优化调度等领域的科研与工程实践。; 适合人群:具备一定电力系统、优化理论和Matlab编程基础的研究生、科研人员及从事微电网、能源管理相关工作的工程技术人员。; 使用场景及目标:①复现SCI级微电网鲁棒优化研究成果,掌握应对风光负荷不确定性的建模与求解方法;②深入理解两阶段鲁棒优化、分布鲁棒优化、机会约束规划等先进优化方法在能源系统中的实际应用;③为撰写高水平学术论文或开展相关课题研究提供代码参考和技术支持。; 阅读建议:建议读者结合文档提供的Matlab代码逐模块学习,重点关注不确定性建模、鲁棒优化模型构建与求解流程,并尝试在不同场景下调试与扩展代码,以深化对微电网优化运行机制的理解。
个人防护装备实例分割数据集 一、基础信息 数据集名称:个人防护装备实例分割数据集 图片数量: 训练集:4,524张图片 分类类别: - Gloves(手套):工作人员佩戴的手部防护装备。 - Helmet(安全帽):头部防护装备。 - No-Gloves(未戴手套):未佩戴手部防护的状态。 - No-Helmet(未戴安全帽):未佩戴头部防护的状态。 - No-Shoes(未穿安全鞋):未佩戴足部防护的状态。 - No-Vest(未穿安全背心):未佩戴身体防护的状态。 - Shoes(安全鞋):足部防护装备。 - Vest(安全背心):身体防护装备。 标注格式:YOLO格式,包含实例分割的多边形坐标和类别标签,适用于实例分割任务。 数据格式:来源于实际场景图像,适用于计算机视觉模型训练。 二、适用场景 工作场所安全监控系统开发:数据集支持实例分割任务,帮助构建能够自动识别工作人员个人防护装备穿戴状态的AI模型,提升工作环境安全性。 建筑与工业安全检查:集成至监控系统,实时检测PPE穿戴情况,预防安全事故,确保合规性。 学术研究与创新:支持计算机视觉在职业安全领域的应用研究,促进AI与安全工程的结合。 培训与教育:可用于安全培训课程,演示PPE识别技术,增强员工安全意识。 三、数据集优势 精准标注与多样性:每个实例均用多边形精确标注,确保分割边界准确;覆盖多种PPE物品及未穿戴状态,增加模型鲁棒性。 场景丰富:数据来源于多样环境,提升模型在不同场景下的泛化能力。 任务适配性强:标注兼容主流深度学习框架(如YOLO),可直接用于实例分割模型开发,支持目标检测和分割任务。 实用价值高:专注于工作场所安全,为自动化的PPE检测提供可靠数据支撑,有助于减少工伤事故。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值