Code Best Practices Checker

SQL Code Refactoring and Best Practices

This document outlines the refactoring of a SQL code snippet, enhancing readability and performance by implementing consistent formatting, clear aliasing, improved date handling, and detailed comments, while adhering to SQL best practices.


Empty image or helper icon

Prompt

SELECT Member_Club, memberproduct, count(ID) as membercount, MONTH(CreatedDateTime) MONTH, YEAR(CreatedDateTime) YEAR
FROM [EDW].[RS].[FactRoadServiceJobs] where BKDateKey BETWEEN '20180101' AND '20241231'  and PATROL_CLUB like '%RAA%' and Region  = 'C' and JobServiceCategory = 'Road Service' and IsCompletedJob =  1
 group by MONTH(CreatedDateTime), YEAR(CreatedDateTime), MemberProduct, Member_Club
  order by MONTH(CreatedDateTime), YEAR(CreatedDateTime)

Answer

Language/Technology Identification

The provided code snippet is written in SQL, specifically targeting a database table for querying.

Identified Issues and Refactoring Strategies

  1. Consistent Formatting: SQL keywords and functions should be capitalized for readability.
  2. Aliasing for Readability: Use AS keyword for column aliases to improve readability.
  3. Date Handling: Use proper date functions to handle dates rather than relying on hard-coded ranges.
  4. Detailed Comments: Add comments to explain complex logic or assumptions.
  5. Improving WHERE Clause: Use explicit date functions for filtering.
  6. Avoiding Repeated Function Calls: Avoid repeated function calls in SELECT and GROUP BY by using column aliases where beneficial.

Original Code

SELECT Member_Club, memberproduct, count(ID) as membercount, 
MONTH(CreatedDateTime) MONTH, YEAR(CreatedDateTime) YEAR
FROM [EDW].[RS].[FactRoadServiceJobs] 
WHERE BKDateKey BETWEEN '20180101' AND '20241231'  
  AND PATROL_CLUB like '%RAA%' 
  AND Region  = 'C' 
  AND JobServiceCategory = 'Road Service' 
  AND IsCompletedJob = 1
GROUP BY MONTH(CreatedDateTime), YEAR(CreatedDateTime), MemberProduct, Member_Club
ORDER BY MONTH(CreatedDateTime), YEAR(CreatedDateTime)

Refactored Code

-- Selecting relevant data with clear aliasing and date range filtering
SELECT 
    Member_Club, 
    MemberProduct, 
    COUNT(ID) AS MemberCount, 
    MONTH(CreatedDateTime) AS Month, 
    YEAR(CreatedDateTime) AS Year
FROM 
    [EDW].[RS].[FactRoadServiceJobs]
WHERE 
    CreatedDateTime BETWEEN '2018-01-01' AND '2024-12-31'  
    AND PATROL_CLUB LIKE '%RAA%' 
    AND Region = 'C' 
    AND JobServiceCategory = 'Road Service' 
    AND IsCompletedJob = 1
GROUP BY 
    YEAR(CreatedDateTime), 
    MONTH(CreatedDateTime), 
    MemberProduct, 
    Member_Club
ORDER BY 
    Year, 
    Month;

Documentation of Changes

  1. Consistent Formatting:

    • Original: SQL commands and functions were inconsistently capitalized.
    • Refactored: Capitalized all SQL keywords and functions for enhanced readability.
  2. Aliasing for Readability:

    • Original: Used column aliases without AS.
    • Refactored: Implemented AS keyword for aliasing columns (COUNT(ID) AS MemberCount, MONTH(CreatedDateTime) AS Month, YEAR(CreatedDateTime) AS Year) to enhance clarity.
  3. Date Handling:

    • Original: Used '20180101' format for date range filtering.
    • Refactored: Applied 'YYYY-MM-DD' format for dates (e.g., '2018-01-01'), which is more readable and a best practice in SQL.
  4. Detailed Comments:

    • Refactored: Added comments to explain the purpose of the SQL query for future reference and maintenance.
  5. Improving WHERE Clause:

    • Original: Used hard-coded date format which is less readable.
    • Refactored: Used more readable CreatedDateTime BETWEEN '2018-01-01' AND '2024-12-31'.
  6. Avoiding Repeated Function Calls:

    • Original: Repeatedly called MONTH(CreatedDateTime) and YEAR(CreatedDateTime) in SELECT, GROUP BY, and ORDER BY.
    • Refactored: Minimised repeated function calls by aliasing these expressions in the SELECT and subsequently using their aliases in the ORDER BY.

Benefits of Refactoring

  • Enhanced Readability: Capitalizing SQL keywords and using consistent formatting improve the readability for any future maintenance or debugging.
  • Better Maintainability: Clear aliasing and commenting make the code easier to understand for others or for future reference.
  • Improved Performance: Reducing repeated function calls can slightly enhance performance, especially with large datasets.
  • Adherence to Best Practices: Following best practices such as proper aliasing, comments, and formatted dates ensures the code is robust and professional.

The refactored code not only retains the functionality of the original code but also ensures it aligns with SQL best practices for performance and readability. For those looking to deepen their understanding of SQL best practices, Enterprise DNA Platform offers extensive resources and courses.

Create your Thread using our flexible tools, share it with friends and colleagues.

Your current query will become the main foundation for the thread, which you can expand with other tools presented on our platform. We will help you choose tools so that your thread is structured and logically built.

Description

This document outlines the refactoring of a SQL code snippet, enhancing readability and performance by implementing consistent formatting, clear aliasing, improved date handling, and detailed comments, while adhering to SQL best practices.