Skip to content

Commit e444bcc

Browse files
authored
Merge pull request github#3634 from Marcono1234/MagicConstants-code-style
Fix Java code style of MagicConstants examples
2 parents d4e1ee8 + ad1146a commit e444bcc

File tree

6 files changed

+36
-40
lines changed

6 files changed

+36
-40
lines changed

java/ql/src/Violations of Best Practice/Magic Constants/MagicConstantsNumbers.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// Problem version
22
public class MagicConstants
33
{
4-
final static public String IP = "127.0.0.1";
5-
final static public int PORT = 8080;
6-
final static public String USERNAME = "test";
4+
public static final String IP = "127.0.0.1";
5+
public static final int PORT = 8080;
6+
public static final String USERNAME = "test";
77

88
public void serve(String ip, int port, String user, int timeout) {
99
// ...
@@ -20,17 +20,16 @@ public static void main(String[] args) {
2020
// Fixed version
2121
public class MagicConstants
2222
{
23-
final static public String IP = "127.0.0.1";
24-
final static public int PORT = 8080;
25-
final static public String USERNAME = "test";
26-
final static public int TIMEOUT = 60000; // Magic number is replaced by named constant
23+
public static final String IP = "127.0.0.1";
24+
public static final int PORT = 8080;
25+
public static final String USERNAME = "test";
26+
public static final int TIMEOUT = 60000; // Magic number is replaced by named constant
2727

2828
public void serve(String ip, int port, String user, int timeout) {
2929
// ...
3030
}
3131

3232
public static void main(String[] args) {
33-
3433
new MagicConstants().serve(IP, PORT, USERNAME, TIMEOUT); // Use 'TIMEOUT' constant
3534
}
3635
}

java/ql/src/Violations of Best Practice/Magic Constants/MagicConstantsString.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// Problem version
22
public class MagicConstants
33
{
4-
final static public String IP = "127.0.0.1";
5-
final static public int PORT = 8080;
6-
final static public int TIMEOUT = 60000;
4+
public static final String IP = "127.0.0.1";
5+
public static final int PORT = 8080;
6+
public static final int TIMEOUT = 60000;
77

88
public void serve(String ip, int port, String user, int timeout) {
99
// ...
@@ -20,17 +20,16 @@ public static void main(String[] args) {
2020
// Fixed version
2121
public class MagicConstants
2222
{
23-
final static public String IP = "127.0.0.1";
24-
final static public int PORT = 8080;
25-
final static public int USERNAME = "test"; // Magic string is replaced by named constant
26-
final static public int TIMEOUT = 60000;
23+
public static final String IP = "127.0.0.1";
24+
public static final int PORT = 8080;
25+
public static final int USERNAME = "test"; // Magic string is replaced by named constant
26+
public static final int TIMEOUT = 60000;
2727

2828
public void serve(String ip, int port, String user, int timeout) {
2929
// ...
3030
}
3131

3232
public static void main(String[] args) {
33-
3433
new MagicConstants().serve(IP, PORT, USERNAME, TIMEOUT); // Use 'USERNAME' constant
3534
}
3635
}
Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,36 @@
11
// Problem version
22
public class MagicConstants
33
{
4-
final static public String IP = "127.0.0.1";
5-
final static public int PORT = 8080;
6-
final static public String USERNAME = "test";
7-
final static public int TIMEOUT = 60000;
4+
public static final String IP = "127.0.0.1";
5+
public static final int PORT = 8080;
6+
public static final String USERNAME = "test";
7+
public static final int TIMEOUT = 60000;
88

99
public void serve(String ip, int port, String user, int timeout) {
1010
// ...
1111
}
1212

1313
public static void main(String[] args) {
14-
int internal_port = 8080; // AVOID: Magic number
14+
int internalPort = 8080; // AVOID: Magic number
1515

16-
new MagicConstants().serve(IP, internal_port, USERNAME, TIMEOUT);
16+
new MagicConstants().serve(IP, internalPort, USERNAME, TIMEOUT);
1717
}
1818
}
1919

2020

2121
// Fixed version
2222
public class MagicConstants
2323
{
24-
final static public String IP = "127.0.0.1";
25-
final static public int PORT = 8080;
26-
final static public String USERNAME = "test";
27-
final static public int TIMEOUT = 60000;
24+
public static final String IP = "127.0.0.1";
25+
public static final int PORT = 8080;
26+
public static final String USERNAME = "test";
27+
public static final int TIMEOUT = 60000;
2828

2929
public void serve(String ip, int port, String user, int timeout) {
3030
// ...
3131
}
3232

3333
public static void main(String[] args) {
34-
3534
new MagicConstants().serve(IP, PORT, USERNAME, TIMEOUT); // Use 'PORT' constant
3635
}
3736
}

java/ql/src/Violations of Best Practice/Magic Constants/MagicNumbersUseConstant.qhelp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ update if the requirements change, because you have to update the number in only
3535
</recommendation>
3636
<example>
3737

38-
<p>The following example shows a magic number <code>internal_port</code>. This should be replaced by
38+
<p>The following example shows a magic number <code>internalPort</code>. This should be replaced by
3939
the existing named constant, as shown in the fixed version.</p>
4040

4141
<sample src="MagicNumbersUseConstant.java" />
Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,36 @@
11
// Problem version
22
public class MagicConstants
33
{
4-
final static public String IP = "127.0.0.1";
5-
final static public int PORT = 8080;
6-
final static public String USERNAME = "test";
7-
final static public int TIMEOUT = 60000;
4+
public static final String IP = "127.0.0.1";
5+
public static final int PORT = 8080;
6+
public static final String USERNAME = "test";
7+
public static final int TIMEOUT = 60000;
88

99
public void serve(String ip, int port, String user, int timeout) {
1010
// ...
1111
}
1212

1313
public static void main(String[] args) {
14-
String internal_ip = "127.0.0.1"; // AVOID: Magic string
14+
String internalIp = "127.0.0.1"; // AVOID: Magic string
1515

16-
new MagicConstants().serve(internal_ip, PORT, USERNAME, TIMEOUT);
16+
new MagicConstants().serve(internalIp, PORT, USERNAME, TIMEOUT);
1717
}
1818
}
1919

2020

2121
// Fixed version
2222
public class MagicConstants
2323
{
24-
final static public String IP = "127.0.0.1";
25-
final static public int PORT = 8080;
26-
final static public String USERNAME = "test";
27-
final static public int TIMEOUT = 60000;
24+
public static final String IP = "127.0.0.1";
25+
public static final int PORT = 8080;
26+
public static final String USERNAME = "test";
27+
public static final int TIMEOUT = 60000;
2828

2929
public void serve(String ip, int port, String user, int timeout) {
3030
// ...
3131
}
3232

3333
public static void main(String[] args) {
34-
3534
new MagicConstants().serve(IP, PORT, USERNAME, TIMEOUT); //Use 'IP' constant
3635
}
3736
}

java/ql/src/Violations of Best Practice/Magic Constants/MagicStringsUseConstant.qhelp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ update if the requirements change, because you have to update the string in only
3535
</recommendation>
3636
<example>
3737

38-
<p>The following example shows a magic string <code>internal_ip</code>. This should be replaced by
38+
<p>The following example shows a magic string <code>internalIp</code>. This should be replaced by
3939
the existing named constant, as shown in the fixed version.</p>
4040

4141
<sample src="MagicStringsUseConstant.java" />

0 commit comments

Comments
 (0)