代码审查案例1

我曾在一家缺乏代码审查实践的公司工作。 为了自我完善和开阔眼界,我想收到一些建设性的批评。

现在,我建议用大量分支来分析重复发生的情况。

工作任务


用户打算用鼠标将文件从一个文件夹窗口拖到另一个文件夹窗口。 您需要编写一个调度程序方法,以检查事件的本质以及处理事件的可能性,如有必要,请澄清其详细信息,然后调用所需的方法或显示有关限制的消息。

如果用户从一个文件夹拖放到一个文件夹,而另一个文件夹位于其他部分,请检查可复制性。 如果可以复制,则复制。 否则,给出一条您无法复制的消息。 由于以下原因而无法复制:没有写权限;没有权限。 可用空间不足; 文件系统名称中不支持字符; 目标文件夹中的文件名路径太长; 文件夹中已经有一个具有相同名称的文件(如果用户同意,则打开一个对话框以覆盖该文件,然后覆盖)。

如果目标文件夹与文件位于同一分区,请移动文件。 无法移动:没有​​写权限; 完整的目标路径将太长,文件夹中已经有一个具有相同名称的文件(打开对话框); 该文件是系统文件,无法删除; 已经有一个具有该名称的文件(如果用户同意,则打开一个对话框以覆盖该文件,然后覆盖)。

如果用户将文件移动到另一个窗口,但路径相同,则创建文件的副本(在名称中添加“ copy#”,其中#是使文件唯一的最小正数)。 无法创建副本:没有写权限; 完整路径太长; 可用空间不足。

如果用户使用右键转移,则调用对话框以选择操作(复制/移动/创建快捷方式/创建副本)。

如果用户使用向左按钮在同一窗口中释放了文件(文件已损坏),则什么也不做。 如果正确,则建议创建一个副本或快捷方式。 如果文件不在文件夹窗口中,则什么也不做。

随着时间的流逝,可能会出现新的条件,新的动作,已经描述的更改动作的条件。

讨论解决方案


我用Java提供了有争议的解决方案,如果满足以下条件,该解决方案在嵌套方面达到了第二高的水平:

public static void dispatchFileDropping( FileDragNDropEvent event ) { //------------------------------------------------------ //   // (     ). boolean A = isTargetPlaceIsDirectoryWindow(event); boolean B = isTargetDirEqualsSourceDir(event); boolean C = isTargetVolumeEqualsSourceVolume(event); boolean D = isMouseRightButtonUsed(event); boolean E = isSystemFileDroped(event); boolean F = isTargetVolumeHasFreeSpace(event); boolean G = isTargetDirWritable(event); boolean H = isSourceDirCleanable(event); boolean I = isFileNameOkForTarget(event); boolean J = isNewFileFullPathOkForTargetLimit(event); boolean K = isTargetDirHasSameNamedFile(event); boolean L = isTargetDirSameNamedFileIsWritable(event); Actions userChoise = (A & D) ? askUserForAction(event) : null; if (userChoise == Actions.CANCEL) return; boolean M = (userChoise == Actions.COPY); boolean N = (userChoise == Actions.CLONE); boolean O = (userChoise == Actions.MOVE); boolean P = (userChoise == Actions.LINK); //------------------------------------------------------ //      . boolean copyCase = (M & !K) | (A & !B & !C & !D & !K); boolean copyRewriteCase = (M & K) | (A & !B & !C & !D & K); boolean cloneCase = N | (A & B & !D); boolean moveCase = (O & !K) | (A & !B & C & !D & !K); boolean moveRewriteCase = (O & K) | (A & !B & C & !D & K); boolean createLinkCase = P; //------------------------------------------------------ //      //  . if (copyRewriteCase | moveRewriteCase) { if (askUserWantToRewrite() == Answers.NO) return; } //------------------------------------------------------ //     . boolean isPossibleToCopy = F & G & I & J; boolean isPossibleToCopyRewrite = isPossibleToCopy & L; boolean isPossibleToClone = isPossibleToCopy; boolean isPossibleToMove = isPossibleToCopy & !E & H; boolean isPossibleToMoveRewrite = isPossibleToMove & L; boolean isPossibleToLink = isPossibleToCopy & !K; //------------------------------------------------------ //   ,  , //    . String errorMessage = ""; if (copyCase & !isPossibleToCopy) { errorMessage = "  ."; } else if (copyRewriteCase & !isPossibleToCopyRewrite) { errorMessage = "  ."; } else if (cloneCase & !isPossibleToClone) { errorMessage = "  ."; } else if (moveCase & !isPossibleToMove) { errorMessage = "  ."; } else if (moveRewriteCase & !isPossibleToMoveRewrite) { errorMessage = "    ."; } else if (createLinkCase & !isPossibleToLink) { errorMessage = "  ."; } String reasons = " : \n"; if (!F) { reasons += "--     \n"; } if (!G) { reasons += "--        \n"; } if (!I) { reasons += "--      \n"; } if (!J) { reasons += "--       \n"; } if (moveCase | moveRewriteCase) { if (E) { reasons += "--     \n"; } if (!H) { reasons += "--       \n"; } } else if (copyRewriteCase | moveRewriteCase) { if (!L) { reasons += "--      \n"; } } else if (createLinkCase) { if (K) { reasons += "--       \n"; } } if (errorMessage.isEmpty()) { if (copyCase) copy(event); if (copyRewriteCase) copyRewrite(event); if (cloneCase) clone(event); if (moveCase) move(event); if (moveRewriteCase) moveRewrite(event); if (createLinkCase) createLink(event); } else { showMessage(errorMessage + reasons); } } 

Source: https://habr.com/ru/post/zh-CN417903/


All Articles