User Tools

Site Tools


guides:programstyle:functions

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
guides:programstyle:functions [2020/08/04 15:32] – [Repeated Code] wikiadminguides:programstyle:functions [2024/07/25 15:01] (current) – external edit 127.0.0.1
Line 50: Line 50:
 ===== Function Length ===== ===== Function Length =====
  
 +Functions should be viewable on a single screen.  The programmer should be able to read all, or nearly all of a function at the same time.
 +
 +A standard ascii terminal is 24 lines high, so this is a reasonable goal for function length.  
 +
 +Functions should rarely exceed 50 lines.  
 ===== Function Cohesion ===== ===== Function Cohesion =====
  
-===== The main function =====+There are two aspects of function cohesion: 
 + 
 +   - The code in a function should perform a single well-defined task. 
 +   - The code in a function should be at the same level of abstraction. 
 + 
 +In the first case, this may be a simple or very complex task but it is a single task. 
 + 
 +In the second case, the program should consist  of either high level (other function calls) or low level (computation, input, ...) actions.  High level routines should read like an algorithm. 
 + 
 +===== The main Function ===== 
 + 
 +The main function for most advanced programs should read like an algorithm to solve the main problem.  It should not contain detailed code, but consist of a number of calls to functions. 
 + 
 +A single line main function is not acceptable.   
  
 +===== An Example ======
  
guides/programstyle/functions.1596555161.txt.gz · Last modified: 2024/07/25 15:01 (external edit)