Problem with my code

Discussion in 'Programming' started by Alex0589, Jun 4, 2018.

  1. I'm about to finish a minigame plugin. The code is clean, I used descriptive names(exept in my Main class lol) and I think every developer can understand how it works. There's only a problem: my Main class. Here's the code I use to recreate every arena object after a restart or stop:
    Code (Text):
        private void recreateArenas() {
            File file = new File(getDataFolder(), "Arenas.yml");
            YamlConfiguration fc = YamlConfiguration.loadConfiguration(file);
            for (String name : fc.getStringList("Arena-list")) {
                File arenas = new File(getDataFolder() + File.separator + name + File.separator + "ArenaSettings.yml");
                YamlConfiguration fcn = YamlConfiguration.loadConfiguration(arenas);
                Arena newArena = new Arena(fcn.getString("Name"), fcn.getInt("minPlayers"), fcn.getInt("maxPlayers"), fcn.getInt("time"), checkLocationArena(name, "Lobby"), checkLocationArena(name, "Spectator"));
            }
        }
    Code (Text):
       private Location checkLocationArena(String path, String check) {
            File arena = new File(getDataFolder() + File.separator + path + File.separator + "ArenaSettings.yml");
            YamlConfiguration fcn = YamlConfiguration.loadConfiguration(arena);
            if (fcn.get(check) != null) {
                return (Location) fcn.get(check);
            }
            return null;
        }
     
    I bet that no one understood a fuck. How can I improve it? (I know I shouldn't use all of those File.separator and that there's a better way and I'll change it(one day(NEVERRRR))
     
  2. Honestly I'd say it's pretty good. I understood the code on my first read. If you really want it to be more readable, use comments. The code itself isn't complex, so all you maybe need to add is say what you're doing.

    One thing maybe, you're specifying File arena at two different places. You might wanna create a utility function getArenaFile() which just returns a new File object (or a String containing the path). That'll make the code slightly less complex and if you ever wanna change the path of the arena file, you'll only need to change it at one place.
     
    • Like Like x 1
  3. I get the gist on whats going on with a quick read over and the Method names are very descriptive. One thing I would so is put a /** comment above all the methods you build and type the explanations so you can generate a Javadoc and send it out if your concerned about Future Developer readablility.

    Another thing I would do is add some spacing like this to make it easier to follow the scopes

    Code (Java):
    private void recreateArenas() {

        File file = new File(getDataFolder(), "Arenas.yml");
        YamlConfiguration fc = YamlConfiguration.loadConfiguration(file);
     
        for (String name : fc.getStringList("Arena-list")) {
     
            File arenas = new File(getDataFolder() + File.separator + name + File.separator + "ArenaSettings.yml");
            YamlConfiguration fcn = YamlConfiguration.loadConfiguration(arenas);
            Arena newArena = new Arena(fcn.getString("Name"), fcn.getInt("minPlayers"), fcn.getInt("maxPlayers"), fcn.getInt("time"), checkLocationArena(name, "Lobby"), checkLocationArena(name, "Spectator"));
        }
    Code (Java):
    private Location checkLocationArena(String path, String check) {

        File arena = new File(getDataFolder() + File.separator + path + File.separator + "ArenaSettings.yml");
        YamlConfiguration fcn = YamlConfiguration.loadConfiguration(arena);
     
        if (fcn.get(check) != null) {
     
            return (Location) fcn.get(check);
        }
        return null;
    }
     
    • Like Like x 1
  4. I would also suggest that you put your curly brackets on their own lines. This makes it clearer when you have a big project. Also please remember to add comments, or in 6 months you'll come back to this project and it'll be a pain to remember what certain things do. And you don't need curly brackets in a single line if statement.
    Code (Java):
    private Location checkLocationArena(String path, String check)
    {
        File arena = new File(getDataFolder() + File.separator + path + File.separator + "ArenaSettings.yml");
        YamlConfiguration fcn = YamlConfiguration.loadConfiguration(arena);
         
        if (fcn.get(check) != null)
            return (Location) fcn.get(check);
         
        return null;
    }
     
  5. I actually never commented code as I think that in a small program, like a Minecraft plugin, if I don't understand the logic of something I wrote it means I coded it badly. Of course in a bigger software they will be needed. Maybe it's time to start
     
  6. Thanks I'm going to do it
     
  7. I used to do the same. But over time I have found that a simple plugin can very quickly get quite complicated. And you might think you understand the plugin perfectly now but after 6 months of working on other projects it probably won't be as clear. So commenting can save you time and effort in the future.

    But the main reason for comments (i think) is for other people's convenience. Lets say for example you gave me a task to fix a bug in your code. If you have clearly commented your code then I won't have to go through multiple classes to figure out how it all works.
     
    • Agree Agree x 1
  8. Don't listen to this guy's bracing style tips, he's bad influence :mad:
     
    • Agree Agree x 5
  9. How is that bad dude? It makes it clear which curly brackets belong to each other. Cause they are always indented the same amount.
     
  10. that's C standard, not Java
    nothing is readable with java anyways so i dont see where you're coming from with those braces
    it's not only prettier and easier to read, but it's also standard to always use braces on if statements
     
  11. I mainly work in C#. Its the correct canonical form there. Not sure why java doesn't use it as standard, cause its better in my opinion. At work, if I summit any code without separate lines for curly brackets it gets automatically rejected.

    Also in my opinion, I believe its just as clear to read an if statement with curly brackets as it is without them. In fact, i'd argue its better to refrain from using curly brackets for single line blocks, because its more compact.
     
  12. LOL it will actually help me because I want to learn C++. I know just Java, html and some CSS. I saw what you can do with C++ in games and I understood I need to learn it. In this way I will know both of the most important programming languages, in my opinion: C++ and Java. I think I'm going to use Unreal Engine 4
     
  13. C++ is great, but I would honestly suggest you try C# instead. If java and C had a baby, it would be C#. Its like the best of both worlds in my opinion. And though unreal engine is good, its much quicker to code with unity.
     
    • Like Like x 1
  14. Strahan

    Benefactor

    Ugh, it may be the standard but God I hate that lol. I do agree though about single line blocks. Though the way I do single lines would probably also give you fits, lol, as it's all one one line.
     
  15. You are right about that :) Luckily I'm tired and can't bother to make a case in the contrary.
     
  16. Code style is just a preference. Consistency is what you should be aiming for.
     
    • Agree Agree x 4
  17. No worries, it has just become a meme in the dev community to fight about bracing style, just like we fight about spaces vs. tabs. Each language has its own preferences; Java puts it on the same line, C# puts it on a new line, C does some weird mixture.

    Generally though, I'd go with the language default, so for Java it's braces on the same line.