Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

7 rules of simple and maintainable code

931 views

Published on

Rule 1: Follow a consistent coding standard
Rule 2: Name things properly, long variable and function names are allowed
Rule 3: Be expressive, write code as you speak and be optimally verbose
Rule 4: Max indent per method should be 2, in case of exceptions 3
Rule 5: Avoid creating god object and long methods
Rule 6: Keep the method in one place, inject the class and call it, DRY
Rule 7: Avoid in-line comments (comment with code), put comments in the method doc

Published in: Technology
  • Be the first to comment

  • Be the first to like this

7 rules of simple and maintainable code

  1. 1. 7 GOLDEN RULES OF SCRUMT CODE Simple, Clean, Readable, Understanable, Maintainable (Testable) Code Slides by , presented by Sumit Chhetri. Geshan Manandhar
  2. 2. WHY WRITE CLEAN CODE
  3. 3. Code for humans not machines Think that the next person who reads your code is a chainsaw maniac If you don't write clean code, you know your fate.
  4. 4. Rule 1: Follow a consistent coding standard If you are doing PHP follow Other languages like Java, Python, Javascript also have coding standards Use a linter or CS fixer to follow the standard. Integrate build tools in Sublime or use default PHPStrom code refactor, to automate this Makes it easy for everyone - including yourself - in the team, to read the code PSR-2
  5. 5. Rule 2: Name things properly, long variable and function/method names are allowed Stick to one convention. ($someVariable or $SomeVariable or $some_variable) Don't use abbreviations, may not be understandable to the person, who reads your code Naming is difficult, do it well. Name classes, variables and methods that make sense Do not be redundant. Example
  6. 6. Rule 3: Be expressive, write code as you speak and be optimally verbose Focus on API rather than patterns. First, write down the API for perfect scenario, observe how it feels, then jump to coding and make it work. Tell, don't ask. Example
  7. 7. Rule 4: Max indent per method should be 2, in case of exceptions 3 Avoid the use of else. Extract the logic to other readable method. Return early Throw exception Example
  8. 8. 6 levels of indentation (in our public code) source public function settingsAction() { //0 if ($_POST) { //1 try { //2 ... } else { ... //3 if ($reportingOrgNew != $reportingOrgOld) { //4 if ($save == "ok") { ... //5 if ($activityPublish) { //6
  9. 9. Rule 5: Avoid creating   and long methods god object One class should do one thing not everything like StatusChangeror StatusManagernot StatusGod Avoid using andin method names like validateAndSave, one method needs to do one thing and one thing well Keep methods small, a 50 line method is a problem Keep classes small, a 1000 line class is a pain Keep the instance variables to as low as 6 or 7.
  10. 10. Rule 6: Keep the method in one place, inject the class and call it, DRY In MVC keep controllers slim, business logic belongs to services or repositories, check for controller 5-10-20 If you access property of a property a->b->c->d() there is something wrong, you can use a wrapper or proxy function. Use language constructs like interfaces, traits to make code more expressive and reusable If you find yourself copying the same code several times, extract that code into its own method. Refactor your code, every once in a while. symfony best practice Example
  11. 11. Rule 7: Avoid in-line comments (comment with code), put comments in the method doc Comment is a code smell (anti pattern), like If you need to write comments to explain your code, means you need to put it in a new method. here Example public function addActivity($data , $default) { $this->defaults = $default; $identity = Zend_Auth::getInstance()->getIdentity(); //var_dump($data);exit; $model = $this->model; $modelActivity = new Model_Activity(); $activitiesId = $model->getIdByField('iati_activities', 'account_id', $ident //Create activity and its defaults $iatiIdentifier['activity_identifier'] = $data['identifier'];
  12. 12. Other considerations Avoid working with just arrays for large data sets, use class and type hint Rather than starting to write something on your own spend 5 minutes to read the framework/library documentation to know if its already provided Extract out methods and rename variables as necessary Read about cyclomatic complexity and N-Path complexity, here
  13. 13. Recap 1 Rule 1: Follow a consistent coding standard Rule 2: Name things properly, long variable and function names are allowed Rule 3: Be expressive, write code as you speak and be optimally verbose Rule 4: Max indent per method should be 2, in case of exceptions 3
  14. 14. Recap 2 Rule 5: Avoid creating and long methods Rule 6: Keep the method in one place, inject the class and call it, Rule 7: Avoid in-line comments (comment with code), put comments in the method doc Dont forget the consideration and reading god object DRY PS: you can print the recap slides and mug them up.
  15. 15. References Most code examples are taken from YIPL open source code on Github. Rules for simpler code - laracast Maintainable Code Readable Code Object Calisthenics Your code sucks - let fix it
  16. 16. Thanks Hope we can step by step minimize code complexity and write SCRUMT code. Remember, simple, understandable, readable and eventually maintainable code is greater than any pattern ever invented (be practical).

×