写干净的if-else语句

使用if-else链接有时看起来更复杂,这可以通过编写小代码块来避免。使用条件语句可以提高代码的可读性,甚至更多。一个最佳实践应该是先处理错误案例。下面的示例显示了如何处理错误情况并简化if-else逻辑。 例1: updateCache()-这是一种基于某些类级别变量的方法,用于更新主数据库。 updateBackupDb()-这是一种更新数据库的方法。 使用这种类型的if-else很难调试和扩展现有函数中的任何功能。 优化前

null

JAVA

// A simple method handling the data
// base operation related task
private void updateDb( boolean isForceUpdate) {
// isUpdateReady is class level
// variable
if (isUpdateReady) {
// isForceUpdate is argument variable
// and based on this inner blocks is
// executed
if (isForceUpdate) {
// isSynchCompleted is also class
// level variable, based on its
// true/false updateDbMain is called
// here updateBackupDb is called
// in both the cases
if (isSynchCompleted) {
updateDbMain( true );
updateBackupDb( true );
} else {
updateDbMain( false );
updateBackupDb( true );
}
} else {
// execute this if isUpdateReady is
// false i. e., this is dependent on
// if condition
updateCache(!isCacheEnabled);
// end of second isForceUpdate block
}
// end of first if block
}
// end of method
}


观察: 在下面的代码中,布尔变量已被识别并基于此 使用if和return语句将代码分成小块。 1.如果更新未就绪,则无需输入方法 退出这个方法。 2.同样,如果强制更新布尔值为false,则执行if语句中的任务 –更新缓存并从此方法返回。 3.在最后一步rest中,所有任务都完成了,即更新备份数据库和更新主数据库。 优化后

JAVA

// A simple method handling the
// data base operation related
// task
private void updateDb( boolean isForceUpdate) {
// If isUpdateReaday boolean is not
// true then return from this method,
// nothing was done in else block
if (!isUpdateReady)
return ;
// Now if isForceUpdate boolean is
// not true then only updating the
// cache otherwise this block was
// not called
if (!isForceUpdate) {
updateCache(!isCacheEnabled);
return ;
}
// After all above condition is not
// fulfilled below code is executed
// this backup method was called two
// times thus calling only single time
updateBackupDb( true );
// main db is updated based on sync
// completed method
updateDbMain(isSynchCompleted ? true : false );
}


注释- 在上述优化版本中,主要考虑的是基于条件语句简化if-else。 这种简单代码块的好处是——对于下一个开发人员来说,调试/理解/扩展这种方法非常容易。 例2: 通过现有的JavaAPI代码示例来解释这个想法 此代码段取自Java文档:- JDK子字符串代码JAVA API 来自上述API的现有代码——编写得非常完美。 优化后

JAVA

public String substring( int beginIndex, int endIndex) {
// My comment - Below are the example of
// correct use of if else, checking
// condition and returning from // methods,
// this is not about throwing error ie
// return or throw error or do something
// else - the idea is braking if // else
// chaining.
if (beginIndex < 0 ) {
throw new StringIndexOutOfBoundsException(beginIndex);
}
if (endIndex > value.length) {
throw new StringIndexOutOfBoundsException(endIndex);
}
int subLen = endIndex - beginIndex;
if (subLen < 0 ) {
throw new StringIndexOutOfBoundsException(subLen);
}
return ((beginIndex == 0 ) && (endIndex == value.length))
? this
: new String(value, beginIndex, subLen);
}


如果任何人在修改上述逻辑后使用If else,如下面的示例所示,这将非常复杂,但最终会产生相同的结果,因此我不希望按照下面所示的方式实现—— 优化前

JAVA

public String substring( int beginIndex, int endIndex) {
if (beginIndex < 0 ) {
throw new StringIndexOutOfBoundsException(beginIndex);
} else {
// Again why this else block is used,
// this need not to write, see above
// correct implementation
if (endIndex > value.length) {
throw new StringIndexOutOfBoundsException(endIndex);
} else {
// This else is also not required
int subLen = endIndex - beginIndex;
if (subLen < 0 ) {
throw new StringIndexOutOfBoundsException(subLen);
}
}
return ((beginIndex == 0 ) && (endIndex == value.length))
? this
: new String(value, beginIndex, subLen);
}
}


在看到以上两个示例后,可以观察到错误处理是在输入方法时完成的。这是一种先处理错误情况的好做法。 总的来说,if-else可以根据需要分块使用,这将消除代码复杂性,代码将易于使用/调试/维护/扩展。

© 版权声明
THE END
喜欢就支持一下吧
点赞14 分享