By beinghuman

2013-09-16 15:35:09 8 Comments

I have been reading DRY principle.Though it seems so easy but I am having difficulty in understanding how we actually achieve it in any Project be it Web Application or Swing? May be few examples will give me lead and help me in developing my thought process. I have tried to search on Google but could not find any thing suitable.


@gview 2013-09-16 15:42:39

DRY is nothing that requires examples. If you are copying and pasting blocks of code from one method, function or block of code to another, then you are in violation of DRY.

Very simply, DRY tells you to move that code into a reusable unit: either into a function or method or macro, or other mechanism relevant to your programming language. If you have places in your codebase where you clearly have very similar code (even with minor variations) then you should be looking to refactor that code to make it DRY.

"Don't repeat yourself!" == "Don't write the same code repeatedly"

@beinghuman 2013-09-16 15:56:21

by reusable unit ,could it be a new class itself?If the repeated code is to be removed but there is no similarity between the objects.What should I do in such case?

@nachokk 2013-09-16 15:58:35

@Niks for example? A lot of answer could be done if you are not precisely, you could use generics perhaps.

@gview 2013-09-16 16:04:36

Yes, the reusable unit could be an entire class, or an abstract class, or any other component of reusability. If you're using a Dependency injection framework then certainly you will have entire classes. Where oop is concerned, sophistication in your design starts to veer into the area of common design patterns, described in the Gang of 4 book as well as many other language specific books. This site is also a great free resource:

Related Questions

Sponsored Content

87 Answered Questions

[SOLVED] Is Java "pass-by-reference" or "pass-by-value"?

43 Answered Questions

[SOLVED] How do I efficiently iterate over each entry in a Java Map?

33 Answered Questions

[SOLVED] When to use LinkedList over ArrayList in Java?

58 Answered Questions

[SOLVED] How to create a memory leak in Java?

28 Answered Questions

66 Answered Questions

[SOLVED] How do I generate random integers within a specific range in Java?

  • 2008-12-12 18:20:57
  • user42155
  • 4092766 View
  • 3530 Score
  • 66 Answer
  • Tags:   java random integer

49 Answered Questions

[SOLVED] Does a finally block always get executed in Java?

44 Answered Questions

[SOLVED] How do I convert a String to an int in Java?

59 Answered Questions

[SOLVED] How do I read / convert an InputStream into a String in Java?

31 Answered Questions

Sponsored Content