Skip to content

Coding Standards

Nabin Hait edited this page Jun 20, 2018 · 15 revisions

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. Examples:

  • $c_obj()
  • cur_frm
  • get_query
  • add_fetch

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:

frappe.db.sql("""select item_name, description, default_warehouse from tabItem 
                                    where disabled = 0""")              

This is better:

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()
  fc()

def fb():
  pass

def fc():
  pass
Clone this wiki locally