NINJA-PingU icon indicating copy to clipboard operation
NINJA-PingU copied to clipboard

Refactor code to adhere to Clean Code principles

Open igorpenhaa opened this issue 5 months ago • 0 comments

  • Renamed variables and functions for clarity:

    • mSocket -> socketDescriptor
    • createSock() -> createSocket()
    • start_receiver() -> startReceiver()
    • iphdrlen -> ipHeaderLength
    • mBuffer -> packetBuffer
  • Extracted functions to follow Single Responsibility Principle (SRP):

    • Added createSocket() for socket creation
    • Added closeSocket() for closing the socket
    • Added receivePacket() for packet reception
    • Added processPacket() for processing received packets
    • Added handleOpenPort() for handling open ports
  • Improved error handling:

    • Replaced generic error messages with perror() for more detailed diagnostics
    • Replaced exit(1) with appropriate error handling and resource cleanup
  • Removed redundant and unclear comments:

    • Replaced comments with self-explanatory function and variable names
  • Defined constants for magic numbers:

    • Replaced numeric literals with named constants, e.g., MAX_BUFFER_SIZE, RECEIVE_BUFFER_SIZE, CACHE_SYNC
  • Added proper memory management:

    • Ensured allocated memory is freed with free()

igorpenhaa avatar Sep 03 '24 03:09 igorpenhaa