<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>Api Design on Vasileios Chroniadis blog</title><link>https://chronvas.github.io/tags/api-design/</link><description>Recent content in Api Design on Vasileios Chroniadis blog</description><generator>Hugo</generator><language>en</language><lastBuildDate>Mon, 13 Nov 2023 00:00:00 +0000</lastBuildDate><atom:link href="https://chronvas.github.io/tags/api-design/index.xml" rel="self" type="application/rss+xml"/><item><title>A simple software principle</title><link>https://chronvas.github.io/posts/2023-11-13-a-simple-software-principle/</link><pubDate>Mon, 13 Nov 2023 00:00:00 +0000</pubDate><guid>https://chronvas.github.io/posts/2023-11-13-a-simple-software-principle/</guid><description>&lt;p&gt;&lt;em&gt;Prefer language tools, if they exist, rather than relying on assumptions.&lt;/em&gt;&lt;/p&gt;
&lt;figure&gt;
&lt;a href="https://chronvas.github.io/img/workshop.png"&gt;&lt;img src="https://chronvas.github.io/img/workshop.png"&gt;&lt;/a
&lt;/figure&gt;
&lt;p&gt;Assume you are working on a project that consumes a third-party SDK that provides you with a simple list of fruits. Your application’s task is to take these fruits and make juice with them. You name the juice that you make with the name of the 1st fruit you get from this list.&lt;/p&gt;
&lt;p&gt;Your application looks like this:&lt;/p&gt;
&lt;pre tabindex="0"&gt;&lt;code&gt;import fruitery.SDK
import fruitery.SDK.Fruit
fun main(args: Array&amp;lt;String&amp;gt;) {
val sdk = SDK()
val fruits = sdk.getFruits()
val juice = makeJuice(fruits)
}
private fun makeJuice(fruits: List&amp;lt;Fruit&amp;gt;) =
Juice(
name = fruits.first().name,
content = squash(fruits)
)
}
private data class Juice(val name: String, val content: String)
private fun squash(fruits: List&amp;lt;Fruit&amp;gt;) = /* squash the fruits */
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Do you see a problem with that? If not, look closer. More specifically “fruits.first()”. Kotlin docs state clearly that it can throw, but there is no compilation error. It is therefore up to you to decide if you build your app to defend against this possibility or not. Some other languages don’t tell you that.&lt;/p&gt;</description></item></channel></rss>