logic-in-entry-point
An entry point does work itself instead of handing it off.
- Group
- Code
- Runs
- Optional: run it with
ur lint --rule logic-in-entry-point - Level
- warning
- Reads
- Every file
- Fix
- None: where the work goes, and what the new function is called, is a design choice
Why
An entry point, the function or script a program starts at or a function that handles one of its commands, should parse the program's arguments and hand them to code that does the work, which can then be tested and reused. Ur finds entry points from each language's facts and the project's manifests: Rust's main in src/main.rs, src/bin, or a [[bin]] path, functions taking a type that derives clap's Parser, Args, or Subcommand, Python's if __name__ == "__main__":, __main__.py, and [project.scripts], the files bin names in package.json, Ruby's exe and bin, Java's and C++'s main, PHP's bin and composer.json bin, and shell scripts in bin or starting with #!, which it finds but does not measure, since a shell script is its whole program. A same-file function that only the entry point calls counts as part of it. What is left once argument parsing is set aside is reported when its loops and branches other than dispatch to a handler number more than four, or more than two when it also has two kinds of effect, such as file access and output, or when it calls into more than six subsystems of the program. Leaving the process is not counted: that is an entry point's to do.
The fix, as Ur suggests it: parse the arguments here and hand the work to a function elsewhere.
For agents
Keep each entry point to parsing its arguments and handing them to a function elsewhere that does the work; put loops, decisions, and effects in that function.
Example
.ur/ur.toml[lint.logic-in-entry-point]
billing/__main__.pyimport argparse
import json
def main():
parser = argparse.ArgumentParser()
parser.add_argument("orders")
args = parser.parse_args()
with open(args.orders) as handle:
orders = json.load(handle)
total = 0
for order in orders:
if order["status"] == "cancelled":
continue
if order["express"]:
total += order["price"] * 2
else:
total += order["price"]
print(f"total: {total}")
if __name__ == "__main__":
main()
$ ur lint --rule logic-in-entry-point
warning[logic-in-entry-point]: The block this guard runs is an entry point, run when the module runs as the program, and does work itself, with `main`, which only it calls: 1 loop and 2 branches that are not dispatch; file, output effects; parse the arguments here and hand the work to a function elsewhere
--> ./billing/__main__.py:22:1 guard entry point
= step 1: ./billing/__main__.py:12:5-18 loop
= step 2: ./billing/__main__.py:13:9-14 branch
= step 3: ./billing/__main__.py:15:9-18 branch
= step 4: ./billing/__main__.py:9:10 file `open`
= step 5: ./billing/__main__.py:19:5 output `print`
= hint: parse the arguments here and hand the work to a function elsewhere
= see: Clean Code: A Handbook of Agile Software Craftsmanship, chapter 11, Systems: Separation of Main, Robert C. Martin (2008-08), https://www.oreilly.com/library/view/clean-code-a/9780136083238/
0 clone classes and 1 `logic-in-entry-point` finding in 1 file
Source
Clean Code: A Handbook of Agile Software Craftsmanship, chapter 11, Systems: Separation of Main, Robert C. Martin (2008-08).