---
# System prepended metadata

title: Coding Solutions Convention

---

# Coding Solutions Convention

1. **Be consistent.**
	
	* 	**Proper variable name cases should be followed**. Name cases are shown below.
		* 	**camelCase**
		* 	**PascalCase**
		* 	**UPPER_CASE**
		* 	**snake_case**
	
		Follow the name cases conventions for each language.
	
	
		![](https://i.imgur.com/uANabBj.png)

	
		To summarize, observe the comparison between a bad code and a consistent code in C language.
	
		**BAD PRACTICE**
	
		![](https://i.imgur.com/aidmJta.png)
	
		---
	
		**GOOD PRACTICE**
	
		![](https://i.imgur.com/WbjBhdp.png)


	---
	
	* **Curly braces must be properly placed**. A table of proper placement of braces for each language is shown below.
	
		![](https://i.imgur.com/GrPIbKc.png)

		Here is another example to compare a bad and  good coding practice for curly braces in C language.
		
		**BAD PRACTICE**
		![](https://i.imgur.com/TC9dM9Q.png)

		---
		
		**GOOD PRACTICE**
		![](https://i.imgur.com/JOmpOag.png)
		
		---
		
		
	* **Code must observe proper indentation to make code more readable**. 

		**BAD PRACTICE**
		
		![](https://i.imgur.com/IugK84v.png)
		
		---
		
		**GOOD PRACTICE**
		
		![](https://i.imgur.com/Y4Ep2lC.png)

		---

2. **Limit line length.**
	
	Make it that the code solution are visible in the code editor so that it would be easier to read for the users.
	
	**BAD PRACTICE**
	
	![](https://i.imgur.com/HcBrnXo.png)
	
	**GOOD PRACTICE**
	
	![](https://i.imgur.com/pkcSHxe.png)
	
	---

3. **Readability is more important than Micro Optimization.**

	A readable and maintainable code is better than a code that only adds a bit of performance but is very hard to read. Cleaner code would help the users understand the concept and logic of the problem easier. You can make functions or methods and just call them in the main function. This will make a cleaner and understandable main function. This will also help in digesting the solution easier since the logic is split into multiple parts which helps the users focus on one area at a time.
	
	**Bad Practice**
	
	![](https://i.imgur.com/7r8P0Td.png)

	---
	
	**Good Practice**
	
	![](https://i.imgur.com/MuzFaJs.png)

	---
	
4. **Use guard clauses.**

	To avoid deep nesting, use guard clauses. A guard clause is simply a check that immediately exits the function, either with a return statement or an exception. For example, instead of using if else statements, use only if statements which if true, returns the function immediately. This simplifies the  code making the solution more comprehensible.
	
	**Bad Practice**
	
	![](https://i.imgur.com/hR8fZGs.png)

	---
	
	**Good Practice**
	
	![](https://i.imgur.com/KRuvXC6.png)

	---
	
5. **Add comments.**

	Adding comments in your code is crucial to teachers since it also help them explain why the code  works. In your code, answer the following questions: **"What is this?"** and **"Why is this?"**. It is encouraged that the parameters and return type is discussed if functions and methods are being dealt with. 
	
	If adding a comment makes a length line very long, it is necessary that multiline comments are used. Also, make sure that the comments you place in your code solution is relevant to the topic or the problem tags.
	
	**Bad Practice**
	
	![](https://i.imgur.com/kawLKxc.png)
	
	---
	
	**Good Practice**
	
	![](https://i.imgur.com/2iMR4QD.png)

	---
	
6. **Avoid useless/redundant comments.**

	If the problem is a course problem, do not add comments on a code that has been already discussed. For example, if the topic "if statements" has not been discussed yet, it is mandatory to add comments line by  line. However, if it has been discussed already, do not discuss anymore. On complex problems, only comment on the algorithms and logic. Do not add trivial comments as it only leads to confusion and longer code.

	**Bad Practice**
	
	![](https://i.imgur.com/CeCS8Ci.png)

	---
	
	**Good Practice**
	
	![](https://i.imgur.com/qQkaR0Y.png)

	---
	
7. **Name variables properly.**

	Make the variable name explain itself. It means that variable names must tell what it does like if the variable is a number then the variable should also be `number`. Also, if the problem indicates the name of the variable, it is mandatory that it is followed.

	**Bad Practice**
	
	![](https://i.imgur.com/FkbrGFY.png)

	---
	
	**Good Practice**
	
	![](https://i.imgur.com/mQgwnpz.png)

8. **Use consistent temporary names**.

	If you are going to use the string "temp" as your name for temporary variables, make it consistent for all parts of your codes. On loops, make sure to be consistent with the looping variable introduction. For example, if you use `i` as your looping variable in a for loop, make it true for all for loops.


9. **Other notes**

* 	In Python, use C style strings which use placeholders like %d.