Answers.Body: <p>There is an issue beyond screen size that is important to consider. In fact, &quot;screen size&quot; seems awfully long and would be for the (Java, Python,,,) code that I write. Anything longer than five lines or so is too much and much (not all - see below) of my code is like that. But, it isn't artificially broken up just to be short. Each method captures a single idea and the code isn't complex (no/few nested structures for example). The complexity is in the interactions of objects.</p>
  <p>The real problem is that our minds have a limited ability to juggle deep complexity with many parts. But deeply nested code structures are like that, so writing short(er) methods makes it harder to exceed the <a href="https://en.wikipedia.org/wiki/The_Magical_Number_Seven%2C_Plus_or_Minus_Two" rel="nofollow noreferrer">magic rule of seven</a>. Then keeping a method to a single page (actually much less) is easy and natural.</p>
  <p>So, I would state a somewhat different rule for my students: minimize the nesting of code in any given method. If it is more than two it starts to be harder to grok and easier to introduce errors. That is also the <a href="https://en.wikipedia.org/wiki/KISS_principle" rel="nofollow noreferrer">KISS principle</a>.</p>
  <p>So, while it is useful to be able to see a method all at once without scrolling, the more important thing is that you can <em>understand</em> the method yourself without needing to take notes or re-consider or wander off into dead ends.</p>
  <hr />
  <p>Some code doesn't work that way, however. The logic of a program can usually be constructed with (very) short methods using modern <a href="https://en.wikipedia.org/wiki/Software_design_pattern" rel="nofollow noreferrer">design patterns</a>.</p>
  <p>But GUI code, depending on pre-built libraries is often long and fairly boring. There may not be much structure to the code, but it can require long sequences of statements to build a user interface. That code can be long (pages, perhaps) but is easy to understand.</p>
  <hr />
  <p>Note (joke) about short methods. Have a team of 10 people write a 1000 line program by having each person write 100 lines, independently. Person A writes the first 100 lines while person B writes the next 100 lines, etc, all together.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-11-27T12:48:38.127Z
Answers.LastActivityDate: 2023-11-27T12:48:38.127Z
Answers.UserDisplayName: Buffy
Answers.Body: <p>There are lots of different coding standards. Typically, every workplace and project tends to have its own. So, &quot;fit within a single screen&quot; is not a universally-adopted guideline, but something that can quite plausibly be chosen regarding method size and complexity.</p>
  <blockquote>
  <p>While I understand the importance of crafting concise and
  straightforward functions that can be easily comprehended at a glance, [...]</p>
  </blockquote>
  <p>That's the point. If you allow for methods to exceed the screen size, it's no longer possible to even see all of it at once.</p>
  <p>Of course, &quot;screen size&quot; is hard to define. Which screen resolution? Which font and size and line spacing? How much space dedicated to task bar, window frame, toolbar and so on?</p>
  <p>It's all about readability, and these rules should be taken as guidelines. Typically, in a method that spans e.g. 100 lines of code, you can identify subtasks and give them names that describe the task, i.e. to introduce methods for these subtasks.</p>
  <p>Or from another point of view:</p>
  <p>By introducing classes and methods, we create the problem-specific language that we want to use when writing our higher-level solutions. Don't take the language as a fixed thing. No, every class and method that you write adds a word to the vocabulary of your problem-specific language, and it's your choice what that language finally looks like.</p>
  <p>So, if there is a method that you do not like (e.g. because it is too long or otherwise too hard to understand), then:</p>
  <ul>
  <li><p>Either this method in itself does too much. As a hint, look at its name. Is that a word that you would like to have in your vocabulary, expressing some concise action? Or is it a mess of things that just accidentally happen one after another in a given program?</p>
  </li>
  <li><p>Or the steps necessary to fulfill its jobs are just so complicated that they cannot be expressed in a few lines, using the language at hand. If you were free to invent your own language, how would you write the method? It's quite possible that you can come close to that language, by introducing the appropriate methods and letting the method under consideration call them.</p>
  </li>
  </ul>
  <p>To your concrete questions:</p>
  <ul>
  <li><p>A switch-case condition with multiple lines of code, I'd break down in one method call per case. E.g. in a <code>paint()</code> method that switches on type, the methods might become <code>paintRectangle()</code>, <code>paintLine()</code>, <code>paintCircle()</code> and so on. Then the <code>paint()</code> method still has the switch-case, but the individual blocks are only one-liners now, with a name that clearly states their purpose.</p>
  </li>
  <li><p>If you need substantial handcoded data, make sure the array/structure/whatever gets a name, either by making the data a constant or a read-only static field (whatever your programming language calls it) or the result of a <code>createMyHandcodedData()</code> method. And if it isn't obvious why the data have exactly the content you handcoded, explain it in a comment.</p>
  </li>
  </ul>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-11-27T12:28:42.137Z
Answers.LastActivityDate: 2023-11-27T12:28:42.137Z
Answers.UserDisplayName: Ralf Kleberhoff
Body: <p>According to a computer science teacher, it is beneficial to create methods that fit within a single screen. While I understand the importance of crafting concise and straightforward functions that can be easily comprehended at a glance, the question arises: why is the limit set at precisely one screen? How should one approach writing a switch-case condition with multiple lines of code or handling substantial hardcoded data?</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2023-11-27T11:20:07.147Z
Comments.Text: One size doesn't fit all. What matters is legibility. and sometimes creating more space helps.
Comments.UserDisplayName: A.bakker
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2023-11-27T20:23:05.170Z
Comments.Text: I haven't heard the "one screen" function size limit recommendation before. This seems pretty arbitrary. Nowadays, with high res vertical monitors, "one screen" can probably fit 500 lines of code or more. In general, it's good to approach any sort of one-size-fits all rule like this with suspicion. Regarding the switch-case question, usually use a lookup table in whatever language you're using, preferably populated by raw data (JSON, typically). But it depends on your specific use case. switch-cases may be calling functions rather than accessing simple data.
Comments.UserDisplayName: ggorlen
Tags: <programming>
Title: Why is it considered undesirable for a function to exceed the dimensions of a single screen?
ThreadText: # Why is it considered undesirable for a function to exceed the dimensions of a single screen?
  
  (Asked by: username_0 on 2023-11-27)
  
  According to a computer science teacher, it is beneficial to create methods that fit within a single screen. While I understand the importance of crafting concise and straightforward functions that can be easily comprehended at a glance, the question arises: why is the limit set at precisely one screen? How should one approach writing a switch-case condition with multiple lines of code or handling substantial hardcoded data?
  
  **Tags:** programming
  
  > **Comments:**
  > **username_1**: One size doesn't fit all. What matters is legibility. and sometimes creating more space helps.  
  > **username_2**: I haven't heard the "one screen" function size limit recommendation before. This seems pretty arbitrary. Nowadays, with high res vertical monitors, "one screen" can probably fit 500 lines of code or more. In general, it's good to approach any sort of one-size-fits all rule like this with suspicion. Regarding the switch-case question, usually use a lookup table in whatever language you're using, preferably populated by raw data (JSON, typically). But it depends on your specific use case. switch-cases may be calling functions rather than accessing simple data.  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_4 on 2023-11-27. Score: 3
  
  There are lots of different coding standards. Typically, every workplace and project tends to have its own. So, "fit within a single screen" is not a universally-adopted guideline, but something that can quite plausibly be chosen regarding method size and complexity.
  
  > While I understand the importance of crafting concise and
  > straightforward functions that can be easily comprehended at a glance, [...]
  
  That's the point. If you allow for methods to exceed the screen size, it's no longer possible to even see all of it at once.
  
  Of course, "screen size" is hard to define. Which screen resolution? Which font and size and line spacing? How much space dedicated to task bar, window frame, toolbar and so on?
  
  It's all about readability, and these rules should be taken as guidelines. Typically, in a method that spans e.g. 100 lines of code, you can identify subtasks and give them names that describe the task, i.e. to introduce methods for these subtasks.
  
  Or from another point of view:
  
  By introducing classes and methods, we create the problem-specific language that we want to use when writing our higher-level solutions. Don't take the language as a fixed thing. No, every class and method that you write adds a word to the vocabulary of your problem-specific language, and it's your choice what that language finally looks like.
  
  So, if there is a method that you do not like (e.g. because it is too long or otherwise too hard to understand), then:
  
  * Either this method in itself does too much. As a hint, look at its name. Is that a word that you would like to have in your vocabulary, expressing some concise action? Or is it a mess of things that just accidentally happen one after another in a given program?
  * Or the steps necessary to fulfill its jobs are just so complicated that they cannot be expressed in a few lines, using the language at hand. If you were free to invent your own language, how would you write the method? It's quite possible that you can come close to that language, by introducing the appropriate methods and letting the method under consideration call them.
  
  To your concrete questions:
  
  * A switch-case condition with multiple lines of code, I'd break down in one method call per case. E.g. in a `paint()` method that switches on type, the methods might become `paintRectangle()`, `paintLine()`, `paintCircle()` and so on. Then the `paint()` method still has the switch-case, but the individual blocks are only one-liners now, with a name that clearly states their purpose.
  * If you need substantial handcoded data, make sure the array/structure/whatever gets a name, either by making the data a constant or a read-only static field (whatever your programming language calls it) or the result of a `createMyHandcodedData()` method. And if it isn't obvious why the data have exactly the content you handcoded, explain it in a comment.
  
  
  ---
  
  ### Answer by username_3 on 2023-11-27. Score: 1
  
  There is an issue beyond screen size that is important to consider. In fact, "screen size" seems awfully long and would be for the (Java, Python,,,) code that I write. Anything longer than five lines or so is too much and much (not all - see below) of my code is like that. But, it isn't artificially broken up just to be short. Each method captures a single idea and the code isn't complex (no/few nested structures for example). The complexity is in the interactions of objects.
  
  The real problem is that our minds have a limited ability to juggle deep complexity with many parts. But deeply nested code structures are like that, so writing short(er) methods makes it harder to exceed the [magic rule of seven](https://en.wikipedia.org/wiki/The_Magical_Number_Seven%2C_Plus_or_Minus_Two). Then keeping a method to a single page (actually much less) is easy and natural.
  
  So, I would state a somewhat different rule for my students: minimize the nesting of code in any given method. If it is more than two it starts to be harder to grok and easier to introduce errors. That is also the [KISS principle](https://en.wikipedia.org/wiki/KISS_principle).
  
  So, while it is useful to be able to see a method all at once without scrolling, the more important thing is that you can *understand* the method yourself without needing to take notes or re-consider or wander off into dead ends.
  
  ---
  
  Some code doesn't work that way, however. The logic of a program can usually be constructed with (very) short methods using modern [design patterns](https://en.wikipedia.org/wiki/Software_design_pattern).
  
  But GUI code, depending on pre-built libraries is often long and fairly boring. There may not be much structure to the code, but it can require long sequences of statements to build a user interface. That code can be long (pages, perhaps) but is easy to understand.
  
  ---
  
  Note (joke) about short methods. Have a team of 10 people write a 1000 line program by having each person write 100 lines, independently. Person A writes the first 100 lines while person B writes the next 100 lines, etc, all together.
  
  
  ---
  
  