This query enforces a concise coding style by checking the names of parameters within routines (such as functions or procedures). It finds parameters whose names unnecessarily repeat the name of the routine they belong to. For example, in a function named calculate_invoice, a parameter named calculate_invoice_id would be flagged, as invoice_id is sufficient. A routine cannot have two parameters with the same name, so the shorter name is unambiguous within the context of the routine and results in cleaner, more readable code.
Notes
The query does not consider the routines that are a part of an extension. The query uses bold (b) tags to highlight the parts of parameter names that violate the suggestion. In the output data the query removes from the end of the routine name the numbers, which represent the object identifier of the routine in the system catalog.
Type
Problem detection (Each row in the result could represent a flaw in the design)
Drop the routine and recreate it with the improved parameter names. Use a consistent style of naming.
Data Source
INFORMATION_SCHEMA+system catalog
SQL Query
SELECT specific_schema AS routine_schema,
regexp_replace(specific_name,'_[0-9]*$','') AS routine_name,
pg_get_function_identity_arguments(translate(substring(specific_name,'_[0-9]+$'),'_','')::int::oid) AS parameters,
regexp_replace(parameter_name, regexp_replace(specific_name,'_[0-9]*$',''), '' || regexp_replace(specific_name,'_[0-9]*$','') || '','g') AS suspected_parameter
FROM information_schema.parameters
WHERE specific_schema NOT IN (SELECT schema_name
FROM INFORMATION_SCHEMA.schemata
WHERE schema_name<>'public' AND
schema_owner='postgres' AND schema_name IS NOT NULL)
AND parameter_name ~* ('^' || regexp_replace(specific_name,'_[0-9]*$','') || '.{0,1}_')
AND NOT EXISTS (SELECT 1
FROM pg_catalog.pg_depend d inner join pg_catalog.pg_proc pc ON d.objid=pc.oid
WHERE EXISTS (SELECT 1 FROM pg_catalog.pg_extension e WHERE d.refobjid=e.oid) AND
pc.proname || '_' || pc.oid = parameters.specific_name)
ORDER BY routine_schema, routine_name, parameters, parameter_name;
Collections
This query belongs to the following collections:
Name
Description
Find problems automatically
Queries, that results point to problems in the database. Each query in the collection produces an initial assessment. However, a human reviewer has the final say as to whether there is a problem or not .
Categories
This query is classified under the following categories:
Name
Description
Naming
Queries of this category provide information about the style of naming.
User-defined routines
Queries of this category provide information about the user-defined routines
Further reading and related materials:
Reference
The corresponding code problem in case of cleaning code is "Don’t Add Gratuitous Context". (Robert C. Martin, Clean Code)