Skip to content

sniperadmin/psb-analyzer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 

Repository files navigation

Golden Ratio Balance Code Analyzer

It is a fun exercise to design a good engineering metric that captures the idea of a "Golden Ratio" of code complexity and performance! Though there exists no such software engineering metric that has been widely accepted, we can develop a heuristic metric that tries to measure the trade-off between the structural complexity of a code and its speed. This translation is in line with the spirit of the Golden Ratio, the signature of harmonious proportions and ideal balance in nature and design, and echoes the Islamic ideals of Mizan (balance) and I'tidal (moderation).

I've added a Performance-Simplicity Balance (PSB) Ratio to the "Code Performance Analyzer" prototype. The ratio seeks to provide you with an intuitive feeling for how "harmonious" your code is by comparing the measure of complexity of your code to its execution time.

Below is how the Performance-Simplicity Balance (PSB) Ratio is defined and computed in the prototype:

Code Complexity Score (CCS): This is a simplified metric of your code's structural complexity. It is computed as:

text{CCS} = {Lines of Code (LOC)} + ({Control Flow Count (CFC)} * {Control Flow Weight})

Lines of Code (LOC): Simply counts the number of non-blank lines in your snippet. Control Flow Count (CFC): Counts how many of the standard control flow keywords (if, for, while, switch, try, catch) are present. These are generally branching or looping logic and increase complexity. Control Flow Weight: A configurable multiplier (which defaults to 5) to allow control flow structures to count more. Execution Time (ET): The amount of time your code snippet executes in, in milliseconds.

PSB Ratio: Then the simple measure is given by:

{PSB Ratio} = {Code Complexity Score (CCS)} / {Execution Time (ET)}

Interpretation:

A larger PSB Ratio indicates your code has more "complexity per millisecond." This may be extremely fast code that happens to be very complex, or very slow code that is actually highly complex. A smaller PSB Ratio indicates less "complexity per millisecond." This may be very simple code that is also very fast, or simple code that happens to be rather slow.

The "Golden Zone": We'd like a ratio that establishes an ideal balance. If your PSB Ratio approaches the Golden Ratio (phi) ~= approx 1.618 (i.e., within $ \pm 0.2 $), it indicates a possible "harmonious" balance in which the structural effort (complexity) of the code is proportionally related to its performance. This is a heuristic interpretation, not a law. This prototype offers a concrete means of experimenting with these concepts and seeing how modifications to your code's performance and shape impact this test "balance" measure.

How to Use the Prototype: Paste Code: Paste your JavaScript code snippet into the big text box. Adjust Weight: You can modify the "Control Flow Weight" to observe its impact on the complexity score and the subsequent PSB Ratio. A larger weight implies control flow statements are given greater contribution towards perceived complexity. Run Analysis: Click "Run and Analyze Balance."

Review Results:

  • Execution Time: Displays how quickly your code executed.
  • Lines of Code (LOC): A simple code size measure.
  • Control Flow Statements: Counts if, for, while, switch, try, catch keywords.
  • Code Complexity Score: Complexity calculated based on LOC and control flow.
  • PSB Ratio: The calculated ratio.
  • Interpretation: A colored box will inform you whether your code is in the "Golden Zone," "Warning Zone," or "Danger Zone" depending on its PSB Ratio compared to the Golden Ratio.

This prototype offers a beginning for considering code quality and performance in a more integrated, balanced manner, instead of pure speed or fewest lines of code. It is an experiment in how abstract notions of harmony can be used, even heuristically, to address real engineering issues.

About

Performance-Simplicity Balance (PSB) Ratio Analyzer. This Program does analysis using the Golden Ratio 1.618

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages