{"id":16,"date":"2014-05-27T18:05:43","date_gmt":"2014-05-27T18:05:43","guid":{"rendered":"https:\/\/blogs.scummvm.org\/subr3v\/?p=16"},"modified":"2022-05-23T18:07:37","modified_gmt":"2022-05-23T18:07:37","slug":"refactoring-and-code-readability","status":"publish","type":"post","link":"https:\/\/blogs.scummvm.org\/subr3v\/2014\/05\/27\/refactoring-and-code-readability\/","title":{"rendered":"Refactoring and code readability"},"content":{"rendered":"<p>Today I&#8217;m going to write a post about how refactoring is useful to increase the readability (and thus, maintainability) of the code.<\/p>\n<p>I will start with a basic example: tinyGL uses a struct called V3 to represent three dimensional vectors:<\/p>\n<pre>struct\u00a0V3\u00a0{\r\n\tfloat\u00a0v[3];\r\n};<\/pre>\n<p>The APi also defines some functions to work with vectors (namely construction, function to get the normal vector, multiplication, copy, etc)<\/p>\n<pre>V3\u00a0gl_V3_New(float\u00a0x,\u00a0float\u00a0y,\u00a0float\u00a0z);\r\nint\u00a0gl_V3_Norm(V3\u00a0*a);\r\nvoid\u00a0gl_MulM3V3(V3\u00a0*a,\u00a0const\u00a0M4\u00a0*b,\u00a0const\u00a0V3\u00a0*c);\r\nvoid\u00a0gl_MoveV3(V3\u00a0*a,\u00a0const\u00a0V3\u00a0*b);<\/pre>\n<p>Which in turn leads to code like this:<\/p>\n<pre>V3\u00a0a,\u00a0b;\r\na\u00a0=\u00a0gl_V3_New(5,5,5);\r\nM4\u00a0transform;\r\ngl_MulM4V3(b,transform,a);<\/pre>\n<p>My goal with this refactoring is to increase the readability by introducing a class Vector3, which will make creating vectors and using the much easier (I also plan to write classes that represent matrices so that operations between vector and matrices can be expressed in a much concise and intuitive way)<\/p>\n<pre>class\u00a0Matrix4\u00a0{\r\n\u00a0\u00a0public:\r\n\tMatrix4();\r\n\tMatrix4(const\u00a0Matrix4\u00a0&amp;other);\r\n \r\n\tMatrix4\u00a0operator=(const\u00a0Matrix4\u00a0&amp;other);\r\n\tMatrix4\u00a0operator*(const\u00a0Matrix4\u00a0&amp;b);\r\n\tstatic\u00a0Matrix4\u00a0identity();\r\n \r\n\tMatrix4\u00a0transpose()\u00a0const;\r\n\tMatrix4\u00a0inverse_ortho()\u00a0const;\r\n\tMatrix4\u00a0inverse()\u00a0const;\r\n\tMatrix4\u00a0rotation()\u00a0const;\r\n \r\n\tVector3\u00a0transform(const\u00a0Vector3\u00a0&amp;vector)\u00a0const;\r\n\tVector4\u00a0transform(const\u00a0Vector4\u00a0&amp;vector)\u00a0const;\r\n\u00a0\u00a0private:\r\n\tfloat\u00a0m[4][4];\r\n};\r\n\r\nclass\u00a0Vector3\u00a0{\r\n\u00a0\u00a0public:\r\n\tVector3();\r\n\tVector3(const\u00a0Vector3\u00a0&amp;other);\r\n\tVector3(float\u00a0x,\u00a0float\u00a0y,\u00a0float\u00a0z);\r\n\tVector3\u00a0operator=(const\u00a0Vector3\u00a0&amp;other);\r\n \r\n\tstatic\u00a0Vector3\u00a0normal(const\u00a0Vector3\u00a0&amp;v);\r\n \r\n\tVector3\u00a0operator*(float\u00a0value);\r\n\tVector3\u00a0operator+(const\u00a0Vector3\u00a0&amp;other);\r\n\tVector3\u00a0operator-(const\u00a0Vector3\u00a0&amp;other);\r\n \r\n\u00a0\u00a0private:\r\n\tfloat\u00a0v[3];\r\n};<\/pre>\n<p>Those changes would make that previous snippet look like this:<\/p>\n<pre>Vector3\u00a0a(5,5,3);\r\nMatrix4\u00a0transform;\r\nVector3\u00a0b\u00a0=\u00a0transform.transform(a);<\/pre>\n<p>As we can see from this little snippet the code is more readable, plus now that vectors are represented by classes we can also overload binary operators such as + and &#8211; to express vector addition and subtraction in a more concise way (instead of having to write the addition separately for each component).<\/p>\n<p>That&#8217;s all for now! I will try to update the blog again this week to show more examples.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today I&#8217;m going to write a post about how refactoring is useful to increase the readability (and thus, maintainability) of the code. I will start with a basic example: tinyGL uses a struct called V3 to represent three dimensional vectors: struct\u00a0V3\u00a0{ float\u00a0v[3]; }; The APi also defines some functions to work with vectors (namely construction, [&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-16","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blogs.scummvm.org\/subr3v\/wp-json\/wp\/v2\/posts\/16","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.scummvm.org\/subr3v\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.scummvm.org\/subr3v\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.scummvm.org\/subr3v\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.scummvm.org\/subr3v\/wp-json\/wp\/v2\/comments?post=16"}],"version-history":[{"count":2,"href":"https:\/\/blogs.scummvm.org\/subr3v\/wp-json\/wp\/v2\/posts\/16\/revisions"}],"predecessor-version":[{"id":18,"href":"https:\/\/blogs.scummvm.org\/subr3v\/wp-json\/wp\/v2\/posts\/16\/revisions\/18"}],"wp:attachment":[{"href":"https:\/\/blogs.scummvm.org\/subr3v\/wp-json\/wp\/v2\/media?parent=16"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.scummvm.org\/subr3v\/wp-json\/wp\/v2\/categories?post=16"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.scummvm.org\/subr3v\/wp-json\/wp\/v2\/tags?post=16"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}