Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Problem version
public class MagicConstants
{
final static public String IP = "127.0.0.1";
final static public int PORT = 8080;
final static public String USERNAME = "test";
public static final String IP = "127.0.0.1";
public static final int PORT = 8080;
public static final String USERNAME = "test";

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

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

public static void main(String[] args) {

new MagicConstants().serve(IP, PORT, USERNAME, TIMEOUT); // Use 'TIMEOUT' constant
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Problem version
public class MagicConstants
{
final static public String IP = "127.0.0.1";
final static public int PORT = 8080;
final static public int TIMEOUT = 60000;
public static final String IP = "127.0.0.1";
public static final int PORT = 8080;
public static final int TIMEOUT = 60000;

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

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

public static void main(String[] args) {

new MagicConstants().serve(IP, PORT, USERNAME, TIMEOUT); // Use 'USERNAME' constant
}
}
Original file line number Diff line number Diff line change
@@ -1,37 +1,36 @@
// Problem version
public class MagicConstants
{
final static public String IP = "127.0.0.1";
final static public int PORT = 8080;
final static public String USERNAME = "test";
final static public int TIMEOUT = 60000;
public static final String IP = "127.0.0.1";
public static final int PORT = 8080;
public static final String USERNAME = "test";
public static final int TIMEOUT = 60000;

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

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

new MagicConstants().serve(IP, internal_port, USERNAME, TIMEOUT);
new MagicConstants().serve(IP, internalPort, USERNAME, TIMEOUT);
}
}


// Fixed version
public class MagicConstants
{
final static public String IP = "127.0.0.1";
final static public int PORT = 8080;
final static public String USERNAME = "test";
final static public int TIMEOUT = 60000;
public static final String IP = "127.0.0.1";
public static final int PORT = 8080;
public static final String USERNAME = "test";
public static final int TIMEOUT = 60000;

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

public static void main(String[] args) {

new MagicConstants().serve(IP, PORT, USERNAME, TIMEOUT); // Use 'PORT' constant
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ update if the requirements change, because you have to update the number in only
</recommendation>
<example>

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

<sample src="MagicNumbersUseConstant.java" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,37 +1,36 @@
// Problem version
public class MagicConstants
{
final static public String IP = "127.0.0.1";
final static public int PORT = 8080;
final static public String USERNAME = "test";
final static public int TIMEOUT = 60000;
public static final String IP = "127.0.0.1";
public static final int PORT = 8080;
public static final String USERNAME = "test";
public static final int TIMEOUT = 60000;

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

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

new MagicConstants().serve(internal_ip, PORT, USERNAME, TIMEOUT);
new MagicConstants().serve(internalIp, PORT, USERNAME, TIMEOUT);
}
}


// Fixed version
public class MagicConstants
{
final static public String IP = "127.0.0.1";
final static public int PORT = 8080;
final static public String USERNAME = "test";
final static public int TIMEOUT = 60000;
public static final String IP = "127.0.0.1";
public static final int PORT = 8080;
public static final String USERNAME = "test";
public static final int TIMEOUT = 60000;

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

public static void main(String[] args) {

new MagicConstants().serve(IP, PORT, USERNAME, TIMEOUT); //Use 'IP' constant
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ update if the requirements change, because you have to update the string in only
</recommendation>
<example>

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

<sample src="MagicStringsUseConstant.java" />
Expand Down