Coding best practices
Competing on F2F challenges means that in most cases you’ll be working based on an existing code base. That means you should follow the existing coding style that’s already used in the application and always follow the coding best practices.
Here are some best practices you should always have in mind:
1. Use consistent indentation
There is no right or wrong indentation that everyone should follow. The best style, is a consistent style. Once you start competing in large projects you’ll immediately understand the importance of consistent code styling.
2. Follow the DRY Principle
DRY stands for “Don’t Repeat Yourself.
The same piece of code should not be repeated over and over again.
3. Avoid Deep Nesting
Too many levels of nesting can make code harder to read and follow.
For example:
[objc]
```
if (a) {
…
if (b) {
…
if (c) {
…
…
…
}
}
}
```
[/objc]
Can be written as:
[objc]
```
if (a) {
return …
}
if (b) {
return …
}
if (c) {
return …
}
```
[/objc]
4. Limit line length
Long lines are hard to read. It is a good practice to avoid writing horizontally long lines of code.
5. File and folder structure
You should avoid writing all of your code in one of 1-2 files. That won’t break your app but it would be a nightmare to read, debug and maintain your application later.
Keeping a clean folder structure will make the code a lot more readable and maintainable.
6. Naming conventions
Use of proper naming conventions is a well known best practice. Is a very common issue where developers use variables like X1, Y1 and forget to replace them with meaningful ones, causing confusion and making the code less readable.
7. Keep the code simple
The code should always be simple. Complicated logic for achieving simple tasks is something you want to avoid as the logic one programmer implemented a requirement may not make perfect sense to another. So, always keep the code as simple as possible.
For example:
[objc]
```
if (a < 0 && b > 0 && c == 0) {
return true;
} else {
return false;
```
[/objc]
Can be written as:
[objc]
```
return a < 0 && b > 0 && c == 0;
```
[/objc]
This article is part of the 5 Weeks to Learn Topcoder educational series. Want to learn more? Check out the entire series and all the helpful content here.