{"id":7,"date":"2016-07-14T22:26:28","date_gmt":"2016-07-14T22:26:28","guid":{"rendered":"http:\/\/cppdepend.com\/wordpress\/?p=7"},"modified":"2016-07-22T15:06:32","modified_gmt":"2016-07-22T15:06:32","slug":"sqlite-the-art-of-keep-it-simple","status":"publish","type":"post","link":"https:\/\/cppdepend.com\/blog\/sqlite-the-art-of-keep-it-simple\/","title":{"rendered":"SQLite: The art of keep it simple."},"content":{"rendered":"<p><a href=\"http:\/\/article.gmane.org\/gmane.comp.db.sqlite.general\/102084\">16 years after its first checkin<\/a>, SQLite is the\u00a0<a style=\"color: #734559;\" href=\"https:\/\/www.sqlite.org\/mostdeployed.html\">most widely deployed<\/a>\u00a0database engine in the world.\u00a0such open source project is a good candidate to learn how to make your code easy to understand and to maintain.<\/p>\n<p>Let&#8217;s discover some facts about the SQLite code base, for that\u00a0let&#8217;s begin with the following code snippet:<!--more--><\/p>\n<p><a href=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/sqlite1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2395\" src=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/sqlite1.png\" alt=\"sqlite\" width=\"592\" height=\"398\" \/><\/a><\/p>\n<p>Here are some remarks concerning this function:<\/p>\n<ul>\n<li>The function\u00a0is declared as static.<\/li>\n<li>The function return an error code.<\/li>\n<li>The function has few parameters.<\/li>\n<li>The\u00a0function exists as early as possible.<\/li>\n<li>Assert technique is used to check some conditions.<\/li>\n<li>No global variable used.<\/li>\n<li>The \u00a0variable naming is easy to understand.<\/li>\n<li>The method is short.<\/li>\n<li>\u00a0No extra comments in the body.<\/li>\n<li>The function body is well indented.<\/li>\n<\/ul>\n<p>If we\u00a0navigate across the SQLite source code we can remark the coherence of the implementation. The same best pratices rules are applied to each function.<\/p>\n<p>Here are some best practices to learn from\u00a0the SQLite codebase:<\/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 style=\"color: #333333;\">\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 style=\"color: #333333;\">Each project has its 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 style=\"color: #333333;\">Let\u2019s search using CQlinq and <a href=\"http:\/\/www.cppdepend.com\">CppDepend<\/a> 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: #333333;\"><strong>Encapsulation<\/strong><\/p>\n<p>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>Let\u2019s search for all static functions by executing the following CQLinq query<\/p>\n<p style=\"color: #333333;\"><a href=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/sqlite2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2402\" src=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/sqlite2.png\" alt=\"sqlite\" width=\"441\" height=\"546\" \/><\/a><\/p>\n<p>As we can observe many functions are declared as static.<\/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 style=\"color: #333333;\"><strong>Avoid defining complex functions<\/strong><\/p>\n<p style=\"color: #333333;\">Many metrics exist to detect complex functions, NBLinesOfCode,Number of parameters and number of local variables are the basic ones.<\/p>\n<p style=\"color: #333333;\">There are other interesting metrics to detect complex functions:<\/p>\n<ul style=\"color: #333333;\">\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 style=\"color: #333333;\">The max value tolerated for these metrics depends more on the team choices, there\u2019s no standard values.<\/p>\n<p style=\"color: #333333;\">Let\u2019s search for functions candidate to be refactored:<\/p>\n<p style=\"color: #333333;\"><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 style=\"color: #333333;\">only very few functions could be considered as complex.<\/p>\n<p style=\"color: #333333;\"><strong>Be Const Correct<\/strong><\/p>\n<p style=\"color: #333333;\"><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\u2019t modify its object. Using const in all the right places is called \u201cconst correctness.\u201d It\u2019s hard at first, but using const really tightens up your coding style.\u00a0<\/span><\/p>\n<p style=\"color: #333333;\">Let\u2019s search for functions having at least one const parameter:<\/p>\n<p style=\"color: #333333;\"><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 style=\"color: #333333;\"><strong>Function coupling<\/strong><\/p>\n<p style=\"color: #333333;\">Functions using many other ones are very difficult to understand and maintain. It\u2019s recommended \u00a0to minimize the efferent coupling of your functions.<\/p>\n<p style=\"color: #333333;\">For SQLite very few functions have\u00a0a high efferent coupling:<\/p>\n<p style=\"color: #333333;\"><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 style=\"color: #333333;\"><strong>If you can exit a function early, you should.<\/strong><\/p>\n<p style=\"color: #333333;\"><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 style=\"color: #333333;\">In the SQLite source code this best practice rule is applied for almost all the functions.<\/p>\n<p style=\"color: #333333;\"><strong>Conclusion<\/strong><\/p>\n<p style=\"color: #333333;\">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","protected":false},"excerpt":{"rendered":"<p>16 years after its first checkin, SQLite is the\u00a0most widely deployed\u00a0database engine in the world.\u00a0such open source project is a good candidate to learn how to make your code easy to understand and to maintain. Let&#8217;s discover some facts about the SQLite code base, for that\u00a0let&#8217;s begin with the following code snippet:<\/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":[11,7,8,10],"class_list":["post-7","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-best-practice","tag-c","tag-keep-it-simple","tag-sqlite"],"_links":{"self":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/7","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/comments?post=7"}],"version-history":[{"count":1,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/7\/revisions"}],"predecessor-version":[{"id":8,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/posts\/7\/revisions\/8"}],"wp:attachment":[{"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/media?parent=7"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/categories?post=7"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cppdepend.com\/blog\/wp-json\/wp\/v2\/tags?post=7"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}