Skip to content

Latest commit

ย 

History

History
58 lines (36 loc) ยท 3.02 KB

Why_is_sort()_a_static_method.md

File metadata and controls

58 lines (36 loc) ยท 3.02 KB

์™œ Java์˜ ์ •๋ ฌ ํ•จ์ˆ˜ sort()๋Š” static ๋ฉ”์†Œ๋“œ์ผ๊นŒ?

Introduction

์ตœ๊ทผ์— Python์œผ๋กœ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด๋ฅผ ํ•˜๋‹ค๊ฐ€, Java๋กœ ์ฝ”๋”ฉ ํ…Œ์ŠคํŠธ๋ฅผ ์ค€๋น„ํ•  ์ผ์ด ์žˆ์—ˆ๋‹ค.

์ค€๋น„๋ฅผ ์œ„ํ•ด Java์—์„œ ์ฃผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด ๋ฌธ๋ฒ•์„ ์ •๋ฆฌํ•˜๋‹ค๊ฐ€ ์˜๋ฌธ์ ์ด ์ƒ๊ฒผ๋‹ค.

์™œ ์ž๋ฐ”๋Š” ์ •๋ ฌ์„ array.sort()๊ฐ€ ์•„๋‹Œ Array.sort(array)๋กœ ์ง€์›ํ•˜๋Š” ๊ฑธ๊นŒ?

์ด์— ๋Œ€ํ•ด ์กฐ์‚ฌํ•ด๋ณด์•˜๋‹ค.

๋‚ด์žฅ ํ•จ์ˆ˜ vs ์Šคํƒœํ‹ฑ ํ•จ์ˆ˜

๋ณดํ†ต Python ๋“ฑ์˜ ์–ธ์–ด์—์„œ๋Š” ์ •๋ ฌ ๋“ฑ์˜ ๋™์ž‘์ด ๊ฐ์ฒด ๋‚ด๋ถ€ ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ์ด๋ฃจ์–ด์ง„๋‹ค.

list = [3, 1, 4, 5, 2]
list.sort()
print(list) # [1, 2, 3, 4, 5]

๊ทธ๋Ÿฌ๋‚˜ Java์—์„œ๋Š” ๋ฐฐ์—ด/๋ฆฌ์ŠคํŠธ๋ฅผ ๋‹ค๋ฃจ๋Š” ํด๋ž˜์Šค์—์„œ static ํ•จ์ˆ˜๋กœ ์ •๋ ฌ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•œ๋‹ค.

import java.util.Arrays;

public class Example
{
    public static void main(String[] args)
    {
        int[] numArray = {3, 1, 4, 5, 2};
        Arrays.sort(numArray);
        System.out.println(Arrays.toString(numArray)); // [1, 2, 3, 4, 5]
    }
}

์‹ฌ์ง€์–ด ๊ฐ์ฒด๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•˜๋Š” toString์กฐ์ฐจ Arrays ํด๋ž˜์Šค์—์„œ ์ œ๊ณตํ•˜๋Š” static ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜๊ณ  ์žˆ๋‹ค.

๊ทธ๋ ‡๋‹ค๋ฉด ์™œ ๊ฐ ๊ฐ์ฒด์—์„œ ์ง์ ‘ ๋‚ด์žฅ๋œ sort๋ฅผ ์ •์˜ํ•˜๊ณ  ํ˜ธ์ถœํ•˜๋Š” ๋Œ€์‹  ์œ„์™€ ๊ฐ™์€ ๋ฐฉ๋ฒ•์„ ํƒํ•œ ๊ฒƒ์ผ๊นŒ?

๊ตฌ๊ธ€๋ง

ํ•ด๋‹น ์งˆ๋ฌธ๊ณผ ๊ฐ™์ด ๋‚˜์™€ ๊ฐ™์€ ์˜๋ฌธ์„ ์ œ๊ธฐํ•œ ์‚ฌ๋žŒ์ด ์žˆ์—ˆ๊ณ , ๊ทธ์— ๋‹ฌ๋ฆฐ ๋‹ต๋ณ€์€ ๋‹ค์Œ๊ณผ ๊ฐ™์•˜๋‹ค.

One possible reason for the Java designers is that sort() isn't always necessary for arrays so they didn't want to force developers to always make sure that it is accurately implemented.

์ฆ‰, ๋ชจ๋“  array์— ์ •๋ ฌ ๊ธฐ๋Šฅ์ด ํ•„์š”ํ•˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, ์ •๋ ฌ์„ ๊ตฌํ˜„ํ•˜๋„๋ก ๊ฐ•์ œํ•˜์ง€ ์•Š๊ณ ์ž ํ•จ์ด ํ•˜๋‚˜์˜ ์ด์œ ์ผ ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

๋ฐฐ์—ด์„ ์ •๋ ฌํ•  ์ฑ…์ž„์„ Arrys ํด๋ž˜์Šค๋กœ ๋„˜๊น€์œผ๋กœ์จ, ๋ฐฐ์—ด๊ณผ ๊ด€๋ จ๋œ ์ƒˆ๋กœ์šด ํด๋ž˜์Šค๋ฅผ ์„ ์–ธํ•˜๊ณ  ๊ธฐ์กด์˜ ํด๋ž˜์Šค๋ฅผ ์ƒ์†ํ•  ๋•Œ์—๋„ sort๋ฅผ ๋ฐ˜๋“œ์‹œ ๊ตฌํ˜„ํ•  ํ•„์š”๊ฐ€ ์—†๊ฒŒ ๋˜์—ˆ๋‹ค. ๋งŒ์•ฝ sort๊ฐ€ ๋‚ด์žฅ ํ•จ์ˆ˜์ด๊ณ  ์ด๊ฒƒ์ด ์ƒ์†/๊ตฌํ˜„์„ ํ†ตํ•ด ๊ฐ•์ œ๋œ๋‹ค๋ฉด, ์ •๋ ฌ์ด ํ•„์š”ํ•˜์ง€ ์•Š์€ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ์„ ์–ธํ•  ๋•Œ์—๋„ ๋ฐ˜๋“œ์‹œ sort๋ฅผ ๊ตฌํ˜„ํ•  ์˜๋ฌด๋ฅผ ๊ฐ€์ง€๊ฒŒ ๋œ๋‹ค.

์ด๋Š” Java์—์„œ ์ค‘์š”์‹œ ์—ฌ๊ธฐ๋Š” SOLID ์›์น™์—์„œ โ€˜S(SRP, ๋‹จ์ผ ์ฑ…์ž„ ์›์น™)โ€™์™€๋„ ๊ด€๋ จ์ด ์žˆ๋Š” ๊ฒƒ ๊ฐ™๋‹ค. ๋ฐฐ์—ด ๊ฐ์ฒด์—๋Š” ๋‹จ์ˆœํžˆ ์ž๋ฃŒ๋ฅผ ์ €์žฅํ•  ์ฑ…์ž„๋งŒ์„ ๋ถ€์—ฌํ•˜๊ณ , ์ด๋Ÿฌํ•œ ์ž๋ฃŒ๋ฅผ ์ œ์–ดํ•  ์ฑ…์ž„์„ ๋ณ„๋„๋กœ ๋ถ„๋ฆฌํ•จ์œผ๋กœ์จ ๋‹จ์ˆœํžˆ ์ž๋ฃŒ๋ฅผ ์ €์žฅ๋งŒ ํ•˜๋ฉด ๋˜๋Š” ์ƒˆ๋กœ์šด ๊ฐ์ฒด๊ฐ€ ์ถ”๊ฐ€๋  ๋•Œ์— ๋ถˆํ•„์š”ํ•˜๊ฒŒ ์ž๋ฃŒ๋ฅผ ์ œ์–ดํ•  ์ฑ…์ž„์ด ํ•จ๊ป˜ ๋ถ€์—ฌ๋˜์ง€ ์•Š๋„๋ก ํ•˜์˜€๋‹ค.

์•ž์œผ๋กœ ์ƒˆ๋กœ์šด ๊ฐ์ฒด๋ฅผ ๊ตฌํ˜„ํ•  ๋•Œ์—๋„ ์ด๋Ÿฌํ•œ ์š”์†Œ๋ฅผ ์ž˜ ๊ณ ๋ คํ•ด์„œ ๊ธฐ์กด์˜ ์„ค๊ณ„ ์˜๋„๋ฅผ ํ•ด์น˜์ง€ ์•Š๋„๋ก ํ•ด์•ผ ํ•  ๊ฒƒ ๊ฐ™๋‹ค.