#Are there any functions I can use?
1 messages ยท Page 1 of 1 (latest)
Here is an AI assisted attempt to answer your question ๐ค. Maybe it helps! In any case, a human is on the way ๐. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.
Yes, there are many built-in functions in Java that you can use to make your code more concise and efficient. Some commonly used functions include:
- String methods such as substring(), length(), and equals().
- Math methods like Math.abs(), Math.pow(), and Math.max().
- Arrays methods such as Arrays.sort(), Arrays.toString(), and Arrays.copyOf().
In addition to built-in functions, you can also create your own custom functions to further streamline your code. By breaking down complex logic into smaller, reusable functions, you can make your code more readable and maintainable.
To add additional functions to your code, you can follow these steps:
- Identify repetitive or complex tasks that can be encapsulated in a function.
- Define the function with a meaningful name and parameters that capture the necessary inputs.
- Implement the logic inside the function body.
- Call the function whenever that specific task needs to be performed.
By using built-in and custom functions effectively, you can write cleaner, more concise code that is easier to understand and maintain in the long run.
I uploaded your attachments as Gist.
๐
So effectively "Please code-review ~170KB of code, with no detail of requirements, and suggest improvements"
You may not get many takers.
ight so
no classes
I just want any suggestions for possible functions I can make to shorten the code
I just downloaded to give you a sense of the request. That's 3000+ lines of code.
alot of it is repetitive
And if you have a specific area of repetition you'd like to ask about someone would have something on which to focus. We could probably pore over 3000 lines looking at improvements for hours at least.
Here's one. Using Random is usually the wrong choice. In places where you really want a random number ThreadLocalRandom.current() is usually preferred. However in this case you can use the built in Collections.shuffle combined with Arrays.asList.
eh
I cant used what the teacher doesnt let me
And the teacher didnt teach that so
its a real pain in the neck
Then how can we help if we don't know the requirements/constraints.
How about. Use clearer names for params or add a method comment to explain them.
eg
`public static void board(int r1, int r2, int r3, int r4, int g1, int g2, int g3, int g4, int y1, int y2, int y3, int y4, int b1, int b2, int b3, int b4) {`
also these if statements look like they use hardcoded values
oh yuh
yeah
cuz there aint rweally a pattern to it
so I had to
I cant tell but there most likely is some pattern
how do you know?
cuz I never tried
also why not using classes?
ima try
stupid rules
I genuinly dont know
they also came up with this rule where we cant use while (true)
cuz apparently its not good for coding?
it is not good
soo now everytime I wanna do while true I hvae to set a boolean value to true
and then do while(boolean value)
but I wouldnt care if the I see a file with 3k + lines of unreadable code
its the same thing as while(true)
yeah
so you kinda still against that rule
you shouldnt
I doubt he quite understood what you are doing
whatever
still dont do it
that just indicates bad code
btw do you know about generics?
nah
also follow naming conventions
send link
and rename this method to fill or similar:
// makes all the values in a string list into anything other then null
public static String[] make_str_not_null(String[] array, String replacement) {
The naming convention in Java is as follows:
Classes:
PascalCase. Example: GoldMiner, FoodDestroyer, DispenserBuilder
Methods / Fields / Variables:
camelCase. Example: foodAmount, integerValue, goodBoyAsADog
Packages:
All lowercase. Preferably the domain name backwards. Example: "google.com" ==> com.google, "ialistannen.me" ==> me.ialistannen
Followed by the project name. Example: Project "Builder" ==> com.google.builder
Constants/Enum entries:
UPPER_SNAKE_CASE. Where words are separated by underscores. Example: RED_DOG, I_AM_THE_BEST, CONQUER_WORLD
yeah there are
for example java k == 4 || k == 12 || k == 20 || k == 28 || k == 36
so why would you say "no they aren't"
cuz that part wasnt important
lmao
I alreayd knew
so I didnt wanna focus on dat part
yk
you could write a function for a check like this
which can be applied to multiple areas
k == 68 || k == 76 || k == 84 || k == 92```
same pattern
also dont create multiple scanner that use System.in
you aren't going to be able to create decent functions if you're too lazy to focus on patterns
if (is_human) {
System.out.print("Which pawn would you like to move? ");
pawn_move = scan.nextLine();
}
if (!is_human) {
int rand_index = random.nextInt(pawns.length);
pawn_move = pawns[rand_index];
}
use if else
cant use global variable
so if k - 68 % 8 == 0
be careful with operator precedence
no, you'd check for a range
because there's another pattern which is the same, but +4 instead
okey
whats dat
its saying which operator gets applied first
k - 68 % 8 == 0 is the same as k - (68 % 8) == 0
I doubt you want that
yuh
but whatever that pattern seems not correct in the first place
ig
please do not spam
huh
we are here to help you with your problem, no need for random memes. it's easiest if everyone stays focused on the problem
seems like bro doesnt want help
/help-thread close
but I still got some feedback
seems like this part could fit into a method
yeah but it gives that part of code a name
a method doesnt always need to be reused
its also there to structure your code
arent methods used to shorten code
if I wanted to name stuff then everything would be a method
lmao
isnt that what comments are for
no
thats a way to avoid comments
you dont always want to look into the implementation of a piece of code
ill take that advice
a descriptive name works fine most of the time
yea
thanks
like this part
move it into a method
and give it a descriptive name
and then call it there
yeah
prefer using existing methods like String#isBlank or isEmpty depending what you want
you dont need to learn that to use it
I know
you learned strings right?
so you can use methods from that class
but I cant use a 2d array
some might say you should use !in_str_array(...) instead, imo both are fine for readability
cuz if she aint allowin 2d arrays...
yeah
I was gonnaa change it to that
thanks tho
also
I was thinking
changing in_str_array to
InStrArray
or how do I name methods again?
with 3 words
it would be inStrArray, though I would rename it to like containsString or similar
use a switch statement instead if you learned about it
I didnt
also I see a lot of else if for pawn move, you have a unique implementation for each g1, g2, g3 etc
they almost got the identical code, make a method for it instead
they might even got the exact same code
cant tell
wdym?
show example
and put the different ones depending on the situtation
?
just checked
they got the exact same code
just operating on a different variable
so all g1, g2, g3, g4 should share a method
then y1, y2, y3, y4 should share a method etc
might even be able to combine them
into a single method
and then pass different variables depending on the situtation
how do I have y1 in the main equal y1 in the method?
then when its printed to board wont it not be the same?>
I cant really understand about what you are talking about in the 3k+ lines
Your ide will even likely suggest it. There's a lot of duplication.
everything from line 400 smth -> 3100 is repetition
you can save 2500 lines here
lmao
all good
I doubt IDE will work properly in that mess lmao
I've seen intelliJ do some wonders, it might take a bit to analyze though.
not something I would recommend for java
ah
also
uh
can you help me figure out the patturn
pattern
Im doing smth else rn
IntelliJ offers a lot more advice. For example for refactoring.
is it payed
There's a free version.
/tag intellijce
how
get rid of the repetition
sorry but I wont do it for you
check whats similar and make a method
variables like r1, r2, r3 are in main method which are used in the board method
how do I return r1, r2, r3 from one method to the main method on the same variable?
its not possible!!!!
check this
its the difference of the r1 vs r4
and I am pretty sure the difference is similar to all r variables
also it seems like the r4 logic is not complete
based on the comment
though I see what you mean
it changes two variable values 
which makes it kind cancer
but that only happens for the later part
so you can at least make a big method for the first part
and then return the new value of r
and the swap logic changes both values

