Best Practices

Development Best Practices

Best Practices

General coding best practices are a set of guidelines and recommendations that help developers write clean, efficient, and maintainable code. While specific practices may vary depending on the programming language and the project’s requirements, here are some general coding best practices:

  1. Consistent and meaningful naming: Use descriptive names for variables, functions, classes, and other entities to make the code self-explanatory. Maintain consistency in naming conventions throughout the codebase.

  2. Code readability: Write code that is easy to read and understand. Use proper indentation, spacing, and formatting. Add comments to explain complex logic or important details.

  3. Modular and reusable code: Break down the code into smaller, logical modules or functions that perform specific tasks. This improves code maintainability, readability, and allows for code reuse.

  4. Don’t repeat yourself (DRY): Avoid duplicating code. Instead, create reusable functions or abstractions to eliminate redundancy. This reduces the chances of introducing bugs and makes code maintenance easier.

  5. Keep functions and methods small: Aim for small, focused functions or methods that perform a single task. This improves code readability, testability, and makes it easier to reason about the behavior of the code.

  6. Follow the Single Responsibility Principle (SRP): Each module, class, or function should have a single responsibility. Separating concerns helps in maintaining and modifying code without impacting other parts of the system.

  7. Error handling and validation: Implement proper error handling and input validation to make the code more robust. Validate user inputs, handle exceptions gracefully, and provide meaningful error messages.

  8. Use version control: Utilize a version control system (e.g., Git) to track changes to the codebase, collaborate with others, and easily revert or review code changes when needed.

  9. Code reviews: Encourage peer code reviews to catch bugs, ensure adherence to best practices, and maintain code quality. Reviews provide an opportunity to share knowledge and improve the overall quality of the codebase.

  10. Testing: Write automated tests to validate the behavior and correctness of the code. Unit tests, integration tests, and other testing techniques help catch bugs early, ensure code stability, and facilitate refactoring.

  11. Performance optimization: Optimize code for performance when necessary. Identify bottlenecks, use appropriate data structures and algorithms, and minimize unnecessary computations or operations.

  12. Security considerations: Follow security best practices to protect against common vulnerabilities. Sanitize user inputs, use prepared statements or parameterized queries to prevent SQL injection, and encrypt sensitive data.

  13. Documentation: Document the code to provide insights into its functionality, usage, and any specific requirements. Use inline comments, README files, and documentation tools to make it easier for others to understand and use the code. The README is the first file a person encounters when exploring a project, making it crucial to create one and provide comprehensive details to the best of your knowledge. There isn’t a single right way to structure a README, but it should cover some essential topics, including:

    • The purpose of the code.
    • Instructions for installing the code.
    • Guidelines on how to use the code effectively.
    • Any other relevant information users need to know about the code.

    A well-crafted README fosters collaboration and ensures the project’s long-term development by making it accessible and understandable to a broader audience.

  14. Keep up with best practices: Stay updated with the latest best practices, coding standards, and programming language conventions. Regularly learn and improve your coding skills to write better code over time.

Remember that best practices may vary depending on the specific programming language, domain, and project requirements. It’s important to adapt and adjust these practices as needed for each situation.

Mounting a Windows Share

Quick Reference

The command below can be used to create a temporary mount point although the mount will disassociate on next server reboot.

mkdir /home/<user>/mnt/DataManagement && \
sudo mount -t cifs //10.10.145.5/DataManagement /home/<user>/mnt/DataManagement -o username=<user>,password="",uid=1000

Persistent Mounts

  1. Create a mount point to a Windows share name (e.g. Q): sudo mkdir /mnt/share/Q

  2. Append the following similar line to /etc/fstab– to associate the mount automatically upon machine boot:

    //192.168.50.19/Transfer /mnt/share/Q cifs credentials=/home/DatamanBU/.smbpasswd,uid=1011,gid=1005,dir_mode=0775,file_mode=0775,rw 0 0

    Notes:

    • uid=1011: mount as user username. Find UID with id -u <username>

    • gid=1005: mount as group datamgrs

    • dir_mode=0775, file_mode=0775: readable/writeable by any users that are members of datamgrs group

    • The credentials file /home/DatamanBU/.smpbasswd must already exist (or be created) by root. It contains 2 lines:

      username=DatamanBU
      password=password
    • Make the credentials file only readable by root: sudo chmod 700 /home/DatamanBU/.smbpasswd

  3. Associate the mount immediately: sudo mount -a -v -t cifs

Linux Permissions

Syntax

General: _rwxrwxrwx 1 owner group

_ | rwx | rwx | rwx = Special | Owner | Group | All Users

Shorthand

  • 4 | r = Read
  • 2 | w = Write
  • 1 | x = Execute

Detailed

  • 0 = ---
  • 1 = --x
  • 2 = -w-
  • 3 = -wx
  • 4 = r-
  • 5 = r-x
  • 6 = rw-
  • 7 = rwx

Commands

chgrp = Change group

Example: sudo chgrp -R <group> <folder>

chown = Change ownership

Example: sudo chown -R <user>:<group> <file/folder>

chmod = Change permissions

Example: sudo chmod -R 774 <file/folder>

Make new files inherit the group: sudo chmod g+s <folder>

Example

Create a shared directory for a group.1. Create a shared directory for users to access: /share

  1. Assign users to a common group (staff): sudo usermod -a -G staff <user>
  2. Verify user groups: groups <user>
  3. Create shared directory and assign permissions:
sudo mkdir /share && \
sudo chgrp -R staff /share && \ # assign group
sudo chmod -R g+w /share && \ # permissions
sudo chmod -R +s /share # inherit permissions for newly created files/folders

Diffing PDF and DOCX files

If you have a PDF and DOCX of the same document and want to check for difference in text, use diff to compare them. Since we’re using --word-diff, it doesn’t matter that the two files use wildly different line wrapping.

gs -q -sDEVICE=txtwrite -o- file1.pdf > file1.txt
pandoc -t plain file2.docx > file2.txt
git diff --no-index --word-diff file1.txt file2.txt

Or create a shortcut…

alias pdfcat='gs -q -sDEVICE=txtwrite -o-'
alias doccat='pandoc -t plain'
pdfcat file1.pdf > file1.txt
doccat file2.docx > file2.txt
git diff --no-index --word-diff file1.txt file2.txt

Credits

SFTP

Below are steps for pulling server files to a local machine using SFTP.

  1. Allow secure connection for local IP on remote machine

    sudo ufw allow from 73.239.143.170 to any port 22
  2. Create SFTP connection to remote from local machine

    sftp hostname@167.99.102.213
    sftp hostname@167.99.102.213's password: SuperSecret
  3. Pull remote files to local machine

    get {remote-path} {local-path}