Inline Method Refactoring  

 

�� ������ ��ſ��� Inline Method  ï¿½ï¿½ï¿½ï¿½ï¿½ä¸µ �󿡼� ��������  overview��  how ï¿½ï¿½ �����丵�� IntelliJ IDEA���� ���� �� �ִ°��� �����մϴ�.

����


�Ϲ������� �����ڸ�, Inline Method   �����丵�� Extract Method ï¿½ï¿½ �ݴ��Դϴ�.  Extract Method  ï¿½ï¿½ï¿½ï¿½ï¿½ä¸µ �����ϸ�

IDEA�� �� ȣ������ ���� �޼ҵ��� ���� ���� ���̰� �޼ҵ�� �����˴ϴ�.


�ݸ鿡, �� �����丵�� �����ϸ� ���� ����� ������ �ϳ��� �޼ҵ� ���� �����ϰ�, Inline�ϰ�, �ٸ� �ݰ� �޼ҵ� ���� �����մϴ�.


���� ���, �츮�� ���� �ڵ带 �����ϴ�:

 
    public class AClass {
        public int methodA(int param) {
            return methodB() + param;
        }
        private int methodB() {
            some code here
        }
    }
 
    ...
 
    public class AnotherClass {
        void foo (AClass aClass){
            int res = aClass.methodA(1);
        }
    }

 

�׸��� ����� methodA�� ��� �ߺ��� ���� �˾ҽ��ϴ�.  ï¿½Ì°ï¿½ï¿½ï¿½ �����丵�� ����ǰ� �� �Ŀ� �ڵ��Դϴ�:
 
    public class AClass {
        private int methodB() {
            some code here
        }
    }
 
    ...
 
    public class AnotherClass {
        void foo (AClass aClass){
            int res = aClass.methodB() + 1;
        }
    }



Inlining Method  


�޼ҵ带 inline �ϱ� ���ؼ�

 1.   ï¿½ï¿½ï¿½Ï´ï¿½ �޼ҵ� �̸������� �� ���� �Ǵ� �κ����̼ǿ� �� ij���� ��ġ�ϰ�, �� �޴����� �Ǵ� �����Ϳ��� ������ ��ư�� Ŭ���Ͽ� �ҷ����� �˾� �޴����� Refactor Inline...  ï¿½ï¿½ Ŭ���ϰų� OR  Ctrl + Alt + N ï¿½ï¿½ �����ϴ�. �׸��� ����� ���� ���̾�α� �����츦 �� ���Դϴ�:


���� �����丵�� �޼ҵ� �������κ��� ȣ��Ǹ� This invocation only and keep the method �ɼ� ��ư�� �Ұ����ϰ� �ϰ� �� �� �Դϴ�.



 2.   ï¿½ï¿½ï¿½Ì¾ï¿½Î±×´ï¿½ �ɼ� �׷�� üũ �ڽ��� �����ϴ�:

Inline  

�޼ҵ尡 ��� Inline�Ǵ��� �����մϴ�:

 All invocations and remove the method  ��� �޼ҵ� �κ����̼��� �޼ҵ� ����� ��ü�� ���Դϴ�. �׸��� �޼ҵ� �� ��ü�� ���ŵ� ���Դϴ�.
 
This invocation only and keep the method  - ï¿½Þ¼Òµï¿½ �� ��ü�� ������ ���� ij���� �ִ� �κ����̼��� �޼ҵ� ����� ��ü�� ���Դϴ�.

Preview invocations  to be inlined  

�� üũ �ڽ��� �����ϸ� ����� � ����� �߰ߵǾ����� �����ϰ�, �׵��� ��ü�� �����ϵ��� ����մϴ�.  ï¿½ï¿½ üũ �ڽ��� ���õ��� ���� ��, IDEA�� ��ü ��ɸ� �ڵ������� �����մϴ�.


������ üũ �ڽ��� ���õ��� �ʾ��� ����, ���� �߰ߵ� ����� � �б� ���� ���Ͽ��� �����ϸ� ��ü ����� Ȯ���ϴ� ������Ʈ�� ��ſ� ���� �� �Դϴ�.


Refactoring Preview ï¿½ï¿½ï¿½Ì¾ï¿½Î±×¿ï¿½ï¿½ï¿½ (���� �װ��� ��Ÿ����) ����� ����� ������ ��� �� �ֽ��ϴ�.  ï¿½ï¿½ï¿½ï¿½ï¿½ä¸µï¿½ï¿½ �Բ� �����ϱ� ���ؼ�, Do Refactor�� Ŭ���մϴ��׷��� �ʴٸ� Cancel �� Ŭ���մϴ�. ���� ���� ������ Refactoring Preview�� ���ʽÿ�.


���� This invocation only and keep the method   �ɼ� ��ư�� ���õǸ� Preview invocations to be inlined üũ �ڽ��� �Ұ����ϰ� ���ϴ�.



 3.   OK�� Ŭ���Ͻʽÿ�. �׸��� ���ϴ� ������ inline �� ���Դϴ�.


Inline Method  ï¿½ï¿½ï¿½ï¿½ï¿½ä¸µï¿½ï¿½ �����ϴµ� �Ϻ� ������ �ֽ��ϴ�.  IDEA�� ���� �帧�� ���� ������Ʈ��Ʈ�� ���� �ߴܵ� ����� �޼ҵ� �Ǵ� �޼ҵ带 inline�� �� �����ϴ�.

���� ���:  

 
     private int methodA(){
         int[] aaa = new int [10];
         for (int i = 0; i < aaa.length; i++) { i = aaa[i]; if (aaa[i] == 0) return i;
         }
         return -1;
     }
     

���⿡���� methodA()  ï¿½Þ¼Òµï¿½ï¿½ inline�� �� �����ϴ�.  ï¿½×·ï¿½ ��쿡 ��ſ��� ���� �޽�����  ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Æ®ï¿½ï¿½ ���Դϴ�.  ï¿½ï¿½ ���� ����� �������� �ʿ��� �޼ҵ带 inline�ؾ߸� �ϰų�, �ڵ带 �����ϴ� ���� �����ؾ߸� �ϰų�, �̹��� Inline Method   �����丵�� �ϴ� ���� �ܳ��ؾ߸� �մϴ�.