{"id":181,"date":"2017-05-31T08:00:01","date_gmt":"2017-05-31T12:00:01","guid":{"rendered":"http:\/\/langstonsoftware.com\/?p=181"},"modified":"2024-01-30T19:09:16","modified_gmt":"2024-01-31T00:09:16","slug":"why-put-single-lines-of-code-into-a-separate-function","status":"publish","type":"post","link":"https:\/\/langstonsoftware.com\/2017\/05\/31\/why-put-single-lines-of-code-into-a-separate-function\/","title":{"rendered":"Why put single lines of code into a separate function?"},"content":{"rendered":"
When I wrote previously about refactoring<\/a> I introduced the clean coding concepts of creating a table of contents<\/strong>, removing duplicated instructions<\/strong>, and never remembering more than is required<\/strong>.<\/p>\n There often isn’t much disagreement from developers over removing duplicated instructions<\/strong>. Never remembering more than is required<\/strong> sometimes conflicts with style guides that ask for all variable declarations at the top of a function, but they are counteracted by use of shorter functions to limit variable scopes.<\/p>\n Creating a table of contents<\/strong> seems to be the one that causes the most disagreement, starting when the whole routine falls within a screen and raising to the highest pitch when our logic consists of a single line of code. There are heated debates over refactorings that encapsulate simple logic statements within a function that is only called once.<\/p>\n But sometimes, it is still the right thing to do.<\/p>\n Students and beginners often fail to see the point, feeling that this addition of helper functions makes the code unreadable or at least unwieldy. It is true that at the byte code level a function is merely a GOTO statement, and terrible spaghetti code can be created via a breeding ground of many functions. But academic instructors also often leverage very plain text editing software in coursework, which exacerbates the notion that nested functions can be hard to read. In modern Integrated Development Environments a.k.a IDEs (by which I include Vim, Emacs and other powerful “text” editors with all their related developer plugins), you can jump or peek at function declarations easily. If the function is short and only called once, declare it directly under or above the caller, and it can appear on the same screen.<\/p>\n But beyond this, the student is really forgetting in business you will not be in the details of your application most of the time. You will be reading, rewriting, or re-architecting it to fix and adapt it more often. You’ll have other people’s code to read and they will be reading yours. For these reasons you need to optimize your code for reading at a higher level. You use automated tests to ensure lower level functions and components operate as expected without actually reading the logic directly. This is why books like Clean Code<\/a> make a point of asking for what students feel are “extremely” short methods.<\/p>\n Finally, a function should have a single purpose and be named correspondingly. This is invaluable for knowing where to look for bugs, where code changes should go, and when your design is not adequate for the task. In introductory learning programs this is not important because specifications are relatively clear, programs rarely continue to be used after the assignment is due, and requirements almost never change. In business you will often learn the specifications by making a prototype and receiving unclear feedback, the programs will be used longer than you are at the company, and the requirements will change rapidly and often. When you are learning bugs are quashed in single all night coding session, they don’t wake you up at 3 am costing your company thousands or millions of dollars for every minute they go unfixed. Bugs in homework assignments won’t cost you your job and reputation if you don’t have a speedy fix.<\/p>\nWhy refactor the smallest details? Readability, Reuse, Re-requirements<\/h3>\n
\nLet’s be clear about perspective. What we’re arguing for is very small individual improvements to code. Taken in one at a time, they are insignificant. But good programming practices pay off in aggregate, compounding their value every time the code is read, reused, or requirements change across an entire network of connected applications. They also pay off in the writing, becoming more natural and easier to apply with consistent practice. Yes, there is a cost, but it is one worth paying, at least in regular small installments every time you revisit a section of code.<\/p>\nWhen to refactor the smallest details? Comments, Calculations, Concrete implementations<\/h3>\n