Advices for my code


#1

Hi everyone, i recieved a lot of help from here and i want to show you what i did for now with your help.

here is a video i made:

here is my code:

So here i my question, im self-educated in java and processing and i wonder what i can do best with my code.
I dont ask you to redo my code, i just want advices, what can i do better or things i should not do.

I plan to make a mini survival game with 10 creatures and a least 5 to 10 plants and a player with basic crafting.

Tanks you all for your times.


#2

Happy to see that you manage to finish what you wanted to do.

The kind of question you are asking: “what can I do better” is really difficult to answer. There are always different ways of doing something and often you have pros and cons for each method.

I would say that as long as it works the way you want you are good to go. Move on to your next project. Maybe then you will realize that something you have done a certain way before is not working for this project so you’ll need to change it, maybe you will run into another problem that you had never found before and you will have to find a new solution…

In the end, the more you do, the better you will get at it so don’t worry about improving your current code, if it works it’s good :slight_smile:


#3

Thanks,

i know its a difficult question, i just wonder, maybe someone are gonna check it and see something and think “ho thats a really bad habit”.

i gonna keep going on my project thanks for your help :slight_smile:


#4

First, if your code works and the way you are writing it works for you, take all advice with a grain of salt.

That said, there are a lot of practices that might make your life easier – help you write better, faster, and more reliably etc.

Are you using Eclipse, IntelliJ, Netbeans or something else?

Here are a few starting suggestions:

  1. comments: at a minimum, add a comment to the top of each class describing what it does. How is “Food” different from “Food2”? Especially if you want advice and feedback, this will make it much easier for people to help you. It will also make it easier for you to understand your own code after you have been away from it for a few months. Ideally, you should get in the habit of adding at least single line javadoc comment to every method, no matter how obvious it seems.

  2. Use a code linter, and for most things leave it set to defaults. For example, only 10% of Java projects use tabs the way you are – the vast majority use 4 spaces for code indenting. You may feel passionately about tabs – but if you don’t, doing what everybody else does reduces friction. https://ukupat.github.io/tabs-or-spaces/

  3. Avoid things like referencing an F2 object and an f2 object in the same line

    https://github.com/strebellev/ecosystem-v2/blob/master/src/objects/Vegetal1.java#L47

    – in fact, don’t name objects F2 at all. You don’t need to save space that badly. Name it something like food2List and food2Item – or better yet, don’t name your classes Food2, and then your object names will make a lot more sense. What is a Food2?

  4. Comment out stub methods on concrete classes until you have actually written them, or keep them in a branch (are you using version control?). Player.update() doesn’t do anything

  5. You have a lot of related classes – like 3 Carnivore classes – but they don’t use either inheritance or interfaces to organize shared code. That makes it a lot harder to keep them in sync and a lot harder to debug them if you copied a new feature to two of them, but not the third. Consider writing an interface Carnivorous and then using implements Carnivorous on each of your classes.


#5

Hi sorry for the delay,

yes im using Eclipse,

1: ok i gonna put more comment and be more specific
2: wow i dont even know this exist :slight_smile:
3: yeah its confusing
4: no i dont use it, i gonna check
5: yeah sorry i dont clean a lot my code, i gonna check for interface

Thanks for your advices, my code gonna be a bit better now.


#6

Looks like an awesome project! I’d second everything that’s already been said.

I’d also like to add a couple tips given I was self taught for the first year or so of learning to code. I’d say the hardest thing about being self taught is you end up working in a little bubble. In a bubble you don’t learn good documentation practices and you don’t learn to read other people’s code. Which isn’t necessarily a problem until you want to collaborate on bigger projects.

So I’d definitely encourage you to practice descriptive class and variable names. This is perhaps the simplest form of documentation. It makes code way more readable. Beyond that generating Javadocs is a great way to get some distance from your code and see how others would understand it upon first reading it. And when I first did it my documentation went to a whole new level. Here’s a tutorial on doing that in eclipse but there’s tons of information about Javadocs out there if you’re interested.

A great way to learn how to read other people’s code is to get involved in an opensource project. Which can be really intimidating but this is one I saw recently that you might find interesting and it’s not super large so hopefully it’s less intimidating. And a bonus of reading other’s code is you start learning about things you never knew about.


#7

Thanks for yours advices, the next version will be better with yours helps.
Yes thats why i show my code so early with a lot of mess and errors, to learn what i should do and not :slight_smile:

I gonna follow the project, like you said, its a good exercice.

Merry christmass and happy new year to you all.