Upgrade to Pro — share decks privately, control downloads, hide ads and more …

202004 GDG Incheon 나쁜 프로그래밍 습관

202004 GDG Incheon 나쁜 프로그래밍 습관

나쁜 프로그래밍 습관 (영진출판) - 202004 GDG Incheon 북토크온라인 밋업

Veronikapj

April 18, 2020
Tweet

More Decks by Veronikapj

Other Decks in Programming

Transcript

  1. द੘ೞӝ જ਷೐۽Ӓې߁੉ۆ ▸ ߸ࣻ ੉ܴਸ ૙ח ؘ о੢ જ਷ ߑߨ਷

    ޖ঺ੑפө? ▸ ࢲ࠳ܖ౯ਸ ࢎਊೞח ؘ যו ੿ب੄ ࠂ੟بө૑ ೲਊ೧ঠ ೤פө? ▸ ௿ېझܳ ੿੄ೞח ؘ ୭؀ ݻ ઴ ө૑ ೲਊ೧ঠ ೤פө? ▸ ঴݃ա ݆਷ ௏٘о పझ౟ غযঠ ೤פө?
  2. WARMING UP చҗڸযॳӝ ▸ ׮਺ ௏٘ীࢲ a= -10 ੌٸ, number

    ੄ ч਷? public class Main { } 2. Number = -10 1. Number = 0 public int number; public void assignIfPositive(int a) { } if(a > 0) number = a; System.out.println("a is positive"); ௏٘ח ׮ܲ ࢎۈҗ ੄ࢎࣗా ೞח ୐ߣ૩ ࣻױ੉׮. 2. Number = -10
  3. ҳࢿҗ ҳઑ લ਷௏٘ ▸ ׮਺ ௏٘੄ Ѿҗ ч਷? public void

    doProcessRandomly() { double n = Math.random(); if (n > 10) { System.out.println("n is over 10"); } else { System.out.println("n is " + n); } } 2. N is over 10 1. N is 0.15.. 3. Random ل ઺ ೞա 1. N is 0.15.. ▸ Math.random() ೣࣻח 0җ 1ࢎ੉੄ чਸ ߈ജೠ׮. // if ղ੄ ௏٘ח ੺؀ प೯ ؼ ࣻ হח લ਷ ௏٘׮. double n = GdgUtils.getRandomValue(); ▸ ழझథ ೣࣻо ٜযр׮ݶ? ࠛ೙ਃೠ ௏٘ח ૑ਕ઱੗
  4. ҳࢿҗ ҳઑ ▸ ׮਺ ௏٘ী ࣻ੿੘স੉ ٜয৳णפ׮. Price < 0

    ੌ ٸ, “੤ੑҊ ৘੿” ਵ۽ ߄Լ઱ࣁਃ. /** * ݅ড price о 0 ࠁ׮ ੘ਵݶ ಿ੺ਸ ୹۱ೠ׮. * @param price оѺ */ public void displayPrice(int price) { String message; if(price < 0) { message = "ಿ੺"; } else { message = price + "ਗ"; } System.out.println(message); } “੤ੑҊ ৘੿";
  5. ߸ࣻ ബਯ੸ੋ߸ࣻࢶ঱ ▸ Bobҗ John੄ Score ח? int scoreBob, 1.

    scoreBob = 10, scoreJohn = 10 2. scoreBob = 0, scoreJohn = 10 2. scoreBob = 0, scoreJohn = 10 https://www.oracle.com/technetwork/java/javase/documentation/codeconventions-141270.html#2992 ೠ ઴ীח ೞա੄ ߸ࣻ݅ ࢶ঱ೞ੗. scoreJohn = 10; int
  6. ߸ࣻ ఋੑ୓҅ܳ৮੹൤ঈਊೞӝ int status_code = connect_to_device(); switch (status_code) { case

    0: display_info(info_message[1]); break; case 1: reattempt(); break; case 2: display_warning(warning_messages[3]); break; } ▸ status_code == 1 ࢚క ч਷? ী۞௏٘ࠁ׮ח ৘৻о ੌ߈੸ਵ۽ ӂ੢ػ׮. // ???
  7. ߸ࣻ DeviceStatus status = connect_to_device(); switch (status) { case SUCCESS:

    display_info(info_message.CONNECTION_SUCCEEDED); break; case ERROR_NO_PINGBACK: reattempt(); break; case WARNING_CONNECTION_SLOW: display_warning(warning_messages.CONNECTION_SLOW); break; } ࢚క ௏٘ܳ ࢎਊೡ ٸח пп੄ ݫद૑о ޖ঺ਸ ׸Ҋ ੓ח૑ ݺഛ൤ ॄળ׮. public enum DeviceStatus { SUCCESS = 0, WARNING_CONNECTION_SLOW, ERROR_NO_PINGBACK }
  8. ઑѤޙ ؀উ਷হ׮ ▸ ੼ࣻо 60੼ ੉࢚੉ۄݶ “Pass”ܳ ઱ࣁਃ. public void

    calculateGrade(int score) { if (score > 60) { grade = "Pass"; } ▸ ੼ࣻо 40੼ ੉ۄݶ? } else { grade = "Fail"; } } ▸ ੹ޙ੸ਵ۽ ੘ࢿػ ࣗ೐౟ਝয੄ if ޙ ઺ 50~80%ী else ੺੉ হ׮ח Ѫ੉ ߋഃ઎׮. (Elshoff, 1976) ৘࢚ оמೠ ч ৻ী ೦࢚ ݅ੌ੄ ࢎకܳ Ҋ۰ೞ੗.
  9. ߈ࠂޙ ఎ୹਷ղ݋؀۽ while(true) { //ୡ௒݁੉ۄݶ ղо ਗೞ؍ Ѫ੉׮! if (currentSnack.getType().equals("Chocolate"))

    { chosenSnack = currentSnack; break; } // Ӓۧ૑ ঋ׮ݶ Ӗܖబ੉ হח ࠺झఉਸ ࢶఖೞѷ׮! else if (currentSnack.getType().equals("Biscuit")) { boolean containGluten = hasGluten(currentSnack); if(!containGluten) { chosenSnack = currentSnack; break; } } // হਵݶ ׮਺ਵ۽ ֈযр׮. if (snackIterator.hasNext()) { currentSnack = snackIterator.next(); } else { break; } } for(int i = 0; chosenSnack == null && i < snacks.size(); i++) { chrrentSnack = snacks.get(i); switch(currentSnack.getType()) { case "Chocolate" : chosenSnack = currentSnack; break; case "Biscuit" : if(hasGluten(currentSnack)) { chosenSnack = currentSnack; } break; } } ▸ ৈ۞ ѐ੄ ߈ࠂ ઙܐ੼਷ ҃۽ܳ ࠂ੟ೞѱ ٜ݅Ҋ ௏٘ ੉೧ܳ য۵ѱ ݅ٚ׮. ೞա੄ ߈ࠂޙীח ೞա੄ ઙܐ੼݅ ك׮. ▸ break, continue, return
  10. ࢲ࠳ܖ౯ ߈ജчਸо૑Ҋ֥ইࠁ੗ ▸ ৖੄ ௏٘੄ Ѿҗ ч਷? void move(int x,

    int xDistance, int y, int yDistance) { x += xDistance; y += yDistance; } void recordMovement(int x, List<Integer> xs, int y, List<Integer> ys) { xs.add(x); ys.add(y); } void displayMoves() { int xPos = 5; int yPos = 5; List<Integer> xMoves = new ArrayList<>(); List<Integer> yMoves = new ArrayList<>(); System.out.println("X :" + xPos + ", Y :" + yPos); move(xPos, 10, yPos, -20); recordMovement(10, xMoves, -20, yMoves); System.out.println("X-Movements : " + xMoves); System.out.println("Y-Movements : " + yMoves); System.out.println("X :" + xPos + ", Y :" + yPos); } X : Y : X-Movements: Y-Movements: X : Y : 5 5 5 5 10 -20 ࢲ࠳ܖ౯੉ ߸ࣻܳ ࣻ੿೧ঠ ೠ׮ݶ ࢜۽਍ чਸ ٜ݅য ߈ജೞ੗.
  11. ௿ېझ৬ ё୓ ਬোೞ૑ঋ਷ҙ҅ ▸ Ҋন੉ܳ ୶оೞח ௏٘ܳ ੘ࢿ೧ ઱ࣁਃ. public

    static void main(String[] args) { PetFeeder feeder = new PetFeeder(); Dog lassie = new Dog(); feeder.giveFood(lassie); class Cat { public void feed() { System.out.println("cat feed"); } } class PetFeeder { public void giveFood(Dog d) { d.feed(); } class Dog { public void feed() { System.out.println("dog feed"); } } ઱য૓ ௿ېझ } Cat felix = new Cat(); feeder.giveFood(felix); giveFood(felix); } public void giveFood(Cat c) { c.feed(); }
  12. ௿ېझ৬ ё୓ ਬোೞ૑ঋ਷ҙ҅ ▸ Ҋন੉ܳ ୶оೞח ௏٘ܳ ੘ࢿ೧ ઱ࣁਃ. public

    static void main(String[] args) { PetFeeder feeder = new PetFeeder(); Dog lassie = new Dog(); feeder.giveFood(lassie); class Cat { public void feed() { System.out.println("cat feed"); } } class PetFeeder { public void giveFood(Dog d) { d.feed(); } public void giveFood(Cat c) { c.feed(); } } class Dog { public void feed() { System.out.println("dog feed"); } } ઱য૓ ௿ېझ } Cat felix = new Cat(); feeder.giveFood(felix); giveFood(felix); interface Pet { void feed(); } class PetFeeder { public void giveFood(Pet p) { p.feed(); } } implements Pet { implements Pet { ё୓૑ೱ ೐۽Ӓې߁੄ ࢸ҅ח ੋఠಕ੉झ ೐۽Ӓې߁ਸ ࢶഐೠ׮.
  13. ݃ޖܻ ੿ܻೞӝ ▸ ௏٘ח ׮ܲ ࢎۈҗ ੄ࢎࣗా ೞח ୐ߣ૩ ࣻױ੉׮.

    ▸ ࠛ೙ਃೠ ௏٘ח ૑ਕ઱੗ ▸ જ਷ ઱ࢳ਷ ௏٘ܳ ݺഛೞѱ ೞҊ Ӓ ٍী ऀ਷ ੄޷ܳ ࢸݺೠ׮ ▸ ೠ ઴ীח ೞա੄ ߸ࣻ݅ ࢶ঱ೞ੗. ▸ ী۞௏٘ࠁ׮ח ৘৻о ੌ߈੸ਵ۽ ӂ੢ػ ׮. ▸ ࢚క ௏٘ܳ ࢎਊೡ ٸח пп੄ ݫद૑о ޖ঺ ਸ ׸Ҋ ੓ח૑ ݺഛ൤ ॄળ׮. ▸ ৘࢚ оמೠ ч ৻ী ೦࢚ ݅ੌ੄ ࢎకܳ Ҋ۰ ೞ੗. ▸ ೞա੄ ߈ࠂޙীח ೞա੄ ઙܐ੼݅ ك׮. ▸ ࢲ࠳ܖ౯੉ ߸ࣻܳ ࣻ੿೧ঠ ೠ׮ݶ ࢜۽਍ ч ਸ ٜ݅য ߈ജೞ੗. ▸ ё୓૑ೱ ೐۽Ӓې߁੄ ࢸ҅ח ੋఠಕ੉झ ೐ ۽Ӓې߁ਸ ࢶഐೠ׮.