Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Five Lines of Code: Why five lines?

 
Ranch Hand
Posts: 57
1
Redhat Notepad Fedora Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Christian,

I have two questions -

[1]
By quickly scanning some chapters, all the coding examples presented in your book are written in TypeScript. Any particular reason for choosing that language?

[2]
You made a statement that a method should not contain more than five lines (excluding opening and closing brackets) and the title of your book reflects that. Why do you think five lines are enough for a single method?

Thanks and have a great day!
 
Rancher
Posts: 480
7
Android Tomcat Server Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You made a statement that a method should not contain more than five lines (excluding opening and closing brackets) and the title of your book reflects that. Why do you think five lines are enough for a single method?


There a very good explanation in 3.1. Our First Rule, Why Five Lines
 
Author
Posts: 31
8
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Lucian,

Great questions. I'll try to answer them as best I can.

[1]
There are three main reasons I chose TypeScript for this book:

First, my background is in software quality, and using types is one of the strongest ways to reduce errors. TypeScript has one of the strongest type systems I know of. Even though I do not make use of advanced types in the book, it means there is plenty of room to grow within TypeScript.

Second, through my work as a consultant I see mainly four languages being used: Java, C#, C/C++, and JavaScript. TypeScript has a syntax that is easy to read no matter which of these you come from; it's mainly about loops, ifs, arrays, objects, and methods. The principles in the book applies to all object oriented languages.

Third, I wanted an overarching example in part 1 of the book that was not too small, and written in the same style as the code I encounter in the industry. I also wanted it to be visual, as that would make it easier and more fun to validate that nothing had broken. When looking at the languages mentioned above TypeScript has the least overhead in getting some graphical up and running, because it runs in the browser.

[2]
I landed on five lines for multiple reasons. As the book states the number of lines should correspond to what is required to do a linear search through your data structure. In the book this is a 2D array, thus we need five lines.

Having small methods is nice in itself. The focus of the book is teaching refactoring, and in that perspective five lines does an excellent job of forcing us to learn to move code around and break it down. Pretending it is set in stone while learning forces us into difficult situations where we need to learn new skills and techniques to comply with the rules. Ie. if you have an if with five "else if"s how do you get that below five lines? I am a big fan of this tangible form of motivation.

All the rules are stated very absolute, then their description is a bit more nuanced, and finally the code smells are even more fuzzy. This allows people to grow with the rules, relying first on their names, then learning some of the exceptions, and finally understanding their intention. "Five lines" is no exception.

I do not think five lines is some universal rule, but I have found it to be an excellent teaching tool. That being said my methods in real code tend to average around five lines.


I hope this answers your questions, otherwise, you can find more detail about both questions in the book in these sections:
Answer to [1]: 1.5.1. Programming Language: TypeScript
Answer to [2]: 3.1. Our First Rule, Why Five Lines

Have a great day.

Christian
 
Lucian Maly
Ranch Hand
Posts: 57
1
Redhat Notepad Fedora Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interesting points Chris! I am also working as a consultant (but the truth is i'm mostly infrastructure focused) and use more Python and Golang. But i understand where you are going with this reasoning...

What you said will definitely motivate me to think more about the size of my methods, thanks.
 
Marshal
Posts: 15887
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the Five Lines of Code rule is a great "finger in the wind" way for beginners to get started with refactoring. Actually, it's a great insight into what keeps developers from refactoring. Principles like SOLID and even KISS take a while to internalize because there are so many ways to violate them. A rule like FLoC (I think that's what I'll call it from now on) is a good context-free way to get you started.
 
them good ole boys were drinking whiskey and rye singin' this'll be the day that I die. Drink tiny ad.
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic