How to write good, reusable comments (DRY) ?
(Clean Code Quality Seal : Red)
Table of contents
How can i write good, reusable comments without repeating myself (DRY)?
How can i write reusable comments ?
Why do i think that annotations should be used as „post it“ and not as integral code elements?
Ok, but how can i make annotations powerfull and comments reusable?
How could it look like?
How does it look like in a real example?
Source code convention tools
Literature, good books and references
How can i subscribe/feed this blog ?
How can i rate this blog ?
Where do i find more clean code knowledge and gadgets?
How can i write good, reusable comments without repeating myself (DRY)?
Hi there! In this blog i wanna show you, how you can speed up your development process by taking profit of the power of annotaations. Doing this way, you'll be developing cleaner, faster and applying the DRY (Don't Repeat Yourseft) principle automatically. Share this blog with your developers doing Know-How-Exchange. Make your code readable and comments reusable right now... In the next section i'll show you, how you can create reusable, uniform comments by writing less comments, saving a lot of valuable development time.
How can i write reusable comments ?
Annotations are very powerfull additions in Java. But on my point of view, they should be used, as the name implies. To annotate and not to check and not to write code over that.
Why do i think that annotations should be used as „post it“ and not as integral code elements?
Well annotations are „static“ and hard coded. Once you annotate a type with the intention of write code based on this annotation, (what you can do and in some cases it really make sense) your code will be fixed on this annotation and changes on it are hard to be done without rebuilding the system, because it is in your code. The more annotations you have, the more static you’ll be making your code. I think we could check pre-conditions in a more elegant and flexible way, if you are using annotations just to check some conditions.
Ok, but how can i make annotations powerfull and comments reusable?
Well using annotations to mark your types bring a lot of synergie. Fact is : we would like a programm that does not need to be commented. Fact is : We would like a comment that is there, but does not appears in my source code, making my class huge and confusing me more then helping me. Fact is : Why do i have to write comments over and over again, if i could mark it? (annotate it) In fact we can do that. With a smart annotation you’ll get all reusable comments, you’ll gain overview in your classes, you’ll share your expirience motivating junior developers to use annotation and standard things can be annotated in the same, correctly and most important UNIFORM way, making corporate identity.
How could it look like?
Well i appreciate to use annotations to mark all the things it can be REUSED. Creating an annotation catalog all programmers will be using the same vocabulary and improving synergie and development speed with each iteration. In the annotation you can put(if nescessary) a good, explanatory comment with examples how to use the type and so on. I always write why and how i’m doing something or using something, NEVER what i'm doing. (this sentence is very important – note why and how, never what) Annotations make your source code readable and document it at the same time with concise keywords. Let’s see some examples :
The classic > database annotations: @Entity, @Bean, @Model and so on...
My favourits – Pattern annotations : @ObserverPattern, @StrategyPattern,@StatePattern
My favourits – Pattern annotations : @ObserverPattern, @StrategyPattern,@StatePattern
How does it look like in a real example?
@Debug
public interface Debuggable {
@ReferenceablePattern
Debuggable DO = new Debugger();
@Debug void debug(final Class<?> clazz, final String method);
@ReferenceablePattern class Debugger implements Debuggable {
@ReferenceablePattern
private Debugger() {
super();
}
@Override
public void debug(Class<?> clazz, String method) {
final String message = "class/method path: " + clazz.getName() + "/" + method;
final Component noParent = null;
JOptionPane.showMessageDialog(noParent, message);
}
}
}
public interface Debuggable {
@ReferenceablePattern
Debuggable DO = new Debugger();
@Debug void debug(final Class<?> clazz, final String method);
@ReferenceablePattern class Debugger implements Debuggable {
@ReferenceablePattern
private Debugger() {
super();
}
@Override
public void debug(Class<?> clazz, String method) {
final String message = "class/method path: " + clazz.getName() + "/" + method;
final Component noParent = null;
JOptionPane.showMessageDialog(noParent, message);
}
}
}
As you can see, writing good, reusable comments makes your class thin, readable and the more annotations you have, the less you'll be writing comments again. i recommend you to catalogaze your annotations to make it easy to refind and reuse it.