{"id":2524,"date":"2018-01-23T01:00:13","date_gmt":"2018-01-23T01:00:13","guid":{"rendered":"https:\/\/www.fluentcpp.com\/?p=2524"},"modified":"2018-01-19T11:32:44","modified_gmt":"2018-01-19T11:32:44","slug":"function-return","status":"publish","type":"post","link":"https:\/\/www.fluentcpp.com\/2018\/01\/23\/function-return\/","title":{"rendered":"How to Be Clear About What Your Functions Return"},"content":{"rendered":"<p><span style=\"color: #000000;\">What&#8217;s in a function&#8217;s interface?<\/span><\/p>\n<p>In most languages, a function&#8217;s interface has 3 main parts:<\/p>\n<ul>\n<li>the function&#8217;s name: it indicates what the function does,<\/li>\n<li>the function&#8217;s parameters: they show what the function takes as input to do its job,<\/li>\n<li>the function&#8217;s return type: it indicates the output of the function.<\/li>\n<\/ul>\n<pre class=\"lang:c++ decode:true\">ReturnType functionName(ParameterType1 parameterName1, ParameterType2 parameterName2);<\/pre>\n<p>So far, so good.<\/p>\n<p>But when looking at this prototype, we can notice that something isn&#8217;t symmetric: the function&#8217;s parameters have both a type and a name, while the returned value only has a type. Indeed, <strong>the return value doesn&#8217;t have a name<\/strong>.<\/p>\n<p>In a function declaration, one could choose to also omit the names of the parameters. But still, the return type doesn&#8217;t have a choice. It can only be&#8230; a type.<\/p>\n<p>Why is that? My take is that it&#8217;s because we expect the the function&#8217;s name to be clear enough to express what it returns, plus the returned value has a visible type. So a name for the returned value itself would be superfluous.<\/p>\n<p>But is this the case 100% of the time?<\/p>\n<h3><span style=\"color: #ff6600;\">A use case that should not exist, but that does<\/span><\/h3>\n<p>No. In theory it works fine but, realistically, it&#8217;s not always the case that a function&#8217;s name informs you exactly of what to expect as a return value.<\/p>\n<p>Let&#8217;s take the example of a function that performs a side effect, like saving a piece of information in a database:<\/p>\n<pre class=\"lang:c++ decode:true\">void save(PieceOfData const&amp; preciousData);<\/pre>\n<p>And say that this operation could potentially fail. How does the function lets it caller know whether or not the operation succeeded?<\/p>\n<p>One way to go about that is to make the <code>save<\/code>\u00a0function throw an exception. It works, but not everyone uses exceptions (exceptions need exception-safe code surrounding them, they may impact performance, some teams ban them from their coding conventions&#8230;). There have been <a href=\"https:\/\/blog.tartanllama.xyz\/optional-expected\/\" target=\"_blank\" rel=\"noopener\">hot debates and suggested alternatives<\/a> about this.<\/p>\n<p>We already come across a clear way to indicate that a function could potentially fail to return its result: <a href=\"https:\/\/www.fluentcpp.com\/2016\/11\/24\/clearer-interfaces-with-optionalt\/\">using optionals<\/a>. That is to say, return an <code>optional&lt;T&gt;<\/code>, conveying the message that we expect to return a <code>T<\/code>, but this could potentially fail, and the function caller is supposed to check whether that returned\u00a0<code>optional<\/code>\u00a0is full or empty.<\/p>\n<p>But here we&#8217;re talking about a function that returns <strong>nothing<\/strong>. It merely saves \u00a0piece of data in a database. Should it return an\u00a0<code>optional&lt;void&gt;<\/code>\u00a0then? This would read that it is supposed to return <code>void<\/code>\u00a0but it may return something that isn&#8217;t really a <code>void<\/code>, but an empty box instead. An empty void. Weird. And <code>std::optional&lt;void&gt;<\/code>\u00a0doesn&#8217;t compile anyway!<\/p>\n<p>Another possibility is to return a boolean indicating whether or not the function succeeded:<\/p>\n<pre class=\"lang:c++ decode:true\">bool save(PieceOfData const&amp; preciousData);<\/pre>\n<p>But this is less than ideal. First, the returned value could be ignored at call site. Though this could be prevented by adding the \u00a0<code>[[nodiscard]]<\/code>\u00a0attribute in C++17:<\/p>\n<pre class=\"lang:c++ decode:true\">[[nodiscard]] bool save(PieceOfData const&amp; preciousData);<\/pre>\n<p>Second,\u00a0just by looking at the function&#8217;s prototype, we don&#8217;t know if that <code>bool<\/code>\u00a0means success or failure. Or something else totally unrelated, for that matter. We could look it up in the documentation of the function, but it takes more time and introduces a risk of getting it wrong anyway.<\/p>\n<p>Since the function is only called &#8220;<code>save<\/code>&#8220;, its name doesn&#8217;t say what the return type represents. We could call it something like <code>saveAndReturnsIfSuceeded<\/code>\u00a0but&#8230; we don&#8217;t really want to see that sort of name in code, do we?<\/p>\n<h4><span style=\"color: #ff6600;\">Meta information<\/span><\/h4>\n<p>It is interesting to realize that this is a more general use case that just failure or success. Indeed, sometimes the only way to retrieve a piece of information about a certain operation is to actually perform it.<\/p>\n<p>For instance, say we have a function that takes an <code>Input<\/code> and uses it to add and to remove entries from a existing <code>Entries<\/code> collection:<\/p>\n<pre class=\"lang:c++ decode:true\">void updateEntries(Input const&amp; input, Entries&amp; entries);<\/pre>\n<p>And we&#8217;d like to retrieve some data about this operation. Say an <code>int<\/code>\u00a0that represents the number of entries removed, for example. We could make the function output that <code>int<\/code>\u00a0via its return type:<\/p>\n<pre class=\"lang:c++ decode:true\">int updateEntries(Input const&amp; input, Entries&amp; entries);<\/pre>\n<p>But the return type doesn&#8217;t tell what it represents here, only that it&#8217;s implemented as an <code>int<\/code>. We&#8217;ve lost information here.<\/p>\n<p>In this particular case, we could have added an <code>int&amp; entriesRemoved<\/code>\u00a0function parameter, but I don&#8217;t like this pattern because it forces the caller to initialize a variable before calling the functions, which doesn&#8217;t work for all types, and <a href=\"https:\/\/www.fluentcpp.com\/2017\/12\/08\/make-functions-functional-video\/\" target=\"_blank\" rel=\"noopener\">a non-const reference means input-output<\/a>\u00a0and not output, so it&#8217;s not exactly the message we&#8217;d like to convey here.<\/p>\n<p>What to do then?<\/p>\n<h3><span style=\"color: #ff6600;\">Named return types: strong return types?<\/span><\/h3>\n<p>So in summary, we have return types that lack a meaningful name. This sounds like a job for <a href=\"https:\/\/www.fluentcpp.com\/2016\/12\/08\/strong-types-for-strong-interfaces\/\">strong types<\/a>: indeed, strong types help put meaningful names over types!<\/p>\n<p><em>Spoiler alert: strong types won&#8217;t be the option that we&#8217;ll retain for most cases of return types in the end. Read on to see why and what to use instead.<\/em><\/p>\n<p>Let&#8217;s use <code><a href=\"https:\/\/github.com\/joboccara\/NamedType\" target=\"_blank\" rel=\"noopener\">NamedType<\/a><\/code>\u00a0as an implementation of strong types, and create return types with a name that make sense in each of our functions&#8217; contexts.<\/p>\n<p>So our <code>save<\/code>\u00a0function returns a <code>bool<\/code>\u00a0that is <code>true<\/code>\u00a0if the operation was a success. Let&#8217;s stick a name over that <code>bool<\/code>:<\/p>\n<pre class=\"lang:c++ decode:true\">using HasSucceeded = NamedType&lt;bool, struct HasSucceededTag&gt;;<\/pre>\n<p>The second parameter of <code>NamedType<\/code>\u00a0is a &#8220;phantom type&#8221;, that is to say that it&#8217;s there only for differentiating <code>HasSucceeded<\/code>\u00a0from another <code>NamedType<\/code> over a <code>bool<\/code>.<\/p>\n<p>Let&#8217;s use <code>HasSucceeded<\/code>\u00a0in our function&#8217;s interface:<\/p>\n<pre class=\"lang:c++ decode:true\">HasSucceeded save(PieceOfData const&amp; preciousData);<\/pre>\n<p>The function now expresses that it returns the information about whether the operation succeeded or not.<\/p>\n<p>The implementation of the function would build a <code>HasSucceeded<\/code>\u00a0and return it:<\/p>\n<pre class=\"lang:c++ decode:true \">HasSucceeded save(PieceOfData const&amp; preciousData)\r\n{\r\n    \/\/ attempt to save...\r\n    \/\/ if it failed\r\n    return HasSucceeded(false);\r\n    \/\/ else, if all goes well\r\n    return HasSucceeded(true);\r\n}<\/pre>\n<p>And at call site:<\/p>\n<pre class=\"lang:c++ decode:true\">HasSucceeded hasSucceeded = save(myData); \/\/ or auto hasSucceeded = ...\r\n\r\nif(!hasSucceeded.get())\r\n{\r\n    \/\/ deal with failure...<\/pre>\n<p>Note that we can choose to get rid of the call to\u00a0<code>.get()<\/code>\u00a0by making <code>HasSucceeded<\/code>\u00a0use the <a href=\"https:\/\/www.fluentcpp.com\/2017\/11\/07\/calling-functions-methods-strong-types\/\"><code>FunctionCallable<\/code><\/a>\u00a0skill.<\/p>\n<p>For the sake of the example, let&#8217;s apply the same technique to our <code>updateEntries<\/code>\u00a0function:<\/p>\n<pre class=\"lang:c++ decode:true \">using NumberOfEntriesRemoved = NamedType&lt;int, struct NumberOfEntriesRemovedTag&gt;;\r\n\r\nNumberOfEntriesRemoved updateEntries(Input const&amp; input, Entries&amp; entries);<\/pre>\n<p>By looking at the interface, we now know that it outputs the number of entries removed via the return type.<\/p>\n<h3><span style=\"color: #ff6600;\">Just a weak type will do here<\/span><\/h3>\n<p>The above works, but it&#8217;s needlessly sophisticated. In this case, the only thing we need is a name for other human beings to understand the interface. We don&#8217;t need to create a specific type used only in the context of the return type to also let the compiler know what we mean by it.<\/p>\n<p>Why is that? Contrast our example with the case of input parameters of a function:<\/p>\n<pre class=\"lang:c++ decode:true\">void setPosition(int row, int column);\r\n\r\n\/\/ Call site\r\nsetPosition(36, 42);<\/pre>\n<p>Since there are several parameters that could be mixed up (and the program would still compile), introducing strong types such as <code>Row<\/code>\u00a0and <code>Column<\/code>\u00a0are useful to make sure we pass the parameters in the right order:<\/p>\n<pre class=\"lang:c++ decode:true \">void setPosition(Row row, Column column);\r\n\r\n\/\/ Call site:\r\nsetPosition(Row(36), Column(42));<\/pre>\n<p>But in the return type, what is there to mix up? There is only one value returned anyway!<\/p>\n<p>So a simple alias does the job just well:<\/p>\n<pre class=\"lang:c++ decode:true \">using HasSucceeded = bool;\r\nHasSucceeded save(PieceOfData const&amp; preciousData);<\/pre>\n<p>This is the <strong>most adapted solution<\/strong> in this case, in my opinion.<\/p>\n<h4><span style=\"color: #ff6600;\">The case where strong types <em>are<\/em> useful in return types<\/span><\/h4>\n<p>However there are at least two specific cases where strong types are helpful to clarify a returned value.<\/p>\n<p>One is to\u00a0<a href=\"https:\/\/www.fluentcpp.com\/2017\/11\/10\/strong-types-multiple-return-values\/\">use strong types to return multiple values<\/a>.<\/p>\n<p>The other is when you already have a strong type that represents the return value, and that you <strong>already use<\/strong> at other places in the codeline. For instance, if you have a strong type <code>SerialNumber<\/code>\u00a0that strengthen a <code>std::string<\/code>, and you use it at various places, it makes perfect sense to return it from a function.<\/p>\n<p>The point I want to make is not to create a strong type for the sole purpose of returning it from a function and immediately retrieving the value inside it afterwards. Indeed, in this case <strong>a classical alias will do<\/strong>.<\/p>\n<h3><span style=\"color: #ff6600;\">What&#8217;s in a <em>expressive<\/em> function&#8217;s interface?<\/span><\/h3>\n<p>This technique helps us be more explicit about what it is that a function is returning.<\/p>\n<p>This is part of a more general objective, which is to leverage on every element of the function to express useful information:<\/p>\n<ul>\n<li>a clear function name: by using <a href=\"https:\/\/www.fluentcpp.com\/2017\/01\/30\/how-to-choose-good-names\/\">good naming<\/a>,<\/li>\n<li>well-designed function parameters (a 3-post series coming soon),<\/li>\n<li>an explicit output: either by returning the output directly (thus <a href=\"https:\/\/www.fluentcpp.com\/2016\/11\/22\/make-your-functions-functional\/\">making functions functional<\/a>), or by\u00a0<a href=\"https:\/\/www.fluentcpp.com\/2016\/11\/24\/clearer-interfaces-with-optionalt\/\">using an optional<\/a>\u00a0or, if it comes to that, returning something else, like we saw today. But always, by being the clearest possible about it.<\/li>\n<\/ul>\n<p>You may also like:<\/p>\n<ul>\n<li><a href=\"https:\/\/www.fluentcpp.com\/2016\/12\/08\/strong-types-for-strong-interfaces\/\">Strong types for strong interfaces<\/a><\/li>\n<li><a href=\"https:\/\/www.fluentcpp.com\/2016\/11\/22\/make-your-functions-functional\/\">Make your functions functional<\/a><\/li>\n<li>The right question for the right name<\/li>\n<li><a href=\"https:\/\/www.fluentcpp.com\/2017\/01\/30\/how-to-choose-good-names\/\">How to choose good names in code<\/a><\/li>\n<\/ul>\nDon't want to miss out ? <strong>Follow:<\/strong> &nbsp&nbsp<a class=\"synved-social-button synved-social-button-follow synved-social-size-48 synved-social-resolution-single synved-social-provider-twitter nolightbox\" data-provider=\"twitter\" target=\"_blank\" rel=\"nofollow\" title=\"Follow me on twitter\" href=\"https:\/\/twitter.com\/joboccara\" style=\"font-size: 0px;width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" alt=\"twitter\" title=\"Follow me on twitter\" class=\"synved-share-image synved-social-image synved-social-image-follow\" width=\"48\" height=\"48\" style=\"display: inline;width:48px;height:48px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/i0.wp.com\/www.fluentcpp.com\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/96x96\/twitter.png?resize=48%2C48&#038;ssl=1\" \/><\/a><a class=\"synved-social-button synved-social-button-follow synved-social-size-48 synved-social-resolution-single synved-social-provider-linkedin nolightbox\" data-provider=\"linkedin\" target=\"_blank\" rel=\"nofollow\" title=\"Find us on Linkedin\" href=\"https:\/\/www.linkedin.com\/in\/jonathan-boccara-23826921\/\" style=\"font-size: 0px;width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" alt=\"linkedin\" title=\"Find us on Linkedin\" class=\"synved-share-image synved-social-image synved-social-image-follow\" width=\"48\" height=\"48\" style=\"display: inline;width:48px;height:48px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/i0.wp.com\/www.fluentcpp.com\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/96x96\/linkedin.png?resize=48%2C48&#038;ssl=1\" \/><\/a><a class=\"synved-social-button synved-social-button-follow synved-social-size-48 synved-social-resolution-single synved-social-provider-rss nolightbox\" data-provider=\"rss\" target=\"_blank\" rel=\"nofollow\" title=\"Subscribe to our RSS Feed\" href=\"https:\/\/www.fluentcpp.com\/feed\/\" style=\"font-size: 0px;width:48px;height:48px;margin:0;margin-bottom:5px\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" alt=\"rss\" title=\"Subscribe to our RSS Feed\" class=\"synved-share-image synved-social-image synved-social-image-follow\" width=\"48\" height=\"48\" style=\"display: inline;width:48px;height:48px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/i0.wp.com\/www.fluentcpp.com\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/96x96\/rss.png?resize=48%2C48&#038;ssl=1\" \/><\/a><br\/>Share this post!<a class=\"synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-facebook nolightbox\" data-provider=\"facebook\" target=\"_blank\" rel=\"nofollow\" title=\"Check out this post from Fluent C++\" href=\"https:\/\/www.facebook.com\/sharer.php?u=https%3A%2F%2Fwww.fluentcpp.com%2Fwp-json%2Fwp%2Fv2%2Fposts%2F2524&#038;t=How%20to%20Be%20Clear%20About%20What%20Your%20Functions%20Return&#038;s=100&#038;p&#091;url&#093;=https%3A%2F%2Fwww.fluentcpp.com%2Fwp-json%2Fwp%2Fv2%2Fposts%2F2524&#038;p&#091;images&#093;&#091;0&#093;=&#038;p&#091;title&#093;=How%20to%20Be%20Clear%20About%20What%20Your%20Functions%20Return\" style=\"font-size: 0px;width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" alt=\"Facebook\" title=\"Check out this post from Fluent C++\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"48\" height=\"48\" style=\"display: inline;width:48px;height:48px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/i0.wp.com\/www.fluentcpp.com\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/96x96\/facebook.png?resize=48%2C48&#038;ssl=1\" \/><\/a><a class=\"synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-twitter nolightbox\" data-provider=\"twitter\" target=\"_blank\" rel=\"nofollow\" title=\"Tweet about this\" href=\"https:\/\/twitter.com\/intent\/tweet?url=https%3A%2F%2Fwww.fluentcpp.com%2Fwp-json%2Fwp%2Fv2%2Fposts%2F2524&#038;text=Check%20out%20this%20post%20from%20Fluent%20C%2B%2B\" style=\"font-size: 0px;width:48px;height:48px;margin:0;margin-bottom:5px;margin-right:5px\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" alt=\"twitter\" title=\"Tweet about this\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"48\" height=\"48\" style=\"display: inline;width:48px;height:48px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/i0.wp.com\/www.fluentcpp.com\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/96x96\/twitter.png?resize=48%2C48&#038;ssl=1\" \/><\/a><a class=\"synved-social-button synved-social-button-share synved-social-size-48 synved-social-resolution-single synved-social-provider-linkedin nolightbox\" data-provider=\"linkedin\" target=\"_blank\" rel=\"nofollow\" title=\"Share on Linkedin\" href=\"https:\/\/www.linkedin.com\/shareArticle?mini=true&#038;url=https%3A%2F%2Fwww.fluentcpp.com%2Fwp-json%2Fwp%2Fv2%2Fposts%2F2524&#038;title=How%20to%20Be%20Clear%20About%20What%20Your%20Functions%20Return\" style=\"font-size: 0px;width:48px;height:48px;margin:0;margin-bottom:5px\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" alt=\"linkedin\" title=\"Share on Linkedin\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"48\" height=\"48\" style=\"display: inline;width:48px;height:48px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/i0.wp.com\/www.fluentcpp.com\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/96x96\/linkedin.png?resize=48%2C48&#038;ssl=1\" \/><\/a>","protected":false},"excerpt":{"rendered":"<p>What&#8217;s in a function&#8217;s interface? In most languages, a function&#8217;s interface has 3 main parts: the function&#8217;s name: it indicates what the function does, the function&#8217;s parameters: they show what the function takes as input to do its job, the function&#8217;s return type: it indicates the output of the function. ReturnType functionName(ParameterType1 parameterName1, ParameterType2 parameterName2); [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[84],"tags":[10,16,335,18,43,334,21],"class_list":["post-2524","post","type-post","status-publish","format-standard","hentry","category-strong-types","tag-c","tag-expressive","tag-fail","tag-fluent","tag-interface","tag-return","tag-strong-types"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to Be Clear About What Your Functions Return - Fluent C++<\/title>\n<meta name=\"description\" content=\"Sometimes what a function returns isn&#039;t clear. Here is how strong types can help you be very explicit about your returned values, and thus avoid errors.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.fluentcpp.com\/2018\/01\/23\/function-return\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Be Clear About What Your Functions Return - Fluent C++\" \/>\n<meta property=\"og:description\" content=\"Sometimes what a function returns isn&#039;t clear. Here is how strong types can help you be very explicit about your returned values, and thus avoid errors.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.fluentcpp.com\/2018\/01\/23\/function-return\/\" \/>\n<meta property=\"og:site_name\" content=\"Fluent C++\" \/>\n<meta property=\"article:published_time\" content=\"2018-01-23T01:00:13+00:00\" \/>\n<meta name=\"author\" content=\"Jonathan Boccara\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@joboccara\" \/>\n<meta name=\"twitter:site\" content=\"@JoBoccara\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Jonathan Boccara\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.fluentcpp.com\\\/2018\\\/01\\\/23\\\/function-return\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.fluentcpp.com\\\/2018\\\/01\\\/23\\\/function-return\\\/\"},\"author\":{\"name\":\"Jonathan Boccara\",\"@id\":\"https:\\\/\\\/www.fluentcpp.com\\\/#\\\/schema\\\/person\\\/bc2586443e40d356676d1b1740521237\"},\"headline\":\"How to Be Clear About What Your Functions Return\",\"datePublished\":\"2018-01-23T01:00:13+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.fluentcpp.com\\\/2018\\\/01\\\/23\\\/function-return\\\/\"},\"wordCount\":1394,\"commentCount\":9,\"keywords\":[\"C++\",\"expressive\",\"fail\",\"fluent\",\"interface\",\"return\",\"strong types\"],\"articleSection\":[\"Strong types\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.fluentcpp.com\\\/2018\\\/01\\\/23\\\/function-return\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.fluentcpp.com\\\/2018\\\/01\\\/23\\\/function-return\\\/\",\"url\":\"https:\\\/\\\/www.fluentcpp.com\\\/2018\\\/01\\\/23\\\/function-return\\\/\",\"name\":\"How to Be Clear About What Your Functions Return - Fluent C++\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.fluentcpp.com\\\/#website\"},\"datePublished\":\"2018-01-23T01:00:13+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.fluentcpp.com\\\/#\\\/schema\\\/person\\\/bc2586443e40d356676d1b1740521237\"},\"description\":\"Sometimes what a function returns isn't clear. Here is how strong types can help you be very explicit about your returned values, and thus avoid errors.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.fluentcpp.com\\\/2018\\\/01\\\/23\\\/function-return\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.fluentcpp.com\\\/2018\\\/01\\\/23\\\/function-return\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.fluentcpp.com\\\/2018\\\/01\\\/23\\\/function-return\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.fluentcpp.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Be Clear About What Your Functions Return\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.fluentcpp.com\\\/#website\",\"url\":\"https:\\\/\\\/www.fluentcpp.com\\\/\",\"name\":\"Fluent C++\",\"description\":\"Jonathan Boccara&#039;s blog\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.fluentcpp.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.fluentcpp.com\\\/#\\\/schema\\\/person\\\/bc2586443e40d356676d1b1740521237\",\"name\":\"Jonathan Boccara\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/cf2136ea15306047a4aa549b5400deb87e750440abe9f5fab5ea5d8b47349d27?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/cf2136ea15306047a4aa549b5400deb87e750440abe9f5fab5ea5d8b47349d27?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/cf2136ea15306047a4aa549b5400deb87e750440abe9f5fab5ea5d8b47349d27?s=96&d=mm&r=g\",\"caption\":\"Jonathan Boccara\"},\"description\":\"Hello, my name is Jonathan Boccara, I'm your host on Fluent C++. I have been a developer for 14 years. My focus is on how to write expressive code. I wrote the book The Legacy Code Programmer's Toolbox. I'm happy to take your feedback, don't hesitate to drop a comment on a post, follow me or get in touch directly !\",\"sameAs\":[\"https:\\\/\\\/www.linkedin.com\\\/in\\\/jonathan-boccara-23826921\\\/\",\"https:\\\/\\\/x.com\\\/joboccara\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Be Clear About What Your Functions Return - Fluent C++","description":"Sometimes what a function returns isn't clear. Here is how strong types can help you be very explicit about your returned values, and thus avoid errors.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.fluentcpp.com\/2018\/01\/23\/function-return\/","og_locale":"en_US","og_type":"article","og_title":"How to Be Clear About What Your Functions Return - Fluent C++","og_description":"Sometimes what a function returns isn't clear. Here is how strong types can help you be very explicit about your returned values, and thus avoid errors.","og_url":"https:\/\/www.fluentcpp.com\/2018\/01\/23\/function-return\/","og_site_name":"Fluent C++","article_published_time":"2018-01-23T01:00:13+00:00","author":"Jonathan Boccara","twitter_card":"summary_large_image","twitter_creator":"@joboccara","twitter_site":"@JoBoccara","twitter_misc":{"Written by":"Jonathan Boccara","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.fluentcpp.com\/2018\/01\/23\/function-return\/#article","isPartOf":{"@id":"https:\/\/www.fluentcpp.com\/2018\/01\/23\/function-return\/"},"author":{"name":"Jonathan Boccara","@id":"https:\/\/www.fluentcpp.com\/#\/schema\/person\/bc2586443e40d356676d1b1740521237"},"headline":"How to Be Clear About What Your Functions Return","datePublished":"2018-01-23T01:00:13+00:00","mainEntityOfPage":{"@id":"https:\/\/www.fluentcpp.com\/2018\/01\/23\/function-return\/"},"wordCount":1394,"commentCount":9,"keywords":["C++","expressive","fail","fluent","interface","return","strong types"],"articleSection":["Strong types"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.fluentcpp.com\/2018\/01\/23\/function-return\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.fluentcpp.com\/2018\/01\/23\/function-return\/","url":"https:\/\/www.fluentcpp.com\/2018\/01\/23\/function-return\/","name":"How to Be Clear About What Your Functions Return - Fluent C++","isPartOf":{"@id":"https:\/\/www.fluentcpp.com\/#website"},"datePublished":"2018-01-23T01:00:13+00:00","author":{"@id":"https:\/\/www.fluentcpp.com\/#\/schema\/person\/bc2586443e40d356676d1b1740521237"},"description":"Sometimes what a function returns isn't clear. Here is how strong types can help you be very explicit about your returned values, and thus avoid errors.","breadcrumb":{"@id":"https:\/\/www.fluentcpp.com\/2018\/01\/23\/function-return\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.fluentcpp.com\/2018\/01\/23\/function-return\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.fluentcpp.com\/2018\/01\/23\/function-return\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.fluentcpp.com\/"},{"@type":"ListItem","position":2,"name":"How to Be Clear About What Your Functions Return"}]},{"@type":"WebSite","@id":"https:\/\/www.fluentcpp.com\/#website","url":"https:\/\/www.fluentcpp.com\/","name":"Fluent C++","description":"Jonathan Boccara&#039;s blog","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.fluentcpp.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.fluentcpp.com\/#\/schema\/person\/bc2586443e40d356676d1b1740521237","name":"Jonathan Boccara","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/cf2136ea15306047a4aa549b5400deb87e750440abe9f5fab5ea5d8b47349d27?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/cf2136ea15306047a4aa549b5400deb87e750440abe9f5fab5ea5d8b47349d27?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/cf2136ea15306047a4aa549b5400deb87e750440abe9f5fab5ea5d8b47349d27?s=96&d=mm&r=g","caption":"Jonathan Boccara"},"description":"Hello, my name is Jonathan Boccara, I'm your host on Fluent C++. I have been a developer for 14 years. My focus is on how to write expressive code. I wrote the book The Legacy Code Programmer's Toolbox. I'm happy to take your feedback, don't hesitate to drop a comment on a post, follow me or get in touch directly !","sameAs":["https:\/\/www.linkedin.com\/in\/jonathan-boccara-23826921\/","https:\/\/x.com\/joboccara"]}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.fluentcpp.com\/wp-json\/wp\/v2\/posts\/2524","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.fluentcpp.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.fluentcpp.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.fluentcpp.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.fluentcpp.com\/wp-json\/wp\/v2\/comments?post=2524"}],"version-history":[{"count":14,"href":"https:\/\/www.fluentcpp.com\/wp-json\/wp\/v2\/posts\/2524\/revisions"}],"predecessor-version":[{"id":2808,"href":"https:\/\/www.fluentcpp.com\/wp-json\/wp\/v2\/posts\/2524\/revisions\/2808"}],"wp:attachment":[{"href":"https:\/\/www.fluentcpp.com\/wp-json\/wp\/v2\/media?parent=2524"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fluentcpp.com\/wp-json\/wp\/v2\/categories?post=2524"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fluentcpp.com\/wp-json\/wp\/v2\/tags?post=2524"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}