如何将ReSharpers设置保存到文件?
我需要在我的同事之间共享设置,它还必须包括VS中的格式设置.
在"清洁代码:敏捷软件工艺手册"一书的第11章中,Bob叔叔说下面的Lazy-Initialization不是一个干净的代码.它需要两个职责,并且具有很强的依赖性.
public Service getService() {
if (service == null)
service = new MyServiceImpl(...); // Good enough default for most cases?
return service;
}
Run Code Online (Sandbox Code Playgroud)
除了IoC容器和工厂,有没有办法让代码干净并与依赖关系分开?
lazy-loading ioc-container inversion-of-control code-cleanup factory-pattern
我想和你咨询一段代码。我有:
if tuple_type == Operation.START_SERVER:
dictionary = ServersDictionary()
dictionary.start(some_param)
elif tuple_type == Operation.STOP_SERVER:
dictionary = ServersDictionary()
dictionary.stop(some_param)
(...)
elif tuple_type == Operation.START_APP:
dictionary = AppsDictionary()
dictionary.start(some_param)
elif ...
(....)
Run Code Online (Sandbox Code Playgroud)
我有 27 个 if / elifs。通常,我会进入 map - 函数调度程序,但是在每个 if / elif 之后,我都有两行具有相同字典引用的代码。你能给我建议一些干净的解决方案来代替那些丑陋的结构吗?
创建 27 个类来应用多态或 27 个函数听起来不太好……你怎么看?
我在这里看到了 Martin Fowler 的《重构》中给出的示例
不知道如何以 spring-boot 方式(IoC)实现它。
我正在开发 Spring Web 应用程序。我有一个 REST 控制器,它以给定的格式接受studentId和fileType导出学生fileType的数据。控制器调用ExportService exportFile()如下所示的方法
@Service
public class ExportServiceImpl implements ExportService {
public void exportFile(Integer studentId, String fileType) {
if (XML.equals(fileType)) { exportXML(studentId);}
else if()... // and so on for CSV, JSON etc
}
}
Run Code Online (Sandbox Code Playgroud)
为了以多态性为条件进行重构,
首先我创建了抽象类,
abstract class ExportFile {
abstract public void doExport(Integer studentId);
}
Run Code Online (Sandbox Code Playgroud)
然后我为每个文件类型导出创建服务。例如,以下 XML 导出是一项服务,
@Service
public class ExportXMLFileService extends ExportFile {
public void doExport(Integer studentId) …Run Code Online (Sandbox Code Playgroud) polymorphism refactoring spring code-cleanup conditional-statements
我写了一个函数,应该做一件简单的事情:
作为 RDMS,我在这里使用 mysql。我将所有内容都放在事务中,以避免调用此函数的并发 go 例程中出现竞争条件。
然而,大量的持续检查使err代码变得丑陋,并且很难获得完整的测试覆盖率。
在提高代码质量方面我可以改进什么吗?
func getAddressId(db *sql.DB, address string) (int64, error) {
tx, err := db.Begin()
if err != nil {
tx.Rollback()
return 0, err
}
stmt, err := tx.Prepare("SELECT id FROM address WHERE `address`=?")
if err != nil {
tx.Rollback()
return 0, err
}
defer stmt.Close()
var result sql.NullInt64
err = stmt.QueryRow(address).Scan(&result)
if err != nil && err != sql.ErrNoRows {
tx.Rollback()
return 0, err
}
if result.Valid …Run Code Online (Sandbox Code Playgroud) 我IF在我的存储过程中有这样的陈述:
if ((@someParam = 'C') OR (@someParam = 'I' AND @SomeOtherParam <> 2 AND @SomeOtherParam <> 4 AND @SomeOtherParam <> 5))
Run Code Online (Sandbox Code Playgroud)
我的问题是我可以一次检查@SomeOtherParam,而不是必须分别检查3次吗?
有没有办法避免parent::PHP类中的静态访问器,或者这是一个使用时间@SuppressWarnings(StaticAccess)?
同样,似乎这个StaticAccess警告在可疑的地方突然出现.例如,异常处理 - 当我throw new Exception(...),PHPMD抱怨静态访问时.但是......没有其他方法可以做到(我已经发现)所以我有更多的警告抑制器比我想要的更多.这是正常的吗?
编辑
根据要求,这是一个例子 - 它非常简单:
class aaa {
private $someReasonForAnException = true;
public function __construct() {
echo 'AAA<br>';
if ($this->someReasonForAnException) {
throw new Exception("Something happened that's worth noticing!");
}
}
}
class bbb extends aaa {
public function __construct() {
echo 'BBB<br>';
parent::__construct();
}
}
$BBB = new bbb();
Run Code Online (Sandbox Code Playgroud)
PHPMD将报告上述两个错误:StaticAccess错误Exception,以及调用StaticAccess错误parent::__construct().
为了避免这种情况,我必须注意两个类@SuppressWarnings,这看起来很笨,并且也不会显示"真正的"静态访问问题.
我有一个相当长的逻辑陈述,它需要分成多行.打破声明的最佳位置在哪里?是否存在拆分逻辑操作数和linq lambda表达式的约定?带有ReSharper的Visual Studio 2013以这种方式格式化语句:
var b =
wmsDocument.Document.Types.All(
typePair => validTypes.Any(type => type.DocumentTypeId == typePair.DocumentTypeId &&
(String.IsNullOrWhiteSpace(typePair.DocumentSubTypeId) ||
(type.SubTypes != null &&
type.SubTypes.Any(
subType =>
subType.DocumentSubTypeId == typePair.DocumentSubTypeId)))));
Run Code Online (Sandbox Code Playgroud)
我知道我可以通过一些方法调用来分解逻辑,但我真的想知道是否有人有关于如何缩进这些类型语句的约定.到目前为止,我还没有在网上找到任何运气.
我已经在Try上阅读了有关transform方法的信息,并将此方法与其他方法进行比较,以找出哪种方法更易于编码。我给你看下面的代码
val value: Try[Int] = Try("1".toInt)
.transform(f => Success(f), e => Success(0))
val value2: Try[Int] = Try("1".toInt) match {
case Success(f) => Success(f)
case Failure(e) => Success(0)
}
val value3: Try[Int] = Try("1".toInt)
.map(f => f)
.recover {
case e: Exception => 0
}
Run Code Online (Sandbox Code Playgroud)
我想知道在这些情况下哪个会更好,为什么呢?
谢谢
code-cleanup ×10
refactoring ×2
apk ×1
c# ×1
caching ×1
coding-style ×1
flutter ×1
go ×1
if-statement ×1
indentation ×1
lazy-loading ×1
linq ×1
oop ×1
performance ×1
php ×1
phpmd ×1
polymorphism ×1
python ×1
release ×1
resharper ×1
scala ×1
spring ×1
sql ×1
static ×1
syntax ×1
try-catch ×1