prefixes[rand.nextInt(prefixes.length)]
You have this pattern many many times in your code, and this is a small code duplication in this line as prefixes
is repeated twice. If you ever would accidentally change one of them but not the other, you'd end up with a major ArrayIndexOutOfBoundsException
.
I think this is enough to warrant a method extraction. (This is a slightly modified variant of a real method I wrote years ago. I find it immensely useful)
public static <E> E randomElement(E[] list, Random random) {
if (list == null || list.length == 0) {
return null;
}
return list[random.nextInt(list.length)];
}
It's up to you how you want to treat the handling of null elements there, although as you always use the same random
variable your method can be:
public <E> E randomElement(E[] list) {
return list[rand.nextInt(list.length)];
}
Now you can use it like this:
str.append(randomElement(prefixes));
str.append(randomElement(postfixes1));
str.append(randomElement(people)); // with the name change suggestion mentioned by @rolfl
if (rand.nextInt(100) < 70) {
Randomizing a boolean that has 70% chance of being true is more commonly written as
if (rand.nextDouble() < 0.7) {
This makes it easier to see the direct connection to the exact percentage, and it also reduces your dependency on the "magic number" 100 "magic number" 100
prefixes[rand.nextInt(prefixes.length)]
You have this pattern many many times in your code, and this is a small code duplication in this line as prefixes
is repeated twice. If you ever would accidentally change one of them but not the other, you'd end up with a major ArrayIndexOutOfBoundsException
.
I think this is enough to warrant a method extraction. (This is a slightly modified variant of a real method I wrote years ago. I find it immensely useful)
public static <E> E randomElement(E[] list, Random random) {
if (list == null || list.length == 0) {
return null;
}
return list[random.nextInt(list.length)];
}
It's up to you how you want to treat the handling of null elements there, although as you always use the same random
variable your method can be:
public <E> E randomElement(E[] list) {
return list[rand.nextInt(list.length)];
}
Now you can use it like this:
str.append(randomElement(prefixes));
str.append(randomElement(postfixes1));
str.append(randomElement(people)); // with the name change suggestion mentioned by @rolfl
if (rand.nextInt(100) < 70) {
Randomizing a boolean that has 70% chance of being true is more commonly written as
if (rand.nextDouble() < 0.7) {
This makes it easier to see the direct connection to the exact percentage, and it also reduces your dependency on the "magic number" 100
prefixes[rand.nextInt(prefixes.length)]
You have this pattern many many times in your code, and this is a small code duplication in this line as prefixes
is repeated twice. If you ever would accidentally change one of them but not the other, you'd end up with a major ArrayIndexOutOfBoundsException
.
I think this is enough to warrant a method extraction. (This is a slightly modified variant of a real method I wrote years ago. I find it immensely useful)
public static <E> E randomElement(E[] list, Random random) {
if (list == null || list.length == 0) {
return null;
}
return list[random.nextInt(list.length)];
}
It's up to you how you want to treat the handling of null elements there, although as you always use the same random
variable your method can be:
public <E> E randomElement(E[] list) {
return list[rand.nextInt(list.length)];
}
Now you can use it like this:
str.append(randomElement(prefixes));
str.append(randomElement(postfixes1));
str.append(randomElement(people)); // with the name change suggestion mentioned by @rolfl
if (rand.nextInt(100) < 70) {
Randomizing a boolean that has 70% chance of being true is more commonly written as
if (rand.nextDouble() < 0.7) {
This makes it easier to see the direct connection to the exact percentage, and it also reduces your dependency on the "magic number" 100
prefixes[rand.nextInt(prefixes.length)]
You have this pattern many many times in your code, and this is a small code duplication in this line as prefixes
is repeated twice. If you ever would accidentally change one of them but not the other, you'd end up with a major ArrayIndexOutOfBoundsException
.
I think this is enough to warrant a method extraction. (This is a slightly modified variant of a real method I wrote years ago. I find it immensely useful)
public static <E> E randomElement(E[] list, Random random) {
if (list == null || list.length == 0) {
return null;
}
return list[random.nextInt(list.length)];
}
It's up to you how you want to treat the handling of null elements there, although as you always use the same random
variable your method can be:
public <E> E randomElement(E[] list) {
if (list == null || list.length == 0) {
return null;
}
return list[rand.nextInt(list.length)];
}
Now you can use it like this:
str.append(randomElement(prefixes));
str.append(randomElement(postfixes1));
str.append(randomElement(people)); // with the name change suggestion mentioned by @rolfl
if (rand.nextInt(100) < 70) {
Randomizing a boolean that has 70% chance of being true is more commonly written as
if (rand.nextDouble() < 0.7) {
This makes it easier to see the direct connection to the exact percentage, and it also reduces your dependency on the "magic number" 100
prefixes[rand.nextInt(prefixes.length)]
You have this pattern many many times in your code, and this is a small code duplication in this line as prefixes
is repeated twice. If you ever would accidentally change one of them but not the other, you'd end up with a major ArrayIndexOutOfBoundsException
.
I think this is enough to warrant a method extraction. (This is a slightly modified variant of a real method I wrote years ago. I find it immensely useful)
public static <E> E randomElement(E[] list, Random random) {
if (list == null || list.length == 0) {
return null;
}
return list[random.nextInt(list.length)];
}
It's up to you how you want to treat the handling of null elements there, although as you always use the same random
variable your method can be:
public <E> E randomElement(E[] list) {
if (list == null || list.length == 0) {
return null;
}
return list[rand.nextInt(list.length)];
}
Now you can use it like this:
str.append(randomElement(prefixes));
str.append(randomElement(postfixes1));
str.append(randomElement(people)); // with the name change suggestion mentioned by @rolfl
prefixes[rand.nextInt(prefixes.length)]
You have this pattern many many times in your code, and this is a small code duplication in this line as prefixes
is repeated twice. If you ever would accidentally change one of them but not the other, you'd end up with a major ArrayIndexOutOfBoundsException
.
I think this is enough to warrant a method extraction. (This is a slightly modified variant of a real method I wrote years ago. I find it immensely useful)
public static <E> E randomElement(E[] list, Random random) {
if (list == null || list.length == 0) {
return null;
}
return list[random.nextInt(list.length)];
}
It's up to you how you want to treat the handling of null elements there, although as you always use the same random
variable your method can be:
public <E> E randomElement(E[] list) {
return list[rand.nextInt(list.length)];
}
Now you can use it like this:
str.append(randomElement(prefixes));
str.append(randomElement(postfixes1));
str.append(randomElement(people)); // with the name change suggestion mentioned by @rolfl
if (rand.nextInt(100) < 70) {
Randomizing a boolean that has 70% chance of being true is more commonly written as
if (rand.nextDouble() < 0.7) {
This makes it easier to see the direct connection to the exact percentage, and it also reduces your dependency on the "magic number" 100
prefixes[rand.nextInt(prefixes.length)]
You have this pattern many many times in your code, and this is a small code duplication in this line as prefixes
is repeated twice. If you ever would accidentally change one of them but not the other, you'd end up with a major ArrayIndexOutOfBoundsException
.
I think this is enough to warrant a method extraction. (This is a slightly modified variant of a real method I wrote years ago. I find it immensely useful)
public static <E> E randomElement(E[] list, Random random) {
if (list == null || list.length == 0) {
return null;
}
return list[random.nextInt(list.length)];
}
It's up to you how you want to treat the handling of null elements there, although as you always use the same random
variable your method can be:
public <E> E randomElement(E[] list) {
if (list == null || list.length == 0) {
return null;
}
return list[rand.nextInt(list.length)];
}
Now you can use it like this:
str.append(randomElement(prefixes));
str.append(randomElement(postfixes1));
str.append(randomElement(people)); // with the name change suggestion mentioned by @rolfl