pinkred's mobile program

pinkred mobile programer

Archive for 3월 2018

코드로 대화하기 – Null 만날지 말지~!

leave a comment »

Null 만날지 말지~!

null은 항상 java에서 골치꺼리이다. Class에서 null을 보기 싫다면 의도적으로 null이 되지 못하게 해라.

class Cart
{
    private List<String> cartList;

    public Cart()
    {
        cartList = new ArrayList();
    }

    public void addItem(String item)
    {
        if (item == null || item.isEmpty() == true)
        {
            throw new EmptyItemException("item == null || item.isEmpty() == true");
        }

        cartList.add(item);
    }

    public void addAllItem(List<String> cartList)
    {
        if (cartList == null || cartList.size() == 0)
        {
            throw new EmptyItemException("item == null || item.isEmpty() == true");
        }
        this.cartList.addAll(cartList);
    }

    public List<String> getCartList()
    {
        return cartList;
    }
}

코드에서 cartList는 null이 될수 없다. 그래서 getCartList()를 호출하더라도 안심하고 null 체크 없이 사용이 가능하다. null을 만날지 말지 고민하지 말고 만나기 싫으면 의도적으로 못 만나게 해라. 틈이 보이면 만날수 밖에 없다.

Written by pinkredmobile

2018/03/16 at 8:18 pm

프로그래밍(programming)에 게시됨

Tagged with ,

코드로 대화하기 – 숨기고 싶으면 보여주지 말아라~!

leave a comment »

숨기고 싶으면 보여주지 말아라~!


class Cart
{
    private List cartList;
    public Cart()
    {
        cartList = new ArrayList();
    }

    public void addItem(String item)
    {
        if (item == null || item.isEmpty() == true)
        {
            throw new EmptyItemException("item == null || item.isEmpty() == true");
        }

        cartList.add(item);
    }

    public void addAllItem(List&lt;String&gt; cartList)

    {
        if (cartList == null || cartList.size() == 0)
        {
            throw new EmptyItemException("item == null || item.isEmpty() == true");
        }
        this.cartList.addAll(cartList);
    }
}

cartList의 타입을 외부에 노출 시키고 싶지 말아야 할때는 확실히 숨겨야한다.  안보여서 나쁠것은 없다. 보이면 자꾸 건드리고 싶어진다.

Written by pinkredmobile

2018/03/16 at 8:09 pm

프로그래밍(programming)에 게시됨

Tagged with ,

코드로 대화하기 – 목적을 분명하게 밝혀라~!

leave a comment »

목적을 분명하게 밝혀라~!

코드에는 목적을 분명히 노출 해줄수 있도록 해야한다.

class Cart
{
    public List cartList;

    public Cart()
    {
        cartList = new ArrayList();
    }
}

 


class Cart
{
    private List&amp;lt;String&amp;gt; cartList;

    public Cart()
    {
        cartList = new ArrayList();
    }

    public void addItem(String item)
    {
        if(item == null || item.isEmpty() == true)
        {
            throw new EmptyItemException("item == null || item.isEmpty() == true");
        }

        cartList.add(item);
    }

    public void addAllItem(List&amp;lt;String&amp;gt; cartList)
    {
        if(cartList == null || cartList.size() == 0)
        {
            throw new EmptyItemException("item == null || item.isEmpty() == true");
        }

        this.cartList.addAll(cartList);
    }
}

위의 코드를 보면 cartList의 값을 public로 하여 모든 것을 다 허용해 주겠어라는 식의 대화법을 나타냈다.

난 너를 위해 모든것을 다 줄수 있다는 대화법으로 잘 이용하면 좋지만 잘못 이용하면 난장판이 될수 도 있고 어떻게 관계를 이어가는데도 쉽지 않을수 있다.

아래 코드는 나와 대화하기 위해서는 특정 통로 로만 접근이 가능해. 그리고 지켜야할 약속들이 있어라고 말하고 있다.

어떻게 보면 밀당 같지만 너와 나와의 연결고리를 확실히 하고 있다.

목적이 무엇인지를 생각해보자.

Written by pinkredmobile

2018/03/16 at 9:19 am

프로그래밍(programming)에 게시됨

Tagged with ,