layout: true <div class="my-footer"><span>ouzhang.me/talk/readable_code</span></div> <!-- this adds the link footer to all slides, depends on my-footer class in css--> --- name: xaringan-title class: left, middle background-image: url(img/front_page.jpg) background-size: cover # Write Readable Code ### .fancy[Simple and Practical Techniques for Better Statistical Programming] .large[Ou Zhang | Summer Intern::Seminar 2018] <!-- this ends up being the title slide since seal = FALSE--> --- class: right, middle <img class="circle" src="img/Ou_Zhang.jpg" width="150px"/> # Find me at... [<svg style="height:0.8em;top:.04em;position:relative;" viewBox="0 0 512 512"><path d="M459.37 151.716c.325 4.548.325 9.097.325 13.645 0 138.72-105.583 298.558-298.558 298.558-59.452 0-114.68-17.219-161.137-47.106 8.447.974 16.568 1.299 25.34 1.299 49.055 0 94.213-16.568 130.274-44.832-46.132-.975-84.792-31.188-98.112-72.772 6.498.974 12.995 1.624 19.818 1.624 9.421 0 18.843-1.3 27.614-3.573-48.081-9.747-84.143-51.98-84.143-102.985v-1.299c13.969 7.797 30.214 12.67 47.431 13.319-28.264-18.843-46.781-51.005-46.781-87.391 0-19.492 5.197-37.36 14.294-52.954 51.655 63.675 129.3 105.258 216.365 109.807-1.624-7.797-2.599-15.918-2.599-24.04 0-57.828 46.782-104.934 104.934-104.934 30.213 0 57.502 12.67 76.67 33.137 23.715-4.548 46.456-13.32 66.599-25.34-7.798 24.366-24.366 44.833-46.132 57.827 21.117-2.273 41.584-8.122 60.426-16.243-14.292 20.791-32.161 39.308-52.628 54.253z"/></svg> @zhangou](http://twitter.com/zhangou888) [<svg style="height:0.8em;top:.04em;position:relative;" viewBox="0 0 496 512"><path d="M165.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6zm-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3zm44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9zM244.8 8C106.1 8 0 113.3 0 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C428.2 457.8 496 362.9 496 252 496 113.3 383.5 8 244.8 8zM97.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1zm-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7zm32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1zm-11.4-14.7c-1.6 1-1.6 3.6 0 5.9 1.6 2.3 4.3 3.3 5.6 2.3 1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2z"/></svg> @zhangou](http://github.com/zhangou888) [<svg style="height:0.8em;top:.04em;position:relative;" viewBox="0 0 512 512"><path d="M326.612 185.391c59.747 59.809 58.927 155.698.36 214.59-.11.12-.24.25-.36.37l-67.2 67.2c-59.27 59.27-155.699 59.262-214.96 0-59.27-59.26-59.27-155.7 0-214.96l37.106-37.106c9.84-9.84 26.786-3.3 27.294 10.606.648 17.722 3.826 35.527 9.69 52.721 1.986 5.822.567 12.262-3.783 16.612l-13.087 13.087c-28.026 28.026-28.905 73.66-1.155 101.96 28.024 28.579 74.086 28.749 102.325.51l67.2-67.19c28.191-28.191 28.073-73.757 0-101.83-3.701-3.694-7.429-6.564-10.341-8.569a16.037 16.037 0 0 1-6.947-12.606c-.396-10.567 3.348-21.456 11.698-29.806l21.054-21.055c5.521-5.521 14.182-6.199 20.584-1.731a152.482 152.482 0 0 1 20.522 17.197zM467.547 44.449c-59.261-59.262-155.69-59.27-214.96 0l-67.2 67.2c-.12.12-.25.25-.36.37-58.566 58.892-59.387 154.781.36 214.59a152.454 152.454 0 0 0 20.521 17.196c6.402 4.468 15.064 3.789 20.584-1.731l21.054-21.055c8.35-8.35 12.094-19.239 11.698-29.806a16.037 16.037 0 0 0-6.947-12.606c-2.912-2.005-6.64-4.875-10.341-8.569-28.073-28.073-28.191-73.639 0-101.83l67.2-67.19c28.239-28.239 74.3-28.069 102.325.51 27.75 28.3 26.872 73.934-1.155 101.96l-13.087 13.087c-4.35 4.35-5.769 10.79-3.783 16.612 5.864 17.194 9.042 34.999 9.69 52.721.509 13.906 17.454 20.446 27.294 10.606l37.106-37.106c59.271-59.259 59.271-155.699.001-214.959z"/></svg> ouzhang.me](https://ouzhang.me) [<svg style="height:0.8em;top:.04em;position:relative;" viewBox="0 0 512 512"><path d="M476 3.2L12.5 270.6c-18.1 10.4-15.8 35.6 2.2 43.2L121 358.4l287.3-253.2c5.5-4.9 13.3 2.6 8.6 8.3L176 407v80.5c0 23.6 28.5 32.9 42.5 15.8L282 426l124.6 52.2c14.2 6 30.4-2.9 33-18.2l72-432C515 7.8 493.3-6.8 476 3.2z"/></svg> zhangou888@gmail.com](mailto:zhangou888@gmail.com) --- # .center[Agenda] What is the readable code? -- Why we need to write readable code? -- Factors Influence Code Readability -- Version Control -- Q & A --- name: why class: center, inverse, middle # .fancy[Why I give this talk to **Psychometrician** and **statistical analyst**?] --- # .center[Key elements to success] -- - Content Knowledge -- - Psychometrics skills -- - Programming -- ## .center[Good programming means <p style="color:red"> Work efficiency</p>] --- # .center[Disclaimer] -- I am presenting what I consider the <span style="color: red;">best<span/> practice. -- I acknowledge that there are other ways of doing things. -- Suggestions & Personal Opinion <span style="color: blue;">(Biased)<span/> -- You may have your own opinions or existing standards and... -- ## .center[<span style="color: red;">**it is completely fine.**<span/>] --- name: ugly code runs # .center[Ugly code runs] -- <img src="img/ugly_code_run.png" width="90%" /> --- name: what is good code class: center, inverse, middle # .fancy[What is readable code?] --- # .center[A readable code is...] -- Easy to read & understand -- Easy to extend & reuse -- Gives reliable results -- Easy to maintain -- Testable -- Easy to share with others --- name: why write a readable code class: center, inverse, middle # .fancy[Why write a readable code?] --- ## .center[Why write a readable code?] People often share their code with others -- Code might be adapted or modified by others -- ![](img/reuse_code.jpg) --- ## .center[More importantly, the code is...] -- For your <span style="color: red;">**FUTURE SELF**<span/> -- <img src="img/code_is_for_future_self.jpg" width="60%" style="display: block; margin: auto;" /> --- name: two concept class: center, inverse, middle # .fancy[Readable code vs. Efficient code] --- ## .center[Readable code vs. Efficient code] -- Code efficiency is directly linked with algorithmic efficiency and the speed of runtime execution for software. It is the key element in ensuring ** computer** high performance -- Code readability is directly related to the most important qualities of code. If code is easy to read **by the user**, it will be easy to understand which makes it easier to debug, maintain and extend. --- name: Focus class: center, inverse, middle # .fancy[Today's focus: Code Readability] --- ## .center[Code Readability] -- >"Programs must be written for people to read, and only incidentally for machines to execute." .right[Abelson & Sussman, Structure and Interpretation of Computer Programs] -- > At present, the work efficiency bottleneck is **human brain** not machine. .right[Hadley Wickham] -- <img src="img/computer_VS_human.jpg" width="35%" style="display: block; margin: auto;" /> --- name: what do you mean class: center, inverse, middle # .fancy[What do you mean by readable code?] --- ## .center[What do you mean?] -- Keep it simple, Keep it short. -- At least, keep the **module** simple and short. -- <img src="img/keep_code_small.png" width="65%" style="display: block; margin: auto;" /> --- ## .center[Key elements of readable code] -- .pull-left[ Readable – clarity in the structure and content of the code * is easy for the user to read * is as self-explaining as possible ] -- .pull-right[ <img src="img/self-explaining.PNG" width="100%" style="display: block; margin: auto;" /> ] --- ## .center[Key elements of readable code] -- .pull-left[ Shareable – how quickly and easily someone new can understand and modify your code ] -- .pull-right[ <img src="img/shareable_code.jpg" width="100%" style="display: block; margin: auto;" /> ] --- ## .center[Suggestion] -- Don’t implement code that you won’t need -- Simplify requirements -- Keep your codebase small -- Be familiar with libraries around --- ## .center[Good code standards] -- Beautiful is better than ugly -- Explicit is better than implicit -- Simple is better than complex -- Complex is better than complicated -- Readability counts --- name: factors class: center, inverse, middle # .fancy[Factors influence code readability] --- ## .center[Factors] -- Naming convention -- Style/ Aesthetic -- Commenting -- Functions/Macros -- * Split Your Code into Short, Focused Units * Don’t repeat yourself * Avoid nesting definition -- Check errors and Respond to Them -- Avoid dead code --- name: naming convention title class: right, inverse, bottom background-image: url(img/naming-conventions.png) background-size: cover # Naming Convention --- name: naming convention1 # Naming Convention -- Adopt a convention and stick with it (fetch vs. retrieve vs. get) -- Use intention-revealing names -- Avoid misleading readers -- Make meaningful distinctions -- Use searchable names -- Avoid mental mapping -- Use domain names --- name: naming convention2 # Naming Convention ⏭ <!-- grep("uk", names(emo::ji_name), value = TRUE, ignore.case = TRUE) --> -- Avoid Generic Names -- Bad name example: <span style="color: red;">v1, v2, v3, temp1</span> -- One exception: **well-written, verified function/macro** -- Prefer Concrete Names -- Example: <span style="color: red;">percent_a, average_item_time</span>, etc. --- name: naming convention3 # Naming Convention ⏭ -- Use searchable names <span style="color: blue;">(for debugging) </span> -- <img src="img/searchable_name.png" width="100%" style="display: block; margin: auto;" /> --- name: naming convention4 # Naming Convention ⏭ -- .pull-left[ Length vs Meaning Shorter names for shorter scopes Longer names for larger scopes ] -- .pull-right[ <img src="img/Name_is_the_key.PNG" width="100%" style="display: block; margin: auto;" /> ] --- name: naming convention5 # Naming Convention ⏭ -- .pull-left[ Formatting * All lower letter * All UPPER letter * period.separated * underscore_spearated * lowerCamelCase * UpperCamelCase ] -- .pull-right[ Example * <span style="color: blue;">searchpaths</span> * <span style="color: blue;">IRT_A</span> * <span style="color: blue;">item.info</span> * <span style="color: blue;">expect_theta</span> * <span style="color: blue;">seq_Along</span> * <span style="color: blue;">NextMethod</span> ] -- <div class="figure" style="text-align: center"> <img src="img/matching_naming_convention.png" alt="From “The State of Naming Conventions in R " width="65%" /> <p class="caption">From “The State of Naming Conventions in R </p> </div> --- name: naming convention6 # Naming Convention ⏭ -- <span style="color: red;">Noun</span> for variable name, object name, class name, property name, arguments name -- <span style="color: red;">Verb</span> for function name, method name -- Avoid using the same word for two purposes -- Ask yourself... --- name: naming convention7 class: center, inverse, middle # .fancy[What other meanings could someone interpret from this name?] --- name: naming convention8 # Naming Convention ⏭ -- Names That Can’t Be Misconstrued -- <img src="img/misconstrued.png" width="70%" style="display: block; margin: auto;" /> -- .right[The State of Naming Conventions in R – Rasmus Baath] -- --- name: style_aesthetic title class: center, inverse, middle background-image: url(img/Style-vs-Aesthetics.jpg) background-size: cover # .fancy[style & aesthetic] --- name: style_aesthetic1 # Style and Aesthetics -- Use consistent layout -- * Indent your code & Vertical and horizontal spacing -- * Limit the width of your code per row (80 columns) -- * Limit the length of function -- Make similar code look similar -- Group related lines of code into blocks --- name: style_aesthetic2 # Style and Aesthetics ⏭ -- .pull-left[ Coding text * All lower case text * All UPPER case text * CamelCase text ] -- .pull-right[ Follow the Style Guide * [Google’s R Style Guide](https://google.github.io/styleguide/Rguide.html) * Code like it matters (Paul Kaefer) * Hadley Wickham: [Style Guide](http://adv-r.had.co.nz/Style.html) * SAS Programming Guidelines * [R Style. An Rchaeological Commentary](https://cran.r-project.org/web/packages/rockchalk/vignettes/Rstyle.pdf) ] --- name: Style suggestion class: center, inverse, middle # .fancy[Write code like you work on your daily writing] --- name: style_aesthetic3 # Style and Aesthetics ⏭ -- .pull-left[ Good example <img src="img/good_example.png" width="125%" style="display: block; margin: auto auto auto 0;" /> ] -- .pull-right[ Bad example <img src="img/bad_example.png" width="125%" style="display: block; margin: auto 0 auto auto;" /> ] --- name: commenting title class: left, middle background-image: url(img/comment.png) background-size: cover # .fancy[Commenting] --- name: comment1 # Commenting -- .pull-left[ The purpose of commenting is to help the reader know as much as the writer did. ] -- .pull-right[ <img src="img/put_yourself_in_reader_shoes.jpg" width="125%" style="display: block; margin: auto 0 auto auto;" /> ] --- name: comment2 # Commenting ⏭ -- .pull-left[ Recording Your Thoughts * Include “Director Commentary” * Comment the Flaws in Your Code * Comment on Your Constants ] -- .pull-right[ Put yourself in the reader’s shoes * Anticipating Likely Questions * Advertising Likely Pitfalls * “Big Picture” * Summary ] --- name: No_comment title class: center, inverse, middle background-image: url(img/no_comment.jpg) background-size: cover # .fancy[Don’t comment on the facts that can be derived quickly from the code itself.] --- name: comment3 # Commenting ⏭ -- Comment while writing your code -- <img src="img/Add_comment_is_important.jpg" width="70%" style="display: block; margin: auto;" /> --- name: comment4 # Commenting ⏭ -- .pull-left[ Keep Comments Compact Describe Function Behavior Precisely Use Examples State the Intent ] -- .pull-right[ <img src="img/Meaningful_comments.PNG" width="80%" style="display: block; margin: auto;" /> ] --- name: comment5 # Keep comments compact -- <img src="img/compact_comment.png" width="80%" style="display: block; margin: auto;" /> --- name: comment6 # Describe Function Precisely -- <img src="img/precise_comment.png" width="80%" style="display: block; margin: auto;" /> --- name: comment7 # Use Examples -- <img src="img/use_example_comment.png" width="80%" style="display: block; margin: auto;" /> --- name: comment8 # Precise & Compact Comments <img src="img/Concise_comment.jpg" width="75%" style="display: block; margin: auto;" /> --- name: Good_header class: center, inverse, middle # .fancy[Having a good header delivers most information of the code.] --- name: sas_header # Header Example 1 -- <img src="img/sas_header.png" width="75%" style="display: block; margin: auto;" /> --- name: r_header # Header Example 2 -- <img src="img/r_header.png" width="75%" style="display: block; margin: auto;" /> --- name: loops_logic class: center, middle background-image: url(img/loops.jpg) background-size: cover # .fancy[Loops & Logic] --- name: loop1 # Loop & Logic -- .pull-left[ Make your logic as “**natural**” as possible The code is not just “**Get the job done**”. A good readable code means better work efficiency and better work transition in the future. ] -- .pull-right[ <img src="img/how to write good code.png" width="80%" style="display: block; margin: auto;" /> ] --- name: loop2 # Loop & Logic ⏭ -- Avoid **do/while** Loops -- Returning Early form a Function -- Minimize Nesting -- Flow of Execution --- name: loop3 # Loop & Logic ⏭ -- Minimize the time needed for someone to understand it instead of minimizing the number of lines -- <img src="img/bad_function.png" width="65%" style="display: block; margin: auto;" /> --- name: loop4 # Loop & Logic ⏭ -- Breaking Down Giant Expressions -- <img src="img/one_bite.png" width="65%" style="display: block; margin: auto;" /> --- name: loop5 # Loop & Logic ⏭ -- .pull-left[ Explaining Variables * Summary Variables * Use De Morgan’s Laws * Break Down Giant Statements ] .pull-right[ <img src="img/Demorgan_laws.png" width="75%" style="display: block; margin: auto;" /> ] --- name: loop6 # Loop & Logic ⏭ -- Separate data input/output steps from core function modules .pull-left[ Bad Example: One Big function: 1. Data input 2. Core calculation 3. Data output ] .pull-right[ Good Example: * Function 1: Data input function * Function 2: Short Core calculation function * Function 3: Data output function ] --- name: No_comment title class: center, middle background-image: url(img/modules.png) background-size: cover # .fancy[Function, Module, Macro] --- name: function1 # Function, Module, Macro -- One Task <span style="color: red;">Well<span/> at a Time, One Task <span style="color: red;">Only<span/> at a Time -- <img src="img/multi_tasking.png" width="65%" style="display: block; margin: auto;" /> --- name: function2 # Function, Module, Macro ⏭ -- Don't <span style="color: red;">repeat<span/> yourself -- <img src="img/repeat_line.png" width="75%" style="display: block; margin: auto;" /> --- name: function3 # Function, Module, Macro ⏭ -- Bad Example: <img src="img/bad_repeat.png" width="85%" style="display: block; margin: auto;" /> --- name: function4 # Function, Module, Macro ⏭ -- Good Example: <img src="img/good_no_repeat.png" width="85%" style="display: block; margin: auto;" /> --- name: function5 # Function, Module, Macro ⏭ -- Avoid definition nesting -- <img src="img/nesting_function.png" width="85%" style="display: block; margin: auto;" /> --- name: function6 # Modularity -- Write code so it is multi-purpose and can be repurposed e.g., macro variables for filenames and date values -- <img src="img/macro_driver.png" width="85%" style="display: block; margin: auto;" /> --- name: No_comment title class: center, middle background-image: url(img/version_control.png) background-size: cover # .fancy[Version Control] --- name: vc1 # Version Control -- All codes should use version control -- Even useful in other fields (writing/editing, anybody who makes presentations or has a resume) -- Rudimentary strategy is a terrible idea (i.e., code.sas, code_v2.sas, code_new.sas, code_old.sas...) -- Use software like git <img src="img/git.png" width="5%"/> , Subversion (svn), Mercurial, etc. [Youtube tutorial](https://www.youtube.com/watch?v=HVsySz-h9r4) -- You will hit some learning curve, but the basics can be taught within 1 hour -- Makes sharing easier -- Saves you over and over (story time…) --- name: vc2 # github <img src="img/github.png" width="5%"/> -- <img src="img/oz_github.jpg" width="85%" style="display: block; margin: auto;" /> -- You can learn how to use github from this youtube link below [Youtube tutorial](https://www.youtube.com/watch?v=xuB1Id2Wxak) --- name: Further Reading # Further Reading .pull-left[ <img src="img/clean_code_book.png" width="75%" style="display: block; margin: auto;" /> ] .pull-right[ <img src="img/sas_book.png" width="75%" style="display: block; margin: auto;" /> ] --- # Further Reading .pull-left[ <img src="img/advance_r.png" width="75%" style="display: block; margin: auto;" /> ] .pull-right[ <img src="img/git_book.png" width="75%" style="display: block; margin: auto;" /> ] --- class: center, middle # Thanks!