{"id":1093,"date":"2022-08-30T15:12:16","date_gmt":"2022-08-30T15:12:16","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/what-is-type-safe-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:12:16","modified_gmt":"2022-08-30T15:12:16","slug":"what-is-type-safe-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/what-is-type-safe-collection-of-common-programming-errors\/","title":{"rendered":"What is Type-safe?-Collection of common programming errors"},"content":{"rendered":"<p>Type safety means that the compiler will validate types while compiling, and throw an error if you try to assign the wrong type to a variable.<\/p>\n<p>Some simple examples:<\/p>\n<pre><code>\/\/ Fails, Trying to put an integer in a string\nString one = 1;\n\/\/ Also fails.\nint foo = \"bar\";\n<\/code><\/pre>\n<p>This also applies to method arguments, since you are passing explicit types to them:<\/p>\n<pre><code>int AddTwoNumbers(int a, int b)\n{\n    return a + b;\n}\n<\/code><\/pre>\n<p>If I tried to call that using:<\/p>\n<pre><code>int Sum = AddTwoNumbers(5, \"5\");\n<\/code><\/pre>\n<p>The compiler would throw an error, because I am passing a string (&#8220;5&#8221;), and it is expecting an integer.<\/p>\n<p>In a loosely typed language, such as javascript, I can do the following:<\/p>\n<pre><code>function AddTwoNumbers(a, b)\n{\n    return a + b;\n}\n<\/code><\/pre>\n<p>if I call it like this:<\/p>\n<pre><code>Sum = AddTwoNumbers(5, \"5\");\n<\/code><\/pre>\n<p>Javascript automaticly converts the 5 to a string, and returns &#8220;55&#8221;. This is due to javascript using the + sign for string concatenation. To make it type-aware, you would need to do something like:<\/p>\n<pre><code>function AddTwoNumbers(a, b)\n{\n    return Number(a) + Number(b);\n}\n<\/code><\/pre>\n<p>Or, possibly:<\/p>\n<pre><code>function AddOnlyTwoNumbers(a, b)\n{\n    if (isNaN(a) || isNaN(b))\n        return false;\n    return Number(a) + Number(b);\n}\n<\/code><\/pre>\n<p>if I call it like this:<\/p>\n<pre><code>Sum = AddTwoNumbers(5, \" dogs\");\n<\/code><\/pre>\n<p>Javascript automatically converts the 5 to a string, and appends them, to return &#8220;5 dogs&#8221;.<\/p>\n<p>Not all dynamic languages are as forgiving as javascript (In fact a dynamic language does not implicity imply a loose typed language (see Python)), some of them will actually give you a runtime error on invalid type casting.<\/p>\n<p>While its convenient, it opens you up to a lot of errors that can be easily missed, and only identified by testing the running program. Personally, I prefer to have my compiler tell me if I made that mistake.<\/p>\n<p>Now, back to C#&#8230;<\/p>\n<p>C# supports a language feature called covariance, this basically means that you can substitute a base type for a child type and not cause an error, for example:<\/p>\n<pre><code> public class Foo : Bar\n {\n }\n<\/code><\/pre>\n<p>Here, I created a new class (Foo) that subclasses Bar. I can now create a method:<\/p>\n<pre><code> void DoSomething(Bar myBar)\n<\/code><\/pre>\n<p>And call it using either a Foo, or a Bar as an argument, both will work without causing an error. This works because C# knows that any child class of Bar will implement the interface of Bar.<\/p>\n<p>However, you cannot do the inverse:<\/p>\n<pre><code>void DoSomething(Foo myFoo)\n<\/code><\/pre>\n<p>In this situation, I cannot pass Bar to this method, because the compiler does not know that Bar implements Foo&#8217;s interface. This is because a child class can (and usually will) be much different than the parent class.<\/p>\n<p>Of course, now I&#8217;ve gone way off the deep end and beyond the scope of the original question, but its all good stuff to know \ud83d\ude42<\/p>\n<p id=\"rop\"><small>Originally posted 2013-11-09 23:23:02. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>Type safety means that the compiler will validate types while compiling, and throw an error if you try to assign the wrong type to a variable. Some simple examples: \/\/ Fails, Trying to put an integer in a string String one = 1; \/\/ Also fails. int foo = &#8220;bar&#8221;; This also applies to method [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1093","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1093","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1093"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1093\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1093"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1093"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1093"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}