#Are there any functions I can use?

1 messages ยท Page 1 of 1 (latest)

karmic remnantBOT
#

<@&987246399047479336> please have a look, thanks.

#

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>.

#
TJ-Bot
Are there any additional functions I can add to my code to make it shorter?

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:

  1. String methods such as substring(), length(), and equals().
  2. Math methods like Math.abs(), Math.pow(), and Math.max().
  3. 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:

  1. Identify repetitive or complex tasks that can be encapsulated in a function.
  2. Define the function with a meaningful name and parameters that capture the necessary inputs.
  3. Implement the logic inside the function body.
  4. 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.

remote roost
karmic remnantBOT
wraith shale
#

๐Ÿ’€

muted bramble
#

So effectively "Please code-review ~170KB of code, with no detail of requirements, and suggest improvements"

You may not get many takers.

remote roost
#

no classes

#

I just want any suggestions for possible functions I can make to shorten the code

muted bramble
#

I just downloaded to give you a sense of the request. That's 3000+ lines of code.

remote roost
#

alot of it is repetitive

muted bramble
#

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.

remote roost
#

ight mate

#

Thanks

#

k\

muted bramble
#

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.

remote roost
#

eh

#

I cant used what the teacher doesnt let me

#

And the teacher didnt teach that so

#

its a real pain in the neck

muted bramble
#

Then how can we help if we don't know the requirements/constraints.

remote roost
#

I just asked

#

if any repetitive code

#

can be made into a function

muted bramble
#

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) {`
lusty sparrow
#

also these if statements look like they use hardcoded values

remote roost
#

yeah

#

cuz there aint rweally a pattern to it

#

so I had to

lusty sparrow
#

I cant tell but there most likely is some pattern

remote roost
#

hmmmmmmmm

#

nah there aint

lusty sparrow
#

how do you know?

remote roost
#

cuz I never tried

lusty sparrow
#

also why not using classes?

remote roost
#

ima try

remote roost
#

I genuinly dont know

#

they also came up with this rule where we cant use while (true)

#

cuz apparently its not good for coding?

lusty sparrow
#

it is not good

remote roost
#

soo now everytime I wanna do while true I hvae to set a boolean value to true

#

and then do while(boolean value)

lusty sparrow
#

but I wouldnt care if the I see a file with 3k + lines of unreadable code

lusty sparrow
remote roost
#

yeah

lusty sparrow
#

so you kinda still against that rule

remote roost
#

so I did that

#

nah

lusty sparrow
#

you shouldnt

remote roost
#

teahcer says its allowed

#

lmao

#

thats what confuses me

lusty sparrow
#

I doubt he quite understood what you are doing

remote roost
#

no

#

the teacher did it herself

lusty sparrow
#

whatever

#

still dont do it

#

that just indicates bad code

#

btw do you know about generics?

remote roost
#

nah

lusty sparrow
#

also follow naming conventions

remote roost
#

send link

lusty sparrow
#

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) {
karmic remnantBOT
#

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

remote roost
#

yay

#

thank you

turbid lagoon
#

for example java k == 4 || k == 12 || k == 20 || k == 28 || k == 36

remote roost
#

I know

#

I know there is

#

im just too lazy

turbid lagoon
#

so why would you say "no they aren't"

lusty sparrow
#

BRO

#

you really asking for help

#

but then

#

ignore the advice

#

lel

remote roost
#

cuz that part wasnt important

#

lmao

#

I alreayd knew

#

so I didnt wanna focus on dat part

#

yk

turbid lagoon
#

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

lusty sparrow
#

also dont create multiple scanner that use System.in

turbid lagoon
#

you aren't going to be able to create decent functions if you're too lazy to focus on patterns

lusty sparrow
#
            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

remote roost
lusty sparrow
#

and pass one instance

remote roost
#

how

#

I just learned java like 3 or 4 months ago

remote roost
lusty sparrow
#

be careful with operator precedence

turbid lagoon
#

no, you'd check for a range

#

because there's another pattern which is the same, but +4 instead

remote roost
#

okey

remote roost
lusty sparrow
#

its saying which operator gets applied first

#

k - 68 % 8 == 0 is the same as k - (68 % 8) == 0

#

I doubt you want that

remote roost
#

yuh

lusty sparrow
#

but whatever that pattern seems not correct in the first place

remote roost
#

ig

turbid lagoon
remote roost
#

huh

turbid lagoon
#

we are here to help you with your problem, no need for random memes. it's easiest if everyone stays focused on the problem

remote roost
#

ok

lusty sparrow
#

seems like bro doesnt want help

remote roost
#

i do

#

wait

#

nvm

#

I got it

#

how do I close post

lusty sparrow
#

/help-thread close

#

but I still got some feedback

#

seems like this part could fit into a method

remote roost
#

hmm

#

why would I want that part to be a method lol

#

it only goes once

lusty sparrow
#

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

remote roost
#

arent methods used to shorten code

lusty sparrow
#

also

#

but not only

remote roost
#

if I wanted to name stuff then everything would be a method

#

lmao

#

isnt that what comments are for

lusty sparrow
#

no

#

thats a way to avoid comments

#

you dont always want to look into the implementation of a piece of code

remote roost
#

ill take that advice

lusty sparrow
#

a descriptive name works fine most of the time

remote roost
#

thx]

#

can we keep this thread open so I can come back to it and see the advice

lusty sparrow
#

yea

remote roost
#

thanks

lusty sparrow
#

like this part

#

move it into a method

#

and give it a descriptive name

#

and then call it there

remote roost
#

ight

#

that will look alot cleaner too right

lusty sparrow
#

yeah

remote roost
#

in the main

#

yuhhh ic ic

#

why is it bad to use while true though?

lusty sparrow
#

prefer using existing methods like String#isBlank or isEmpty depending what you want

remote roost
#

we didnt learn that

#

therefore the teacher will take marks off

#

for using it

lusty sparrow
#

you dont need to learn that to use it

remote roost
#

I know

lusty sparrow
#

you learned strings right?

remote roost
#

we just have to use stuff the teacher shoewd us

#

yeah

#

dude

#

we learned arrays

lusty sparrow
#

so you can use methods from that class

remote roost
#

but I cant use a 2d array

remote roost
#

i dont think I can

lusty sparrow
#

some might say you should use !in_str_array(...) instead, imo both are fine for readability

remote roost
#

cuz if she aint allowin 2d arrays...

remote roost
#

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

lusty sparrow
#

it would be inStrArray, though I would rename it to like containsString or similar

remote roost
#

oh thats good

#

bro is giving me such good tips ngl

lusty sparrow
#

use a switch statement instead if you learned about it

remote roost
#

I didnt

lusty sparrow
#

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

remote roost
#

yeah but

#

it can be repeated

#

actually

lusty sparrow
#

wdym?

remote roost
#

idk if it can

#

cuz

#

they are different variables

#

like g1, g2, g3

lusty sparrow
#

and?

#

make parameters for that

remote roost
#

show example

lusty sparrow
#

and put the different ones depending on the situtation

lusty sparrow
#

just checked

#

they got the exact same code

#

just operating on a different variable

remote roost
#

wait

#

oh shit

#

yeah

lusty sparrow
#

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

remote roost
#

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?>

lusty sparrow
#

wdym?

#

you pass a different variable

remote roost
#

huh

#

say r1 is in the main method

#

and I put r1 in a dif method for movement

lusty sparrow
#

I have no idea what r1 is

#

that variable name is so bad

remote roost
#

red 1

#

red piece 1

#

ik its just easy fo rme

#

ill do ctrl h and change it after

lusty sparrow
#

I cant really understand about what you are talking about in the 3k+ lines

remote roost
#

r1 is just easier to type

#

its the sorry board game

lusty sparrow
#

if you put that repetition into a method then you easily get -1k lines

#

even more

past creek
#

Your ide will even likely suggest it. There's a lot of duplication.

lusty sparrow
#

everything from line 400 smth -> 3100 is repetition

#

you can save 2500 lines here

#

lmao

remote roost
#

gimme 5 mins ill brb

#

i gotta eat

lusty sparrow
#

all good

lusty sparrow
past creek
remote roost
#

im back

#

@lusty sparrow

#

I use vscode

#

whats wrong with vscode mate

lusty sparrow
#

not something I would recommend for java

remote roost
#

also

#

uh

#

can you help me figure out the patturn

#

pattern

#

Im doing smth else rn

past creek
past creek
#

There's a free version.

remote roost
#

/tag intellijce

karmic remnantBOT
remote roost
lusty sparrow
#

get rid of the repetition

remote roost
#

Idk how brooooo

#

๐Ÿ˜ญ

lusty sparrow
#

sorry but I wont do it for you

remote roost
#

i know

#

Im just asking

lusty sparrow
#

check whats similar and make a method

remote roost
#

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!!!!

lusty sparrow
#

let me show you smth

#

also is there no else if check for r2?

remote roost
#

uhm

#

there is

lusty sparrow
#

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 peepo_think

#

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

remote roost
#

imasee eq

#

@lusty sparrow is there any conventional naming structure for variables