Skip to main content

Command Palette

Search for a command to run...

4 Best Practices For Coding

Updated
2 min read
4 Best Practices For Coding
R

Hi guys , I am having an total 8+ years of experience with PHP.. during this 8 years i had worked on lots of projects and ideas.

1. Variables & Classes Names

  1. Choose descriptive and unambiguous names.

  2. Make meaningful distinctions.

  3. Choose descriptive and unambiguous names.

  4. Use pronounceable names.

  5. Use searchable names.

  6. Use nouns for classes, packages, and variables

  7. Use verbs/verb phrases for functions.

  8. Avoid unpopular acronyms and other names that don’t make sense.

  9. Avoid using confusing letters for names.

  10. Avoid meaningless prefixes or suffixes.

2. Code Formatting

coding best practices python,good coding practices in software engineering,coding best practices java,software development best practices,

  1. Group code by their functionality. Related functions must be close. Related code must appear vertically dense.

  2. Declare temporary variables close to their usage.

  3. Adapt company/team-wide code conventions. Agree on a common standard for code-formatting. 4. Either use an external tool or IDE options for auto-formatting.

  4. Avoid too long files. 1000–2000 lines are okay. Shorter, the better.

  5. Avoid too-wide code lines. Make sure they fit into the screen (Up to 70 to 120 characters). If they don’t fit, try to split them into multiple lines.

  6. Write high-level code first in a file and keep lower-level implementations towards the end of the file. (Good files are like newspaper articles, with a heading, the important stuff first, and details later)

  7. Make sure the code is ordered as of the calling sequence. The caller should be before the callee.

  8. Follow proper indentation across code files. Use an external tool or IDE’s support.

3. Error Handling

coding best practices python,good coding practices in software engineering,coding best practices java,software development best practices,

  1. At lower levels, throw exceptions instead of returning error codes. Keep error codes for communication between different layers, interfaces, or systems.

  2. Errors and exceptions must provide adequate context (such as intent, failure stage, error type, failed values etc.)

  3. Map foreign errors to adhere to common standards. Wrap all errors and exceptions raised from external systems, third-party libraries, and APIs. Use a generic error type for unknown/unhandled cases.

  4. Reduce the reliance on return type and null checks (since they strongly couple failure path information to main flow). Instead of returning null/false, return a proper error object or throw an exception. Passing null to a function / next stage is also bad, avoid such practices.

Get Some More Best Practices

Sublime Text 3 Plugins That Every Developer Must Use

13 VSCode Extensions That Every Web Developer Should Use

D

There's some good advice here undoubtedly, however, bare in mind there are different standards applied in some different languages, some force one to write a certain way, and cannot be particularly manipulated to follow a general specific path.

Also, remember some languages have formatting and styling built in either optional or not, most enforce a general rule for that language (e.g. Go / Python)

Additionally, there is the use of linting tools available for typescript and javascript, which can be adjusted based on whatever style one adopts.

There are semantic nuances and practices for certain languages, which can be seen as contrary to some of the rules, for example replacing an unused function parameter with an underscore is common in Go and often in typescript, but an underscore is not particularly descriptive. However, providing an argument that is not used in this case, will cause issues with the linter. Yet this rule should not be adjusted merely to allow unused variables, as this would obfuscate the code somewhat.

Also, I would say 1000-2000 line code files are far too long, this would certainly call for modularization as reading a file this long is rather a stretch. I work within a 200-500 line rule which is rarely broken. Unless one is striving for a minimal compiled / transpiled source code size footprint, there is nothing wrong with some more modules/files here. This is more achievable by writing more functional less boilerplate code when possible.

"Make sure the code is ordered as of the calling sequence. The caller should be before the callee.Rahul

Not necessary, it depends very much on the language you are writing. Of course, there are arguments for readability, but in compiled languages especially those with closures and execution scope, it's not always necessary to provide the implementation before the definition in the source code.

Regardless, a good article, in general.

2

More from this blog

knowmore

12 posts