Skip to content

Commit 364d489

Browse files
authored
Merge pull request github#3810 from dilanbhalla/syntaxpython
Python: Function/Class Naming Convention (Syntax)
2 parents 2d618d6 + d73ba13 commit 364d489

10 files changed

+140
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<!DOCTYPE qhelp PUBLIC
2+
"-//Semmle//qhelp//EN"
3+
"qhelp.dtd">
4+
<qhelp>
5+
6+
7+
<overview>
8+
<p>A class name that begins with a lowercase letter does not follow standard
9+
naming conventions. This decreases code readability. For example, <code>class background</code>.
10+
</p>
11+
12+
</overview>
13+
<recommendation>
14+
15+
<p>
16+
Write the class name beginning with an uppercase letter. For example, <code>class Background</code>.
17+
</p>
18+
19+
</recommendation>
20+
21+
<references>
22+
23+
<li>
24+
Guido van Rossum, Barry Warsaw, Nick Coghlan <em>PEP 8 -- Style Guide for Python Code</em>
25+
<a href="https://www.python.org/dev/peps/pep-0008/#class-names">Python Class Names</a>
26+
</li>
27+
28+
</references>
29+
30+
</qhelp>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @name Misnamed class
3+
* @description A class name that begins with a lowercase letter decreases readability.
4+
* @kind problem
5+
* @problem.severity recommendation
6+
* @id py/misnamed-class
7+
* @tags maintainability
8+
*/
9+
10+
import python
11+
12+
predicate lower_case_class(Class c) {
13+
exists(string first_char |
14+
first_char = c.getName().prefix(1) and
15+
not first_char = first_char.toUpperCase()
16+
)
17+
}
18+
19+
from Class c
20+
where
21+
c.inSource() and
22+
lower_case_class(c) and
23+
not exists(Class c1 |
24+
c1 != c and
25+
c1.getLocation().getFile() = c.getLocation().getFile() and
26+
lower_case_class(c1)
27+
)
28+
select c, "Class names should start in uppercase."
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<!DOCTYPE qhelp PUBLIC
2+
"-//Semmle//qhelp//EN"
3+
"qhelp.dtd">
4+
<qhelp>
5+
6+
7+
<overview>
8+
<p>A function name that begins with an uppercase letter does not follow standard
9+
naming conventions. This decreases code readability. For example, <code>Jump</code>.
10+
</p>
11+
12+
</overview>
13+
<recommendation>
14+
15+
<p>
16+
Write the function name beginning with an lowercase letter. For example, <code>jump</code>.
17+
</p>
18+
19+
</recommendation>
20+
21+
<references>
22+
23+
<li>
24+
Guido van Rossum, Barry Warsaw, Nick Coghlan <em>PEP 8 -- Style Guide for Python Code</em>
25+
<a href="https://www.python.org/dev/peps/pep-0008/#function-and-variable-names">Python Function and Variable Names</a>
26+
</li>
27+
28+
</references>
29+
30+
</qhelp>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @name Misnamed function
3+
* @description A function name that begins with an uppercase letter decreases readability.
4+
* @kind problem
5+
* @problem.severity recommendation
6+
* @id py/misnamed-function
7+
* @tags maintainability
8+
*/
9+
10+
import python
11+
12+
predicate upper_case_function(Function func) {
13+
exists(string first_char |
14+
first_char = func.getName().prefix(1) and
15+
not first_char = first_char.toLowerCase()
16+
)
17+
}
18+
19+
from Function func
20+
where
21+
func.inSource() and
22+
upper_case_function(func) and
23+
not exists(Function func1 |
24+
func1 != func and
25+
func1.getLocation().getFile() = func.getLocation().getFile() and
26+
upper_case_function(func1)
27+
)
28+
select func, "Function names should start in lowercase."
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| NamingConventionsClasses.py:2:1:2:14 | Class badName | Class names should start in uppercase. |
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# BAD, do not start class or interface name with lowercase letter
2+
class badName:
3+
4+
def hello(self):
5+
print("hello")
6+
7+
# Good, class name starts with capital letter
8+
class GoodName:
9+
10+
def hello(self):
11+
print("hello")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
experimental/Classes/NamingConventionsClasses.ql
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
| NamingConventionsFunctions.py:4:5:4:25 | Function HelloWorld | Function names should start in lowercase. |
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Test:
2+
3+
# BAD, do not start function name with uppercase letter
4+
def HelloWorld(self):
5+
print("hello world")
6+
7+
# GOOD, function name starts with lowercase letter
8+
def hello_world(self):
9+
print("hello world")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
experimental/Functions/NamingConventionsFunctions.ql

0 commit comments

Comments
 (0)