Skip to content

Best Practices

Chandan Kumar edited this page Apr 10, 2018 · 2 revisions

Software Development Best Practices

Sticking to a pattern helps. In a team it should be followed, like its a law.

General practices

  • Code Formatting: Use .editorconfig at least to apply code formatting rules.
    • Any kind of code formatter requires editor integration, without which it is mostly useless.
  • Use two spaces instead of tab. Avoid heated debates on this subject. Period.
  • Use beautify or Prettifier, or linters.
    • Choice depends if you are fine with opiniated code linter or whatever.
  • Do peer code review
    • Github has this inbuilt. It' a great starting point.
  • Brainstorming application design and spending more time on design is more worthwhile than having hard time refactoring later.
  • API level test cases are very handy as project grows.
    • Easiest way to start is by using Postman | Apps.
    • Can use Mocha + Chai etc.

Git:

  • Avoid create branch name with forward slash / e.g. branch/feature1 instead use branch-feature1
  • Update greatest stable build in Master.
  • Make use of Pull request for code review.
  • Brush up Git best practices.
  • Prefer Git rebase or squash merge
  • Tag releases
  • Use versioning pattern x.x.x

Virtualization AWS:

  • If there is a complex setup to be done on AWS EC2, use Virtualbox to test it out locally first.
  • Vagrant makes it easier to try out VirtualBox or any kind of virtualization.
  • Docker will take over most of these things eventually anyway.

NodeJs Best Practices

  • Use NVM to install NodeJs for Development
  • Use linter: eslint
  • Make use of modules.exports.
  • For code reusability create utility file, to perform simple functions
  • Use logger like Winston
  • Use package.json for repetitive tasks.
  • Use Gulp to perform complex repetitive tasks. Although it is quite likely that package.json will be good enough for most of the script like tasks.
  • Use nodemon for development.
  • Avoid installing node libs globally. For eg. nodemon. Figure out importance of dev and test dependencies.
  • Keep Routes - controllers thin. Put business logic in Model functions or even better create services.
  • Write Functional / API test cases at least.

MVC / Rest

Model

  • Mongo: Encapsulate model/collection specific DB functions in model functions (wrap CRUD in respective models).
  • Mongo: Wrap business logic in services. Especially if logic involves multiple collection.
  • Avoid importing models within each other. It will lead to cyclic dependency issue.

Controller

  • Keep controller code as thin as possible.
  • Separate out reusable code in helper files
  • Make use of middleware/filters
  • Use passportjs for Authentication

View

  • This is UI specific.

Middleware

  • Use middleware for web filters.
    • Authentication check
    • Transformations
    • Error Bubbling and Handling

Helpers

  • Use helpers to transform repetative tasks
    • Data transformation
    • Configuration look up

Service

  • Use service to wrap atomic business logic as a single function call.
  • Do not add web framework dependency in to service. For eg. don't pass req and res. For such things use web helpers.

Database

  • Choose right database for the job.
  • Begin with simplest design, later mix and match database for performance. For eg. you may start with MongoDB and later compliment it with Redis for caching.
  • Schema design is very crucial. Bad schema design is a reciepe for nightmares.
  • Indexing is very important. Over indexing, anti pattern
  • Cache database result to scale and have a better response time.
MongoDB
  • Indexing is important.
  • Over indexing hampers performance.
  • Pagination: offset and skip is expensive for large set of data, use last_id or something similar.
  • Use MongoDB cursor for loading/processing/streaming a lot of data from database.
  • Streams will improve app performance.
    • If a lot of data is transferred per connection, chances are that it can be improved with streams/cursor

Addtional Resource

  1. https://www.mongodb.com/blog/post/6-rules-of-thumb-for-mongodb-schema-design-part-1