Perl::...plexity(3pm)

🌸 NAME 🌸

okay so this is the Perl::...plexity(3pm) page from User Contributed Perl Documentation, and the policy it's serving is Perl::Critic::Policy::Modules::ProhibitExcessMainComplexity β€” its whole thing is minimize complexity in code that is outside of subroutines. slay, this policy ate and left no crumbs.

🫢 AFFILIATION 🫢

this Policy is part of the core Perl::Critic distribution. no cap, it's built in. periodt.

✨ DESCRIPTION ✨

all else being equal, complicated code is more error-prone and more expensive to maintain than simpler code. literally the first step towards managing complexity is to establish formal complexity metrics. one such metric is the McCabe score, which describes the number of possible paths through a block of code. this Policy approximates the McCabe score by summing the number of conditional statements and operators within a block of code. research has shown that a McCabe score higher than 20 is a sign of high-risk, potentially untestable code β€” it's giving red flag energy, bestie. see <http://en.wikipedia.org/wiki/Cyclomatic_complexity> for some discussion about the McCabe number and other complexity metrics. πŸ¦‹

so here's the vibe check: Perl::Critic::Policy::Subroutines::ProhibitExcessComplexity scores the complexity of each subroutine, whereas this Policy scores the total complexity of all the code that is outside of any subroutine declaration. iykyk.

the usual prescription for reducing complexity is to refactor code into smaller subroutines. the Mark Dominus book "Higher Order Perl" also describes callbacks, recursion, memoization, iterators, and other techniques that help create simple and extensible Perl code. obsessed with a codebase that stays simple and extensible, fr fr.

πŸŽ€ CONFIGURATION πŸŽ€

the maximum acceptable McCabe score can be set with the max_mccabe configuration item. if the sum of all code outside any subroutine has a McCabe score higher than this number, it will generate a Policy violation. the default is 20. here's an example section for a .perlcriticrc:

[Modules::ProhibitExcessMainComplexity]
max_mccabe = 30

πŸ’… NOTES πŸ’…

"Everything should be made as simple as possible, but no simpler." -- Albert Einstein

complexity is subjective, but formal complexity metrics are still incredibly valuable. every problem has an inherent level of complexity, so it is not necessarily optimal to minimize the McCabe number. so don't get offended if your code triggers this Policy β€” just consider if there might be a simpler way to get the job done. 🫢 main character energy is knowing when to refactor, ngl.

πŸ‘‘ SEE ALSO πŸ‘‘

  • Perl::Critic::Policy::Subroutines::ProhibitExcessComplexity

🌈 AUTHOR 🌈

Jeffrey Ryan Thalhammer <jeff@imaginative-software.com>

Copyright (c) 2005-2011 Imaginative Software Systems. All rights reserved.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. The full text of this license can be found in the LICENSE file included with this module.


okay bestie, that's the whole page πŸŽ€ β€” this was Perl::...plexity(3pm) from User Contributed Perl Documentation, perl v5.42.0, dated 2024-10-23. iconic. bye slay πŸ’–

πŸ“¦ where this page comes from