{"id":6946,"date":"2022-09-16T14:31:52","date_gmt":"2022-09-16T06:31:52","guid":{"rendered":"https:\/\/slash.bravefactor.com\/?post_type=resources&#038;p=6946"},"modified":"2024-01-31T16:42:54","modified_gmt":"2024-01-31T08:42:54","slug":"5-things-to-look-for-during-code-review","status":"publish","type":"resources","link":"https:\/\/slash.co\/articles\/5-things-to-look-for-during-code-review","title":{"rendered":"5 Things to look for during code review"},"content":{"rendered":"<p><span style=\"font-weight: 400;\">Coding is a job that requires hours and hours of work with careful attention allotted to accuracy. However, even the most accurate coder is bound to make the rare mistake now and then, and this is what makes code review important for your development team. For one you can remove a bit of the stress caused by coding when a developer knows his\/her code will be checked, but also it\u2019s your chance to head off any potential issues before they even become issues. Let\u2019s take a look at five key items to look for during code review.<\/span><\/p>\n<h2>Convention<\/h2>\n<p><span style=\"font-weight: 400;\">Convention, mutual agreement, it\u2019s a must to gain within your development team. Everyone needs to be on the same page when coding so it\u2019s imperative that the team follow the agreed upon coding rules and practices. For example, something as simple as using single vs double quotation marks, declaring short meaningful variable names, maintaining shorter lines of code per file and so on.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Consider this case: If we agree as a team to use camel case for a variable name, e.g. \u2013 waitedTime = 3000, but there is included code which is using snake case, e.g. \u2013 waited_time = 3000, now this may not break anything within the code, but it fails to follow the convention which the team has agreed to follow. Code is written by many developers and it\u2019s necessary for it to merge together seamlessly as though it was written by one person. Following the team coding practices aids in development of really clean code that works as it\u2019s designed to, so ensuring everyone&#8217;s code follows the agreed upon practices &amp; rules is key.<\/span><\/p>\n<h2>Logic<\/h2>\n<p><span style=\"font-weight: 400;\">Logic within the code is innately necessary as we gather you\u2019re aware. So you may be thinking, \u201cIf it\u2019s a given, why bother checking during code review?\u201d Well, no one is perfect and as we\u2019re all prone to mistakes, reviewing is a good way to catch those mistakes before they get released and become larger problems. Therefore, it\u2019s good to review how people\u00a0 have written the code for specific logic\/feature flow and complexity.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">By doing this we can give suggestions to team members on best practices, and comment\/feedback on how the logic should be rewritten according to the acceptance criteria of the review ticket or feature. For example: Consider a situation where there is a requirement to do a login API that verifies the password and generates &amp; responds to the JSON Web token (JWT). In this case, we could review the full logic code as in what the team put in the JWT to encode. If perhaps they accidentally put the password inside the JWT, we can request them to revise that as naturally it will cause security vulnerabilities. So yes, logic is a given, but mistakes are bound to happen so it\u2019s key to examine the logic during code review.\u00a0<\/span><\/p>\n<h2>Complexity<\/h2>\n<p><span style=\"font-weight: 400;\">The third thing which requires your attention during code review is complexity. In addition for the logic to make sense in the acceptance criteria, you must look out for code complexity in terms of the lines of code and conditional uses in the code. Those conditionals being: if \u2013 else, for loop, sub function or callback. Moreover, you must verify whether or not the code is testable.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Let\u2019s consider the following example\u2026 A developer writes the login code in a controller under the MVC pattern, \u201clogin_controller.js\u201d, using logic that contains many responsibilities. Those duties could be any number of things including:<\/span><\/p>\n<ul>\n<li><span style=\"font-weight: 400;\">API routing logic<\/span><\/li>\n<li><span style=\"font-weight: 400;\">Middleware logic<\/span><\/li>\n<li><span style=\"font-weight: 400;\">a definition of the user database model<\/span><\/li>\n<li><span style=\"font-weight: 400;\">a login code login comparing password<\/span><\/li>\n<li><span style=\"font-weight: 400;\">JWT logic to generate a token<\/span><\/li>\n<li><span style=\"font-weight: 400;\">response code logic with result and status (to the frontend)<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">Now as a result of this, we can see the code is too complex and we must make a corrective action. We could simplify it by splitting the responsibilities of those pieces of code into smaller files, import them to use in the \u201clogic_controller.js\u201d instead of having long lines of code. Those smaller files could be:<\/span><\/p>\n<ul>\n<li><span style=\"font-weight: 400;\">login_token.js: generates token function based on user data parameters<\/span><\/li>\n<li><span style=\"font-weight: 400;\">route.js: contain the routing logic of all APIs and include login route API in this file<\/span><\/li>\n<li><span style=\"font-weight: 400;\">guest_middleware.js: contain the middleware check if the user has not logged in for login route<\/span><\/li>\n<li><span style=\"font-weight: 400;\">user_model.js: contain the user database model code<\/span><\/li>\n<li><span style=\"font-weight: 400;\">response.js: contain the response code logic<\/span><\/li>\n<\/ul>\n<h2>Duplication<\/h2>\n<p><span style=\"font-weight: 400;\">Fourthly you want to check for duplication. Reducing duplication wherever is possible should be an integral part of any code review. Again to better understand, let\u2019s consider &amp; add to the example from above:<\/span><\/p>\n<ul>\n<li><span style=\"font-weight: 400;\">response.js: can be reused in another part of the application, other controllers (<\/span><i><span style=\"font-weight: 400;\">in the MVC pattern on the Backend side<\/span><\/i><span style=\"font-weight: 400;\">) can reuse the response function by passing some parameters to it such as\u2026<\/span><\/li>\n<\/ul>\n<ol>\n<li style=\"list-style-type: none;\">\n<ol>\n<li><span style=\"font-weight: 400;\">o<\/span><span style=\"font-weight: 400;\"> \u00a0 <\/span><span style=\"font-weight: 400;\">status code: 200, 300 or 400<\/span><\/li>\n<li><span style=\"font-weight: 400;\">o<\/span><span style=\"font-weight: 400;\"> \u00a0 <\/span><span style=\"font-weight: 400;\">status: true\/false, body as the message response<\/span><\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<ul>\n<li><span style=\"font-weight: 400;\">guest_middleware.js: can also be reused for other API endpoints to check if the user has yet to login; e.g. \u2013 in the request reset password API<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">Now the target you\u2019re aiming for here is to not have identical pieces of code and if by chance you do, then it\u2019ll be necessary to write them in a way which enables you to reuse the same piece every time it\u2019s needed. If this is not done, you will end up dealing with some unexpected\/regression error.<\/span><\/p>\n<h2>Testability<\/h2>\n<p><span style=\"font-weight: 400;\">Finally to cap things off, it\u2019s time to verify for testability. All of the points above will aid greatly in making the code testable. However, we could also review additionally the way we write the unit testing to cover the code functionalities that have been written so far. Consider whether it\u2019s best to test the functions or piece of code which we have extracted into smaller functions or files as per the steps above.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The reason for this is inherently obvious: we want to make sure the code is supposed to do what has been written. Therefore, any changes that come after it, must comply with the test cases, such as to add more cases, update the outcome of the test based on changes etc.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Let\u2019s finish by wrapping up our example previously used. We\u2019ve reviewed if we have the test cases covered for \u201clogin_controller.js\u201d and if we have covered different cases of the \u201clogin_controller.js\u201d and expect a different outcome that results in passing the different parameters to the \u201cresponse.js\u201d to respond accordingly. For example, if we simulate the test case of login failure, we review if we have passed the parameters correctly according to the login failure to the \u201cresponse.js\u201d or not. All in a day\u2019s code review work!<\/span><\/p>\n","protected":false},"featured_media":10561,"parent":0,"template":"","resource-topic":[63],"resource-type":[43],"class_list":["post-6946","resources","type-resources","status-publish","has-post-thumbnail","hentry","resource-topic-software-development","resource-type-articles"],"_links":{"self":[{"href":"https:\/\/slash.co\/wp-json\/wp\/v2\/resources\/6946","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/slash.co\/wp-json\/wp\/v2\/resources"}],"about":[{"href":"https:\/\/slash.co\/wp-json\/wp\/v2\/types\/resources"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/slash.co\/wp-json\/wp\/v2\/media\/10561"}],"wp:attachment":[{"href":"https:\/\/slash.co\/wp-json\/wp\/v2\/media?parent=6946"}],"wp:term":[{"taxonomy":"resource-topic","embeddable":true,"href":"https:\/\/slash.co\/wp-json\/wp\/v2\/resource-topic?post=6946"},{"taxonomy":"resource-type","embeddable":true,"href":"https:\/\/slash.co\/wp-json\/wp\/v2\/resource-type?post=6946"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}