
{"id":1918,"date":"2015-08-10T14:48:43","date_gmt":"2015-08-10T14:48:43","guid":{"rendered":"http:\/\/www.codergears.com\/Blog\/?p=1918"},"modified":"2015-08-15T13:51:31","modified_gmt":"2015-08-15T13:51:31","slug":"learn-c-programming-by-studying-real-projects-sqlite-case-study","status":"publish","type":"post","link":"https:\/\/codergears.com\/Blog\/?p=1918","title":{"rendered":"Learn the &#8220;C&#8221; programming language by studying real projects: SQLite case study"},"content":{"rendered":"<p>Many resources exist talking about the better way to\u00a0learn a programming language, we can enumerate:<\/p>\n<ul style=\"color: #555555;\">\n<li>Reading a book or a magazine.<\/li>\n<li>Web sites.<\/li>\n<li>From a collegue.<\/li>\n<li>Doing a training.<\/li>\n<\/ul>\n<p>Another more interesting approach is to study a known and\u00a0mature\u00a0open source project to discover how their developers implements the code. In case of C language a good candidate could be\u00a0the \u00a0<a href=\"https:\/\/www.sqlite.org\/\">SQLite<\/a>\u00a0source code.<!--more--><\/p>\n<p>What&#8217;s interesting with SQLite is that it&#8217;s accessible even\u00a0for the beginner C developers, its contributors use very basic coding rules, what makes it very easy to understand and maintain.<\/p>\n<p>Let\u2019s go inside the SQLite\u00a0source code using\u00a0<a href=\"http:\/\/www.cppdepend.com\/\">CppDepend\u00a0<\/a>and discover some basic coding rules adopted by their developers.<\/p>\n<p><strong>Encapsulation<\/strong><\/p>\n<p style=\"color: #555555;\">Encapsulation \u00a0is the hiding of functions and data which are internal to an implementation. \u00a0In C, encapsulation is performed by using the static keyword . These entities are called file-scope functions and variables.<\/p>\n<p style=\"color: #555555;\">Let\u2019s search for all static functions by executing the following CQLinq query<\/p>\n<p style=\"color: #555555;\"><a href=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/linux17.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-192\" src=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/linux17.png\" alt=\"linux17\" width=\"383\" height=\"38\" \/><\/a><\/p>\n<p>We can use the Metric view to\u00a0have a good idea how many functions are concerned. In the Metric View, the code base is represented through a Treemap. Treemapping is a method for displaying tree-structured data by using nested rectangles. The tree structure used in a CppDepend treemap is the usual code hierarchy:<\/p>\n<ul>\n<li>Projects contains directories.<\/li>\n<li>Directories\u00a0contains files.<\/li>\n<li>Files\u00a0contains struects, functions\u00a0and variables.<\/li>\n<\/ul>\n<p>The treemap view provides a useful way to represent the result of a CQLinq request, and for the previous request \u00a0the static\u00a0methods are the not greyed ones.<\/p>\n<p><a href=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/sqlite.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1925\" src=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/sqlite.png\" alt=\"sqlite\" width=\"909\" height=\"473\" \/><\/a><\/p>\n<p style=\"color: #555555;\">As we can observe many functions are declared as static.<\/p>\n<p style=\"color: #555555;\"><strong>Use structs to store your data model<\/strong><\/p>\n<p style=\"color: #555555;\">In C programing the functions uses variables to acheive their treatments, theses variables could be:<\/p>\n<ul>\n<li>Static variables.<\/li>\n<li>Global variables.<\/li>\n<li>Local variables<\/li>\n<li>Variables from structs.<\/li>\n<\/ul>\n<p>Each project has it\u2019s data model which could be used by many source files, using global variables is a solution but not the good one, using structs to group data is more recommended.<\/p>\n<p>Let\u2019s search for defined structs:<\/p>\n<p style=\"color: #555555;\"><a href=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/sqlite22.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1929\" src=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/sqlite22.png\" alt=\"sqlite22\" width=\"399\" height=\"556\" \/><\/a><\/p>\n<p style=\"color: #555555;\">Many structs are used to specify the data model.<\/p>\n<p style=\"color: #555555;\"><strong>Let function be short and sweet<\/strong><\/p>\n<p style=\"color: #555555;\">Here\u2019s from the\u00a0<a href=\"https:\/\/www.kernel.org\/doc\/Documentation\/CodingStyle\">linux coding style web page<\/a>, an\u00a0advice about the length of functions:<\/p>\n<pre style=\"color: #000000;\">Functions should be short and sweet, and do just one thing.  They should\r\nfit on one or two screenfuls of text (the ISO\/ANSI screen size is 80x24,\r\nas we all know), and do one thing and do that well.\r\n\r\nThe maximum length of a function is inversely proportional to the\r\ncomplexity and indentation level of that function.  So, if you have a\r\nconceptually simple function that is just one long (but simple)\r\ncase-statement, where you have to do lots of small things for a lot of\r\ndifferent cases, it's OK to have a longer function.<\/pre>\n<p style=\"color: #555555;\">\u00a0Let\u2019s search for functions where the number of lines of code is less\u00a0than 30<\/p>\n<p style=\"color: #555555;\"><a href=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/sqlite5.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1934\" src=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/sqlite5.png\" alt=\"sqlite5\" width=\"400\" height=\"506\" \/><\/a><\/p>\n<p style=\"color: #555555;\">More than 90% of functions\u00a0has less \u00a0than 30 lines of code.<\/p>\n<p style=\"color: #555555;\"><strong>Function Number of parameters<\/strong><\/p>\n<p style=\"color: #555555;\">Functions\u00a0where NbParameters &gt; 8\u00a0might be painful to call \u00a0and might degrade performance. Another alternative is to provide \u00a0a structure dedicated to handle arguments passing.<\/p>\n<p style=\"color: #555555;\"><a href=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/sqlite6.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1935\" src=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/sqlite6.png\" alt=\"sqlite6\" width=\"404\" height=\"519\" \/><\/a><\/p>\n<p style=\"color: #555555;\">only few\u00a0methods has more than 8 parameters.<\/p>\n<p style=\"color: #555555;\"><strong>Number of\u00a0local variables<\/strong><\/p>\n<p style=\"color: #555555;\">Methods where NbVariables is higher than 8 are hard to understand and maintain. Methods where NbVariables is higher than 15 are extremely complex and should be split in smaller methods (except if they are automatically generated by a tool).<\/p>\n<p style=\"color: #555555;\"><a href=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/sqlite7.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1936\" src=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/sqlite7.png\" alt=\"sqlite7\" width=\"402\" height=\"546\" \/><\/a><\/p>\n<p style=\"color: #555555;\">only few\u00a0functions has more than 15 local variables.<\/p>\n<p><strong>Avoid defining complex functions<\/strong><\/p>\n<p>Many metrics exist to detect complex functions, NBLinesOfCode,Number of parameters and number of local variables are the basic ones.<\/p>\n<p>There are other interesting metrics to detect complex functions:<\/p>\n<ul>\n<li>Cyclomatic complexity is a popular procedural software metric equal to the number of decisions that can be taken in a procedure.<\/li>\n<li>Nesting Depth\u00a0is a metric defined on methods that is relative to the maximum depth\u00a0of the more nested scope in a method body.<\/li>\n<li>Max Nested loop is\u00a0equals the maximum level of loop nesting in a function.<\/li>\n<\/ul>\n<p>The max value tolerated for these metrics depends more on the team choices, there\u2019s no standard values.<\/p>\n<p>Let\u2019s search for functions candidate to be refactored:<\/p>\n<p><a href=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/sqlite8.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1937\" src=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/sqlite8.png\" alt=\"sqlite8\" width=\"435\" height=\"512\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>only very few functions could be considered as complex.<\/p>\n<p><strong>Be Const Correct<\/strong><\/p>\n<p><span style=\"color: #000000;\">C provides the\u00a0<\/span><em style=\"color: #34495e;\">const<\/em><span style=\"color: #000000;\">\u00a0key word to allow passing as parameters objects that cannot change to indicate when a method doesn&#8217;t modify its object. Using const in all the right places is called &#8220;const correctness.&#8221; It&#8217;s hard at first, but using const really tightens up your coding style.\u00a0<\/span><\/p>\n<p>Let&#8217;s search for functions having at least one const parameter:<\/p>\n<p><a href=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/sqlite9.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1938\" src=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/sqlite9.png\" alt=\"sqlite9\" width=\"439\" height=\"424\" \/><\/a><\/p>\n<p><strong>Function coupling<\/strong><\/p>\n<p>Functions using many other ones are very difficult to understand and maintain. It&#8217;s recommended \u00a0to minimize the efferent coupling of your functions.<\/p>\n<p>For SQLite very few functions have\u00a0a high efferent coupling:<\/p>\n<p><a href=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/sqlite10.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1942\" src=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/sqlite10.png\" alt=\"sqlite10\" width=\"438\" height=\"521\" \/><\/a><\/p>\n<p><strong>If you can exit a function early, you should.<\/strong><\/p>\n<p><span style=\"color: #34495e;\">Early exits out of a function, specially through guard clauses at the top of a function are preferred since they simplify the logic further down in the function.<\/span><\/p>\n<p>In the SQLite source code this best practice rule is applied for almost all the functions.<\/p>\n<p><strong>What&#8217;s not recommended from the sqlite implementation<\/strong><\/p>\n<p>The sqlite3.c file contains many functions, structs and variables which is not recommended. However Sqlite3 is designed to be embedded in other projects and it&#8217;s very practical to have one file to embed.<\/p>\n<p><strong>Conclusion<\/strong><\/p>\n<p>Exploring some known open source projects is always good to elevate your programming skills, no need to download and build the project, you can just discover the code from GitHub.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Many resources exist talking about the better way to\u00a0learn a programming language, we can enumerate: Reading a book or a magazine. Web sites. From a collegue. Doing a training. Another more interesting approach is to study a known and\u00a0mature\u00a0open source project to discover how their developers implements the code. In case of C language a &hellip; <a href=\"https:\/\/codergears.com\/Blog\/?p=1918\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Learn the &#8220;C&#8221; programming language by studying real projects: SQLite case study&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-1918","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=\/wp\/v2\/posts\/1918","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1918"}],"version-history":[{"count":21,"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=\/wp\/v2\/posts\/1918\/revisions"}],"predecessor-version":[{"id":1997,"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=\/wp\/v2\/posts\/1918\/revisions\/1997"}],"wp:attachment":[{"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1918"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1918"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1918"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}