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.

1 comments

@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: oodesign.com

Related Questions

Sponsored Content

28 Answered Questions

55 Answered Questions

[SOLVED] How to create a memory leak in Java

65 Answered Questions

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

  • 2008-12-12 18:20:57
  • user42155
  • 3919703 View
  • 3376 Score
  • 65 Answer
  • Tags:   java random integer

86 Answered Questions

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

42 Answered Questions

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

58 Answered Questions

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

47 Answered Questions

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

41 Answered Questions

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

32 Answered Questions

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

29 Answered Questions

Sponsored Content