Remember the popular Programming Styles I mentioned before? A few people have asked me exactly what those programming styles look like. Here’s an example of code refactoring to see what that popular programming style looks like in practice. The following practice is not a myth. If it is similar, please mark it.

First, we have an expression like this:

1
s =
7
;

Obviously, the variable name of this expression is too meaningless to make the program readable, so we need a meaningful variable name:

1
slots =
7
;

That’s fine, but the constant 7 is hard-code or a Magic number, and it doesn’t have a name. To change:

1
2
3
SEVEN =
7
;
.
slots = SEVEN;

Depend! Up there, it’s what kind of amendment is this? (Although, I guarantee this is true), the constant name should also make sense.

1
2
3
SLOTS_PER_WIDGET =
7
;
.
slots = SLOTS_PER_WIDGET;

This is similar, but the name may be the same ah, best to put in a class:

1
2
3
import
widgetConstants;
.
slots = widgetConstants.SLOTS_PER_WIDGET;

It looks much better now, but since it is object-oriented, we should learn to use the Design Pattern, such as Factory or Singleton:

1
2
3
widgetModelFactory = WidgetModelFactory.getInstance();
widgetModel = widgetModelFactory.getWidgetModel() ;
slots = widgetModel.getSlotsPerWidget();

Call waiting welfare

1. Recently sorted out 20G resources, including product/operation/test/programmer/market, etc., and Internet practitioners [necessary skills for work, professional books on the industry, precious books on interview questions, etc.]. Access:

  • Scan the code of wechat to follow the public account “Atypical Internet”, forward the article to the moments of friends, and send the screenshots to the background of the public account to obtain dry goods resources links;

2. Internet Communication Group:

  • Pay attention to the public account “atypical Internet”, in the background of the public account reply “into the group”, network sharing, communication;

Of course, that’s not enough when you consider the overall class structure, so here’s our final refactoring :(welcome to the real Java world)

1
2
3
4
5
6
7
8
9
context = Context.getCurrentContext();
serviceDirectoryFactory = ServiceDirectoryFactory.getServiceDirectory(context);
serviceDirectory = serviceDirectoryFactory.getServiceDirectory(context);
serviceDescriptor = ServiceDescriptorFactory.getDescriptor(
"widgetModelFactory"
);
widgetModelFactoryServiceLocator = serviceDirectory.getServiceLocator(serviceDescriptor,context);
widgetModelFactory = (WidgetModelFactory)widgetModelFactoryServiceLocator.findService(context);
widgetModel = widgetModelFactory.getWidgetModel(context);
slots = widgetModel.getSlotsPerWidget();

This is our face like object programming model, remember during the interview the famous N years ago to encourage agile methodologies, in when you use the program to realize a problem, they are, about my program for two reasons, first, I didn’t write UT Case using TDD, secondly, no design patterns in my program. Programming, I learned, was originally about testing and designing patterns, not about requirements. Today, this literature is only for programmers who love popular coding styles.

In fact, this code is just as follows.

1
slots = thisWidget.getSlotCount();

(Full text)

Author: increasingly, blog: https://coolshell.cn/articles/18190.html