Code Conventions


Coding conventions are a set of guidelines for a specific programming language that recommend programming style, practices, and methods for each aspect of a program written in that language. These conventions usually cover file organization, indentation, comments, declarations, statements, white space, naming conventions, programming practices, programming principles, programming rules of thumb, architectural best practices, etc.

These are guidelines for software structural quality. Software programmers are highly recommended to follow these guidelines to help improve the readability of their source code and make software maintenance easier. Coding conventions are only applicable to the human maintainers and peer reviewers of a software project. Conventions may be formalized in a documented set of rules that an entire team or company follows or may be as informal as the habitual coding practices of an individual. Coding conventions are not enforced by compilers. (This paragraph was stolen from Wikipedia)

You might disagree with some of these conventions and do things your own way in your personal projects. And that is perfectly fine. However we think it's important that our students abide to a certain set of standards because that's what will be required from them in companies. More often than not, especially when starting, you'll have to meet certain requirements that might be different from what you would do. That will assure the code base is consistent, which is very important for the efficiency of software development teams.

Microsoft Documentation's Coding Style Guide

Please read through this guide. It contains widely adopted coding style conventions widely used in the industry.

1. Spacing

  • The first line of a file shouldn't be empty.
  • No empty lines are necessary after the last closing bracket.
  • Have one empty line between using statements and namespace.
  • Have one empty line between namespace and class name.
  • Have one empty line between methods.
  • Remove empty lines after opening curly brackets and before closing curly brackets.
  • Group related statements in blocks separated by an empty line. It prevents your code from looking like spaghetti.

2. Remove unused code

Remove anything that's not being used. It makes your code less readable and mantainable. In Visual Studio the compilers will tell you when code isn't being used by slightly fading it.

3. Refrain from commenting

Code should be self-explanatory. Name your classes/methods/properties/fields so that you don't need to compensate by writing comments. Only write comments that are reminders for your future self or to let other developers know about aspects that the code can't possibly explain.

4. Don't abbreviate

A variable called getUserByIdButton is infinitely better then one called btn. Be verbose so that readers don't have to guess what's going on in your code.

5. Write less code

It might sound contradictory to item 4, but it's not. Verbose naming is fine. Writing unnecessary code isn't. (i.e. Don't assign variables that have a default assignment, example: int myInteger = 0 or bool myBoolean = false and use ternaries when possible).

6. Use the newest namespaces

From C# 10 we are able to ditch the namespace block and save one indentation level, which optimizes the use of space.

7. Var or Int?

Up to you.

8. Use string interpolation and string literals.

You can mix strings with variables elegantly using the $ sign. And you can write multiline strings verbatim using the @ sign.

Log in to mark this article as read and save your progress.
An unhandled error has occurred. Reload 🗙