From 35f8ebb9a88100e23dbeb97da6e20035c18d020e Mon Sep 17 00:00:00 2001 From: Rushabh Mehta Date: Wed, 20 Jun 2018 12:55:58 +0530 Subject: [PATCH] Updated Coding Standards (markdown) --- Coding-Standards.md | 52 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 46 insertions(+), 6 deletions(-) diff --git a/Coding-Standards.md b/Coding-Standards.md index 7e626df..179577e 100644 --- a/Coding-Standards.md +++ b/Coding-Standards.md @@ -1,10 +1,50 @@ -1. **Translated Strings:** All user facing strings / text must be wrapped in the `__("")` function in javascript and `_("")` function in Python, so that it is shown as translated to the user. -1. **Deprecated API:** The API used in the pull request must be the latest recommended methods and usage of globals like `cur_frm` must be avoided. -1. **Whitespace and indentation:** The ERPNext and Frappe Project uses tabs (I know and we are sorry, but its too much effort to change it now and we don't want to lose the history). The indentation must be consistent whether you are writing Javascript or Python. Multi-line strings or expressions must also be consistently indented, not hanging like a bee hive at the end of the line. We just think the code looks a lot more stable that way. -1. **SQL Injections:** If you are writing direct SQL query, don't use `.format` to replace strings. Example `frappe.db.sql('select age from tabUser where name='{}'.format(user))` is wrong. It should be `frappe.db.sql('select age from tabUser where name=%s', user)` -1. **Complex Conditions:** Code must be easy to read, so don't try clubbing multiple conditions in one line and make it complex. If you have a line that has multiple AND or OR, break it up into multiple conditions. -1. **Function Sequence:** If you have multiple functions in a file, the most significant function should be on the top and the inner functions should be below. Check the example below: +### User facing messages must be translated + +All user facing strings / text must be wrapped in the `__("")` function in javascript and `_("")` function in Python, so that it is shown as translated to the user. + +### Avoid using Deprecated API + +The API used in the pull request must be the latest recommended methods and usage of globals like `cur_frm` must be avoided. + +### Break functions / methods more than 10 lines of code + +Long functions are hard to read and debug, and 10 lines is usually a good point to break the function into smaller parts. Smaller functions are easy to read and review. You might even want to convert a series of functions into a class so you don't need to pass parameters through all of them. + +### Use simple indenting + +The ERPNext and Frappe Project uses tabs (I know and we are sorry, but its too much effort to change it now and we don't want to lose the history). The indentation must be consistent whether you are writing Javascript or Python. Multi-line strings or expressions must also be consistently indented, not hanging like a bee hive at the end of the line. We just think the code looks a lot more stable that way. + +This is wrong: + +```py +frappe.db.sql("""select item_name, description, default_warehouse from tabItem + where disabled = 0""") +``` + +This is better: + +```py +frappe.db.sql("""select + item_name, description, default_warehouse + from + tabItem + where + disabled = 0""") +``` + + +### Make your SQLs injection proof + +If you are writing direct SQL query, don't use `.format` to replace strings. Example `frappe.db.sql('select age from tabUser where name='{}'.format(user))` is wrong. It should be `frappe.db.sql('select age from tabUser where name=%s', user)` + +### Use simple structures + +Code must be easy to read, so don't try clubbing multiple conditions in one line and make it complex. If you have a line that has multiple AND or OR, break it up into multiple conditions. + +### Function sequence + +If you have multiple functions in a file, the most significant function should be on the top and the inner functions should be below. Check the example below: ``` def fa(): fb()