Skip to content

Distance optimization

Zuy Alexey edited this page Aug 14, 2016 · 2 revisions

General ideas of distance optimization

The general idea of distance optimization is to place methods in some order that lets developer faster understand code. This can be achieved by placing methods in order of necessity, so that developer don`t need to search for method location in class and can just read source code as “narrative story”.

Ordering criterias

Dependent methods should be placed close to each other. Methods that call one another should be placed in order of invocation. That is, if method a() calls b() and c() in that order it should be located in class in that order. Caller method must precede callee whenever possible. Semantically related methods, such as override methods, overloaded methods and accessor methods should be grouped.

Penalty function

Penalty function if formalization of ordering criterias. For any given methods orderings one with the least penalty function value is considered the “best” ordering. Penalty function is defined as:

F = D + R*kR + A*kA + L*kL + P*kP + Q*kQ + S*ks

where

  • D - total sum of distances between dependent methods in amount of methods that separate them;

  • R - override methods group split cases;

  • A - accessor methods split cases;

  • L - overload methods split cases;

  • P - amount of cases when method located before it`s first invocation;

  • Q - amount of methods relative order inconsistency cases;

  • S - amount of cases when called method located at distance that forces user to scroll screen;

  • k* - is just coefficients;

Clone this wiki locally