Your SlideShare is downloading. ×
Unlearning Java
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Saving this for later?

Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime - even offline.

Text the download link to your phone

Standard text messaging rates apply

Unlearning Java

417
views

Published on

The presentation demonstrates the need for understanding the context before applying the best practices.

The presentation demonstrates the need for understanding the context before applying the best practices.


0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
417
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
12
Comments
0
Likes
2
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Unlearning Java„best practices‟ of one time, to avoid; to remain„Professional‟ Naresha K, @naresha_k
  • 2. DisclaimerThe opinions expressed in this presentation are solely of the speaker and not necessarily be of the employer of the speaker.
  • 3. Why Unlearn?
  • 4. Best Practices - Myth Best Practices vs Time
  • 5. Best Practices - Myth Best Practices vs Time
  • 6. Best Practices - Truth Best Practices vs Time
  • 7. The costly mistake !Domain Time
  • 8. #1: Follow the Evolution
  • 9. #1 - illustrationpublic String concateString(String first, String second, String third){ return first + second + third;}
  • 10. #1 - illustrationpublic String concatenateString(String first, String second, String third){ StringBuffer buffer = new StringBuffer(); buffer.append(first); buffer.append(second); buffer.append(third); return buffer.toString();}
  • 11. #1 - illustrationpublic String concatenateStringOptimally(String first, String second, String third){ StringBuilder buffer = new StringBuilder(); buffer.append(first); buffer.append(second); buffer.append(third); return buffer.toString();}
  • 12. #1 - illustrationpublic String concatenateStringImproved(String... words){ StringBuilder buffer = new StringBuilder(); for(String word : words){ buffer.append(word); } return buffer.toString();}
  • 13. #1 - illustration@Testpublic void concateTwoWords() { StringUtil util = new StringUtil(); final String result = util.concatenateStringImproved("first", "second"); final String expectedResult = "firstsecond"; assertThat("concatenated string", result, is(equalTo(expectedResult)));}
  • 14. #1 - illustration@Testpublic void concateFourWords() { StringUtil util = new StringUtil(); final String result = util.concatenateStringImproved("first", "second", "third", "fourth"); final String expectedResult = "firstsecondthirdfourth"; assertThat("concatenated string", result, is(equalTo(expectedResult)));}
  • 15. #1 - illustration@Testpublic void concateInArrayStyle() { StringUtil util = new StringUtil(); final String[] words= {"first", "second", "third", "fourth"}; final String result = util.concatenateStringImproved(words); final String expectedResult = "firstsecondthirdfourth"; assertThat("concatenated string", result, is(equalTo(expectedResult)));}
  • 16. #1 More Examples Check if string is emptyisEmptypublic boolean isEmpty() Returns true if, and only if, length() is 0.Returns: true if length() is 0, otherwise false Since: 1.6
  • 17. #1 More Examples @Override
  • 18. #2: Don’t ignore readability
  • 19. #2 - illustration Prefer Generics
  • 20. #2 - Illustrationpublic Food order(int item){ Food food = null; switch(item){ case 1: food = new MasalaDosa(); break; case 2: food = new GheeDosa(); break; case 3: food = new DahiVada(); break; default: throw new ItemNotAvailableException(); } return food;}
  • 21. #2 - Illustrationpublic enum Snack { MASALA_DOSA, GHEE_DOSA, DAHI_VADA;}
  • 22. #2 - Illustrationpublic Food orderSnack(Snack item){ Food food = null; switch(item){ case MASALA_DOSA: food = new MasalaDosa(); break; case GHEE_DOSA: food = new GheeDosa(); break; case DAHI_VADA: food = new DahiVada(); break; } return food;}
  • 23. #2: Generics ◦ Method signature shows the types ◦ Less documentation Enum ◦ Allowed values are clearly shown
  • 24. #3: Don’t ignore the internals
  • 25. #3: Understand the internalsList<Integer> numbers = new ArrayList<>();numbers.add(new Integer(5));//List<Number> ref = numbers;//List<?> ref = numbers;//List<? extends Number> ref = numbers;List<Integer> ref = numbers;System.out.println(ref.get(0));ref.add(new Integer(10));
  • 26. #3: Understand the internalsList<Number> ref = numbers;Type mismatch: cannot convert from List<Integer> to List<Number>Case 2 & 3The method add(capture#2-of ? extends Number) in the type List<capture#2-of ? extends Number> is not applicable for the arguments (Integer)
  • 27. #4: Avoid lengthy code
  • 28. #4 - illustration
  • 29. #4 - illustration
  • 30. #4 – Truth revealed
  • 31. #4 – illustration – the truth
  • 32. #5: Reduce ambiguity
  • 33. #5: Be preciseReduce MapsUse Strong Types
  • 34. #5: Use strong typesMap<String, Object> customer = new HashMap<>();customer.put("name", "James");customer.put("age", 62);int age = (Integer) customer.get("name");
  • 35. #5: Use strong typesclass Customer{ private String name; private int age; //Getter & Setters}int age = aCustomer.getName();Type mismatch: cannot convert from String to int
  • 36. #6: Dont over optimize
  • 37. #6: Dont over optimize
  • 38. #6: Dont over optimize
  • 39. #6: Dont over optimize
  • 40. #7: Don’t ignore your Env
  • 41. #7: Don’t ignore your Env JDK Version – Compiler level Server Version Clustered?
  • 42. #8: Don’t carry dead bodies
  • 43. #8: Don’t carry dead bodiesHungarian Notation
  • 44. #8: Don’t carry dead bodies
  • 45. Review1. Follow the evolution2. Make them readable3. Understand the internals4. Be concise5. Be precise6. Dont over-optimize7. Dont ignore your environment8. Dont carry dead bodies
  • 46. Take away1. You need to go beyond “it works” to arrive at best practices.2. There is no „universally best‟ practice. They are always context sensitive.3. Follow the evolution of technology.
  • 47. Unlearning Java„best practices‟ of one time, to avoid; to remain„Professional‟ Naresha K, @naresha_k