Clean Code

Clean Code

Code that is more readable must be consider cleaner. If it is readable, it will also be extendable.

If you want a scientific method to measure clean code, this will be it:

Consider a hypothetical scenario where two developers are tasked with building an eCommerce website, by replicating all the functionalities contained in its prototype (the prototype will be given to them). They are also given a dataset that contains logs of how users are expected to use the eCommerce. The dataset must contain all the record-able metrics from the user's interaction. So information like the volume of user-visits within any duration, the exact navigation pathway of each user, along with the exact mouse pointer location and keyboard inputs within the viewport and other related information must be extract-able from the dataset. Based on this data, they are also given SLA guarantee requirements and constraints they must fulfill (they can always test to see if their website performs as required by emulating the user's behavior which will just be a straightforward playback of all the recorded event-logs corresponding to each user, present in the dataset).

Further assume there to be two "test-groups", each contain 50 randomly chosen developers. The two codebases obtained in the above scenario must be given to each of those groups respectively. Let us say, group A gets the first codebase and group B gets the second codebase.

Both the groups must be given a set of features to implement on their respective codebase copies. They must be given the same set of features. And the developers in those groups must work individually in a separate copy of the codebase. They must not work as a team.

Measure the time taken by each individual from both the groups to finish implementing the features on their respective codebase copies. Compute the average time taken by the members of each group to complete the features. If group A's average is significantly lesser than group B's then, the first codebase should be considered "cleaner" than the second codebase. On the other hand, if group B's average is significantly lesser than group A's, the second codebase should be considered cleaner!

This "scientific method" to measure clean code might be too expensive and an unnecessary overkill. But we should be able to rely on anecdotal evidences derived from our experience to know which codebase is readable and which isn't. A codebase will usually be unreadable if it is too complicated and contains too many abstractions to hide its logic.

A real clean codebase's business logic must be directly visible to readers, without them having to use an IDE's navigation tools to jump around as they try to decipher the logic. This will usually be the case when they have too many unnecessary abstractions.

要查看或添加评论,请登录

Sreram K的更多文章

社区洞察

其他会员也浏览了