我目前正在开发两个有很多共同点但却截然不同的社交网站.我发现自己为这两个代码编写了很多相同的代码(包括UI),并且想知道是否存在限制重复代码的最佳实践.
其中一个主要问题是这些项目非常独立,并且可能很快就会出现类似的差异.此外,一旦完成初始工作,它们可能会被移交给其他程序员,因此共享代码库可能最终成为一个大问题.
那些可能不得不处理类似情况的人的任何建议?
PS:我是这两个项目中唯一的开发人员,看起来它会保持这种状态一段时间.
我有三种重复代码的方法.前两种方法几乎完全重复.第三个与火灾略有不同,应该绘制更多的信息.
我想删除这个重复的代码,并考虑使用内部类的模板方法模式.这是正确的方法还是有更好的解决方案?
private void drawWaterSupplies(Graphics g) {
double hScale = getWidth() / (double) groundMap.getWidth();
double vScale = getHeight() / (double) groundMap.getHeight();
int imageOffsetX = waterSupplyImage.getWidth() / 2;
int imageOffsetY = waterSupplyImage.getHeight() / 2;
for (Location l : groundMap.getWaterSupplyLocations()) {
int x = (int) (l.getX() * hScale);
int y = (int) (l.getY() * vScale);
g.drawImage(waterSupplyImage, x - imageOffsetX, y - imageOffsetY,
null);
}
}
private void drawEnergySupplies(Graphics g) {
double hScale = getWidth() / (double) groundMap.getWidth();
double vScale = getHeight() / …Run Code Online (Sandbox Code Playgroud) 我正在学习 Java,正在学习在线课程等,我正在进行其中一项编码练习,并意识到我的两种方法之间存在大量重复,如下所示:
private static void addCustomerTransaction() {
System.out.println("Enter the branch name:");
String branchName = scanner.nextLine();
System.out.println("Enter the customer name:");
String customerName = scanner.nextLine();
System.out.println("Enter the transaction");
while (!scanner.hasNextDouble()) {
scanner.next();
}
double transaction = scanner.nextDouble();
bank.addCustomerTransaction(branchName,customerName,transaction);
}
private static void addCustomer() {
System.out.println("Enter the branch name:");
String branchName = scanner.nextLine();
System.out.println("Enter the customer name:");
String customerName = scanner.nextLine();
System.out.println("Enter the transaction");
while (!scanner.hasNextDouble()) {
scanner.next();
}
double transaction = scanner.nextDouble();
bank.addCustomer(branchName,customerName,transaction);
}
Run Code Online (Sandbox Code Playgroud)
现在显然这两个函数之间的唯一区别是对银行类对象的方法调用 - 最终执行不同的操作。
我想知道如何重构这些方法以减少重复。我了解到:
private static void addCustomerTransaction() …Run Code Online (Sandbox Code Playgroud) 在学习 C++ 时,我决定编写一个简单的模板化二叉搜索树 (bst) 并遇到以下问题:我希望能够通过传递左值和右值来构造一个 bst 。同样,我希望能够插入左值和右值。所以我最终得到了很多我不喜欢的重复代码:const T &valT &&val
/// copy constructor
explicit inline constexpr binary_search_tree(const T &val)
: _root{std::make_unique<binary_search_tree_node>(val)} {}
/// move constructor
explicit inline constexpr binary_search_tree(T &&val)
: _root{std::make_unique<binary_search_tree_node>(std::move(val))} {}
Run Code Online (Sandbox Code Playgroud)
对于构造函数, wherebinary_search_tree_node是其私有成员binary_search_tree,然后还必须提供复制和移动构造函数:
struct binary_search_tree_node {
T value;
std::unique_ptr<binary_search_tree_node> left;
std::unique_ptr<binary_search_tree_node> right;
// prohibit creation of tree_node without value
inline constexpr binary_search_tree_node() = delete;
/// copy constructor
explicit inline constexpr binary_search_tree_node(const T &val)
: value{val}, left{nullptr}, right{nullptr} {} …Run Code Online (Sandbox Code Playgroud) 有没有办法在 Android Studio 中找到重复的(复制/粘贴代码、重复代码)Kotlin 代码?我有 Android Studio 4.0,但找不到这个“重复的代码片段”选项。
是否有任何 Android Studio 插件可以为 Kotlin 完成这项工作?
我正在寻找一些带有静态函数的库,以消除代码中比较"事物"的重复.
(evil.equals(s1) || evil.equals(s2) || evil.equals(s3))
(evil == enum1 || evil == enum2 || evil == enum3)
Run Code Online (Sandbox Code Playgroud) AppendLastSlashIfNotExist我有一个功能.
今天,我决定再做一个功能AppendLastBackSlashIfNotExist
wstring AppendLastSlashIfNotExist(__in const wstring& path)
{
if (path == L"/")
{
return path;
}
if (path.size() == 0 || path[path.size() - 1] != L'/')
{
return path + L"/";
}
return path;
}
wstring AppendLastBackSlashIfNotExist(__in const wstring& path)
{
if (path == L"\\")
{
return path;
}
if (path.size() == 0 || path[path.size() - 1] != L'\\')
{
return path + L"\\";
}
return path;
}
Run Code Online (Sandbox Code Playgroud)
是的,很糟糕.只有Slash - > BackSlash才是变化.我想删除重复.
wstring AppendLastSlashIfNotExist(__in const wstring& …Run Code Online (Sandbox Code Playgroud) 我有一堆函数,所有函数都以相同的方式开始和结束,只有中间部分不同.
我知道我能做到:
int
foo(int bar)
{
intro();
/* stuff that differs */
outro();
}
Run Code Online (Sandbox Code Playgroud)
但我想知道是否还有另一种方法,让我免于重新输入所有函数中的intro()和outro().
我有2个方法具有完全相同的逻辑:
Dog RunDog()
{
// a LOT of businees logic
return DogMethod(dogParams);
}
Employee RunEmployee()
{
// the exact same logic from above
return EmployeeMethod(employeeParams (can be easily converted to/from dogParams));
}
Run Code Online (Sandbox Code Playgroud)
是否有一个通用的设计模式来帮助我避免代码重复?
也许是这样的:
T RunT()
{
// Logic...
// Invoke DogMethod/EmployeeMethod depending on T and construct the params accodringly
}
Run Code Online (Sandbox Code Playgroud)
我选择Dog/Employee来强调在两者之间进行转换没有简单的方法.
我有一些重复的代码,但不确定简化它的最佳方法.
private void CheckData(long PKID, int ExpectedResult, string Server)
{
var a = _ARepo.GetAll();
var b = _BRepo.GetAll();
if(Server == "A")
{
a.Find(x => x.PKID == PKID).Result.ShouldBe(ExpectedResultId);
}
if (Server == "B")
{
b.Find(x => x.PKID == PKID).Result.ShouldBe(ExpectedResultId);
}
}
Run Code Online (Sandbox Code Playgroud)
这是一个单元测试项目,我正在使用该Shouldly库.任何想法都赞赏.
code-duplication ×10
c# ×3
java ×3
refactoring ×3
c++ ×2
android ×1
asp.net ×1
c ×1
c#-5.0 ×1
kotlin ×1
lvalue ×1
methods ×1
overloading ×1
plugins ×1
rvalue ×1