programming_best_practices

Programming Best Practices

Programming Best Practice

Return to Programming Style Guides, Software Engineering Best Practices, Software Architecture Best Practices, Clean Code, Pragmatic Programmer


Below highlights universal best practices applicable to programming in general, focusing on principles that enhance programming code quality, programming maintainability, programming performance, and programming security. This summary will touch on key concepts without diving into language-specific syntax but will include examples where appropriate.

Introduction to Programming Best Practices

Programming best practices are guidelines that developers follow to produce clean, efficient, and error-free code. These practices are essential for enhancing the maintainability, scalability, and performance of software applications. While practices may vary across different programming languages, the underlying principles often remain consistent.

Use Descriptive Naming Conventions

Choosing descriptive and meaningful names for variables, functions, and classes makes your code more readable and maintainable. Names should convey to the reader what a variable holds, what a function does, and what a class represents.

Keep Functions Focused

Each function or method should perform a single responsibility. This simplifies testing, debugging, and understanding the code, adhering to the Single Responsibility Principle (SRP) of the SOLID principles.

Write Reusable Code

Aim to write reusable functions and modules. This reduces redundancy, minimizes the risk of errors, and facilitates easier updates. Reusability can be achieved by generalizing the code to handle various inputs and scenarios.

Comment and Document Your Code

Comments and documentation are essential for explaining the purpose and logic of your code, especially for complex algorithms. However, comments should not be used to explain bad code; instead, strive to make the code self-explanatory.

Follow the DRY Principle

DRY (Don't Repeat Yourself) emphasizes reducing repetition in your code. Repeated code should be abstracted into reusable functions or modules, making the codebase easier to maintain and update.

Utilize Version Control Systems

Version control systems, such as Git, are crucial for managing changes to the codebase, enabling collaboration, and tracking the history of your project. They facilitate branching, merging, and revert operations, enhancing team workflow and backup strategies.

Adhere to Coding Standards

Following coding standards and style guides for the programming language you're using ensures consistency across the codebase. This practice improves readability and reduces the learning curve for new team members.

Optimize for Readability

Code is read more often than it is written. Optimizing for readability means organizing your code logically, using consistent indentation, and following the language's idiomatic conventions.

Implement Error Handling

Robust error handling mechanisms improve the reliability and usability of your software. Catch and handle potential errors gracefully to prevent the application from crashing unexpectedly.

Secure Your Code

Security should be a priority from the start. Follow security best practices such as validating inputs, using secure communication protocols, and adhering to the principle of least privilege.

Use Data Structures and Algorithms Appropriately

Choosing the right data structures and algorithms for the task can significantly impact the performance and efficiency of your application. Understand the time and space complexity of your implementations.

Perform Code Reviews

Code reviews are a valuable practice for ensuring code quality, sharing knowledge, and catching potential issues early. They encourage collaboration and learning among team members.

Test Your Code

Automated testing, including unit, integration, and functional tests, helps ensure your code works as expected and facilitates refactoring and updates. A solid test suite acts as a safety net for changes.

Refactor Regularly

Refactoring is the process of restructuring existing code without changing its external behavior. Regular refactoring improves the design of the codebase, making it easier to understand and extend.

Understand the Problem Before Coding

Spend time understanding the problem you're trying to solve before jumping into coding. A clear understanding of the requirements and constraints leads to more effective and efficient solutions.

Plan Before You Code

A good plan outlines the major components of your solution and how they interact. This can prevent significant rework and ensure the architecture is solid and scalable.

The tech industry evolves rapidly. Staying informed about the latest trends, tools, and best practices in your field can open up new opportunities for optimization and innovation.

Balance Performance with Maintainability

While performance optimizations are important, they should not come at the cost of readability and maintainability. Premature optimization can lead to complex, hard-to-maintain code.

Simplify Your Code

Simplicity is key to good software design. Avoid over-engineering solutions and keep your code as simple as possible while fulfilling the requirements.

Embrace Change and Feedback

Be open to changing your code based on feedback from code reviews, testing, and user input. Adapting to change is a vital part of the development process.

Leverage Design Patterns

Design patterns provide generalized solutions to common design problems. Using them wisely can help structure your code in a more scalable, reusable, and maintainable way.

Prioritize User Experience

Consider

the end-user experience in every aspect of your application, from the UI/UX design to the responsiveness and performance of your software.

Foster a Strong Development Culture

A strong development culture encourages continuous learning, collaboration, and a focus on quality. It's essential for building effective and motivated teams.

Understand Your Tools

A deep understanding of your development tools, including IDEs, build systems, and debuggers, can significantly enhance your productivity and the quality of your output.

Continuous Integration and Continuous Deployment (CI/CD)

CI/CD practices automate the integration, testing, and deployment of your code. They help maintain code quality and speed up the release process.

Monitor and Optimize Performance

Regularly monitor your application's performance in production. Use profiling tools to identify bottlenecks and optimize as necessary.

Handle Data Responsibly

Be mindful of data privacy laws and ethical considerations when handling user data. Implement proper data protection and privacy measures.

Practice Sustainable Coding

Sustainable coding involves writing energy-efficient code and considering the environmental impact of your software, an increasingly important aspect of software development.

Engage with the Community

Participating in programming communities can provide support, inspire new ideas, and keep you informed about best practices and emerging technologies.

Learn from Failure

View failures as learning opportunities. Analyzing mistakes helps improve your problem-solving skills and resilience.

Prioritize Accessibility

Make your applications accessible to as wide an audience as possible, including users with disabilities. Accessibility is not only ethical but often a legal requirement.

Be Ethical in Your Practice

Always consider the ethical implications of your work. Responsible programming involves ensuring that your code and applications benefit users and do not harm society.

Plan for Scalability

Design your systems to handle growth, both in terms of users and data. Scalable architecture ensures that your application can meet future demands.

Documentation Is Key

Maintain comprehensive documentation for your code, APIs, and systems. Good documentation is invaluable for onboarding, maintenance, and future development efforts.

This summary encapsulates core programming best practices that transcend specific languages and technologies, focusing on principles that lead to high-quality, maintainable, and efficient software. For language-specific details and further exploration, consult the official documentation and resources dedicated to your programming language of choice.


Git

Git Best Practices:

  • Touch few things at once
    • it's kind of obvious

Continuous Integration (CI)

Continuous Integration CI Best Practices:

  • Warn about formatting verification
    • No developer disruption in the future
  • warn about coverage illness
    • coverage is a quite important thing
  • track test failures and flakiness
    • statistics, hints, conclusions
  • store build logs for 3+ months
    • having analytics and working links to them
  • do not remove builds/PR/issues
    • unexpected 404 will help no one, will not save disk space also
  • docs are generated from the code
    • end-user changes must be explicit
  • locally reproducible builds
    • re-run part of a job on my local machine
  • use Makefile, Bash
    • simple, easy, well known
  • ability to start/stop a job on CI
    • waiting for an approval to make this action is a horrible bottleneck

Continuous Deployment (CD)

See Continuous Integration vs. Continuous Delivery vs. Continuous Deployment

  • configuration files must be validated before a start
    • unless difficult debug is your goal
  • store config in the same repo as a code
    • this prevents 'the chick or the egg' problem
  • all features must be protected by feature flag
    • in case of an accident, it will(might) be enough to turn it off
  • deployment of a new config must be isolated
    • to separate different machines/containters/storages from each other
  • deploy must go from a (git-)tag or release, not from the master
    • this restricts a changeset which will be deployed

Code Health

  • leave your code better than you found it
  • document your code
    • giving a small context for API will save a lot of time
  • use linters and code analysis all the time
    • the best handling for the best code
  • touch legacy, often
    • it becomes non-legacy faster
  • remove deprecated stuff
    • having a bloated dead code is a mistake
  • also remove old code
    • it adds even fewer reasons to the new code
  • use TODO, BUG, XXX in code
    • jumping to the issue tracker can be minimised
  • no experiments in the master
    • use your 20% time as a playground, please
  • allow changing log-level on the fly
    • this will simplify production's debug routine
  • limit your log file, 'cause it might grow unlimited
    • this might cause troubles to your app, be careful
  • store your config in /etc/myapp and logs in /var/log/myapp
    • this will make everything more structured and well defined
  • all modules must have the same structure
    • similar environment everywhere is a good idea
  • if you can’t show a bottleneck, don’t start to optimise it
    • it might be interesting and challenging, but useless
  • check back compatibility before the new releases
    • make it explicit and obvious (+doc how to port)

Database

Database Best Practices (see also Data Science Best Practices:

  • think about your data
    • don't use SQL/NoSQL without a reason
  • keep models normalized
    • less storage, better performance
  • but don't normalize without a reason
    • everything is a trade off
  • use timestamp to store a date/time
    • this will save you from formatting hell
  • log slow queries
    • see what is happening in a database and who is too greedy
  • don't put business logic into DB or at least make it loosely coupled
    • this will give you an easy migration to another DB

Dependencies and Dependency Management

The most important task of a programmer is dependency management! (see latest Manning book MEAP) Dependency Management Best Practices:

  • wrap any dependency with an interface(or analogue)
    • this will prevent a vendor lock on it
  • bump libs on a permanent basis
    • the new version is expected to be better
  • have a local cache-server with dependencies
    • adds stability to the infrastructure
  • pin your dependencies to a specific version
    • accidental commit to dependency's master will break nothing
  • prefer mature technology, rather than a hyped one
    • mature will die slower, then hyped
  • fork instead of hack
    • it might be better to fix a lib instead of wrapping for the desired behaviour

Continuous Testing and TDD

  • use one test framework
    • a similar environment is better
  • show results, not just stack traces
    • some failures are obvious with visible result
  • isolated tests
    • use beforeTest and afterTest aggressively
  • TDD
    • it really works
  • measure a code coverage
    • a quick and easy way to eliminate bugs
  • test your backups
    • they might be broken
  • do not hard code ports in tests
    • unless you're interested in random flaky tests

Team

Teamwork Best Practices - Team Best Practices:

  • pairing, 50% and more
    • you're doing better, you're not bored
  • high-level stand-ups, time bounded
    • less info about irrelevant stuff
  • 2-3 week sprints
    • have an achievable sprint goal
    • UPD: depends on team/project, might be unuseful
  • per sprint roles
    • it's quite comfortable time bounds
  • only urgent topics are face-to-face
    • fewer distractions for unimportant things
  • friendly atmosphere
    • no insulting environment, respectful trolling
  • 'coding rockstar'
    • it is a demotivation, not an inspiration
  • if you're on the vacations - specify a date range
    • it'll be easier to find someone else or postpone the question
  • FAQ for newcomers
    • 30-day plan with all stuff that they should accomplish
  • document solved and unresolved problems
    • team members will be aware of some problems and it will be resolved much faster
  • every ticket in progress must have an assignee
    • this prevents doing same work on one task
  • there must be a run book or guide for every responsibility
    • 30/60/90 days plan for newcomers, guide for on call, support

Meetings

Meeting Best Practices:

  • I can skip if I'm out of scope
    • do not waste team and own time
  • if you're organizing a meeting - prepare an agenda
    • to have a way how to drive a meeting
  • action points after the meeting
    • who does what and when
  • avoid bus factor as much as possible
    • moving/cancelling an unimportant meeting because 1 person is a bad sign

Communications

Communications Best Practices:

  • use the best apps
    • fast, flexible, pleasurable
  • outcomes of important discussions should be on a wiki
    • better visibility for outcomes
  • only important notifications
    • @all should be rare for irrelevant updates
  • easy access to any team room
    • that's obvious, hey
  • do not delete personal chats with inactive users
    • some chats contain interesting ideas
  • closed ticket must contain a link to the changes
    • every change must be easily accessible and visible for others

Permissions and Security

Permission Best Practices and Security Best Practices:

  • Git, CI, SSH read-access to everything
    • reading server logs cannot cause troubles
  • SSO to anything
    • better organization of credentials
  • each office should have global admin
    • different time zones are a bottleneck

Workspace

Workspace Best Practices:

  • engineers apart from non-engineers
    • fire & ice
  • quiet open-space
    • someone might be sensitive to noise…
  • quiet zones
    • …really sensitive
  • the kitchen isn't for chill-out

** the playroom is a thing

  • nothing smelly near working area
    • even coffee/cinnamon/mowed grass might irritate

Networking

Networking Best Practices:

  • WiFi must work all the time
    • obvious
  • LAN must be even more stable
    • uber-obvious

Life

Life Best Practices:

  • how often should my salary be reviewed?
    • worth asking
  • work from home is a must have
    • family, health, even weather might be a reason
  • educational budget to anything related to dev stuff
    • I would like to learn new technologies, why not?
  • allow committing to the open-source
    • company karma++
  • skipping team events must be acceptable
    • well…obvious
  • 20% time is a vacation like time
    • creating anything that might help someone is awesome
  • brown bags sessions must be rewarded
    • sharing knowledge is the best way to inspire
  • monthly geek swag <3
    • t-shirts, hoodies and all other stuff
  • health food in the kitchen
    • candies are cool, but I would like to live longer

Fair Use Source

Best Practices: ChatGPT Best Practices, DevOps Best Practices, IaC Best Practices, GitOps Best Practices, Cloud Native Best Practices, Programming Best Practices (1. Python - Django - Flask - - Pandas, 2. JavaScript - HTML - CSS - React - Next.js - Node.js - NPM - Express.js - Deno - Babel - Vue.js, 3. Java - JVM - Spring Boot - Quarkus, 4. C# - dot NET, 5. C++, 6. PHP - Laravel, 7. TypeScript - Angular, 8. Ruby - Ruby on Rails, 9. C, 10. Swift, 11. R, 12. Objective-C, 13. Scala - Z, 14. Go - Gin, 15. Kotlin - Ktor, 16. Rust - Rocket Framework, 17. Dart - Flutter, 18. Lua, 19. Perl, 20. Haskell, 21. Julia, 22. Clojure, 23. Elixir - Phoenix Framework, 24. F# Best Practices | F#, 25. Assembly, 26. bash, 27. SQL, 28. Groovy, 29. PowerShell, 30. MATLAB, 31. VBA, 32. Racket, 33. Scheme, 34. Prolog, 35. Erlang, 36. Ada, 37. Fortran, 38. COBOL, 39. VB.NET, 40. Lisp, 41. SAS, 42. D, 43. LabVIEW, 44. PL/SQL, 45. Delphi/Object Pascal, 46. ColdFusion, 47. CLIST, 48. REXX. Old Programming Languages: APL, Pascal, Algol, PL/I); Programming Style Guides, Clean Code, Pragmatic Programmer, Git Best Practices, Continuous Integration CI Best Practices, Continuous Delivery CD Best Practices, Continuous Deployment Best Practices, Code Health Best Practices, Refactoring Best Practices, Database Best Practices, Dependency Management Best Practices (The most important task of a programmer is dependency management! - see latest Manning book MEAP, also Dependency Injection Principles, Practices, and Patterns), Continuous Testing and TDD Best Practices, Pentesting Best Practices, Team Best Practices, Agile Best Practices, Meetings Best Practices, Communications Best Practices, Work Space Best Practices, Remote Work Best Practices, Networking Best Practices, Life Best Practices, Agile Manifesto, Zen of Python, Clean Code, Pragmatic Programmer. (navbar_best_practices - see also navbar_anti-patterns)


© 1994 - 2024 Cloud Monk Losang Jinpa or Fair Use. Disclaimers

SYI LU SENG E MU CHYWE YE. NAN. WEI LA YE. WEI LA YE. SA WA HE.


programming_best_practices.txt · Last modified: 2024/05/01 03:52 by 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki