How to code

Discussion in 'Plugin Development' started by MaTaMoR_, Apr 16, 2015.

Thread Status:
Not open for further replies.
  1. I was wondering which is the best way to code, i mean how code extructure should be:

    Some examples:
    Code:
    public String getItemName(Player player) {
        ItemStack stack = player.getItemInHand();
    
        if(stack == null || stack.getType().equals(Material.AIR)) {
            return null;
        }
    
        if(!stack.hasItemMeta()) {
            return null;
        }
    
        ItemMeta im = stack.getItemMeta();
    
        if(!im.hasDisplayName()) {
            return null;
        }
    
        return im.getDisplayName();
    }
    
    public String getItemName(Player player) {
        if(player.getItemInHand() != null && !player.getItemInHand().getType().equals(Material.AIR)) {
            ItemStack stack = player.getItemInHand();
            if(stack.hasItemMeta()) {
                ItemMeta im = stack.getItemMeta();
                if(im.hasDisplayName()) {
                    return im.getDisplayName();
                }
            }
        }
        return null;
    }
    
     
  2. The best way would probably be something like this:
    Code:java
    1. public String getItemName(ItemStack item) {
    2. if (item == null || item.getType() == Material.AIR || !item.hasItemMeta()) return null;
    3. return item.getItemMeta().hasDisplayName() ? item.getItemMeta().getDisplayName() : null;
    4. }

    :p
     
  3. Offline

    mine-care

    @FisheyLP i would also sugest to make it "silent" so instead of null i would return an empty string, it makes no difference and it is "null safe"
     
  4. Offline

    1Rogue

    I employ the first way. The less subscope, the better.

    That's a terrible idea. Lack of values should be null, that's what null means.
     
    _Filip likes this.
  5. Offline

    _Filip

    agree 110%
     
  6. Offline

    nverdier

    @_Filip And what do you agree with -10%?
     
    Rocoty likes this.
  7. Offline

    _Filip

    thank
     
    nverdier likes this.
  8. Offline

    mine-care

    @1Rogue hm, i see your point yes it does mean that but i was aiming for a silent method meaning you dont need to null check each time you invoke it, but indeed it makes more sense to use null.
     
  9. Offline

    1Rogue

    "Silent method" isn't really even a term, and if it was it would refer to not throwing an exception, not erroneous output. Null (or an Optional<T>, which is what should be used post-Java7) should always be used in place of guessing what the user wants, or an unexpected type return.

    For example, say you have a method #getCustomName:

    Code:java
    1. public class Foo {
    2.  
    3. private final String name;
    4.  
    5. public Foo() { /*...*/ }
    6.  
    7. public String getCustomName() {
    8. return this.name;
    9. }
    10.  
    11. }


    Given this, this code works perfectly:

    Code:java
    1. public void doSomething(Foo foo) {
    2. if (foo.getCustomName() == null) {
    3. System.out.println("Foo does not have a name!");
    4. } else {
    5. System.out.println(String.format("Hello from %s!", foo.getCustomName()));
    6. }
    7. }


    Now if we were to change our original implementation of Foo:

    Code:java
    1. public String getCustomName() {
    2. return this.name == null ? "" : this.name;
    3. }


    We will now get the output below from the previous method:
    Code:
    Hello from !
    By convention, this is simply how it has worked in the past (across many languages). You shouldn't begin supplying default values in that manner to anything.
     
    nverdier likes this.
  10. Offline

    mine-care

    @1Rogue i didnt use it as a term, i know it is for exceptions (seen on Apatche commons mostly) but thanks for the tutorial :- )
     
  11. Thx @1Rogue -sensei
     
Thread Status:
Not open for further replies.

Share This Page