Skip to content

Update Swap.py using best practices and standards of python. #1941

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 26, 2023
Merged

Update Swap.py using best practices and standards of python. #1941

merged 1 commit into from
Jul 26, 2023

Conversation

NitkarshChourasia
Copy link
Contributor

@NitkarshChourasia NitkarshChourasia commented Jul 25, 2023

The improvements made to the program are significant and can be summarized as follows:

Object-Oriented Approach: The original code was a simple script with variables and direct swapping of values. In contrast, the improved version uses an object-oriented approach with a class Swapper that encapsulates the swapping logic and functionality.

Class Methods: The swapping methods (swap_tuple_unpacking, swap_temp_variable, and swap_arithmetic_operations) are defined as class methods in the Swapper class. This allows them to be called on instances of the class and operate on the specific values of x and y associated with each instance.

Error Handling: The init method in the improved version includes error handling to check if both x and y are integers. If not, a ValueError is raised, providing a clear message to the user about the expected input.

Encapsulation: The display of values before and after swapping is encapsulated within the display_values method. This method allows for consistent and easy printing of values without repeating the print statements in each swapping method.

Docstrings: The improved version includes detailed docstrings for the class and each method. Docstrings provide clear documentation about the purpose and usage of the class and its methods, making it easier for other developers (including the original programmer) to understand and use the code.

Examples with Class Methods: The improved version demonstrates the usage of class methods with two examples (swapper1 and swapper2). Each example creates an instance of Swapper with different initial values and then calls one of the swapping methods on the instance.

Reusability: With the improved version, the Swapper class can be easily reused in other parts of the code or in different programs, promoting code modularity and reusability.

Overall, the improved version is more organized, follows object-oriented principles, and provides better readability and maintainability compared to the original script-like code. It also incorporates error handling and documentation, making it more robust and user-friendly.

The improvements made to the program are significant and can be summarized as follows:

    Object-Oriented Approach: The original code was a simple script with variables and direct swapping of values. In contrast, the improved version uses an object-oriented approach with a class Swapper that encapsulates the swapping logic and functionality.

    Class Methods: The swapping methods (swap_tuple_unpacking, swap_temp_variable, and swap_arithmetic_operations) are defined as class methods in the Swapper class. This allows them to be called on instances of the class and operate on the specific values of x and y associated with each instance.

    Error Handling: The __init__ method in the improved version includes error handling to check if both x and y are integers. If not, a ValueError is raised, providing a clear message to the user about the expected input.

    Encapsulation: The display of values before and after swapping is encapsulated within the display_values method. This method allows for consistent and easy printing of values without repeating the print statements in each swapping method.

    Docstrings: The improved version includes detailed docstrings for the class and each method. Docstrings provide clear documentation about the purpose and usage of the class and its methods, making it easier for other developers (including the original programmer) to understand and use the code.

    Examples with Class Methods: The improved version demonstrates the usage of class methods with two examples (swapper1 and swapper2). Each example creates an instance of Swapper with different initial values and then calls one of the swapping methods on the instance.

    Reusability: With the improved version, the Swapper class can be easily reused in other parts of the code or in different programs, promoting code modularity and reusability.

Overall, the improved version is more organized, follows object-oriented principles, and provides better readability and maintainability compared to the original script-like code. It also incorporates error handling and documentation, making it more robust and user-friendly.
@NitkarshChourasia
Copy link
Contributor Author

Admin of this page, don't you like well written programs?

@geekcomputers geekcomputers merged commit 1017326 into geekcomputers:master Jul 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants