کـــدنـویــســیــ بـا  طـراوتـــــــ Clean   Code تـوحـیـد ارسـطـو
برنامه‌ای که خوب نوشته شود، بهشت خودش است برنامه‌ای که بد نوشته شود جهنم خودش است
History of two writer
What is clean code?
یک برنامه باید سبک و سریع‌الانتقال باشد، روالهایش همانند رشتهای از مروارید به یکدیگر متصل باشند. روح و نیت برنامه‬ ‫باید ه...
ی‫ک برنامه باید از «قانون کمترین سرگشتگی» پیروی کند. این قانون چیست؟ به این معنی است که برنامه باید همواره به‌نوعی به کارب...
‫اگر برنامهای این التزامات را برآورده نکند، در حالتی از بی‌نظمی و آشفتگی قرار خواهد گرفت. تنها راه تصحیح آن بازنویسی‬ ‫برن...
اصول و اسلوب‌های کدنویسی باطراوت
In the early days of programming we composed our systems of routines and subroutines. in Fortran it was programs, subprogr...
Clean code Functions <ul><li>The first rule: </li></ul>They should be small. <ul><li>The second rule: </li></ul>They shoul...
Bad!!! function student($op, $name, family, $age, $sex, $id) { if($op == 'add') { $query = “check student exist in data ba...
Clean :) function add_student($name, family, $age, $sex) { $query = “add student to data base”; excute_query($query); $id ...
Functions should not be 100 lines long. Functions should hardly ever be 20 lines long. Smaller Than a Screenful
Functions should Do one thing.
Problems when function do more than one things Bugs and Complexity
Use descriptive names.
Example I changed the name of one example function from subscription_mail To send_subscription_email This is a far better ...
Half the battle to achieving that principle is: choosing good names for small functions, that do one thing.
The smaller and more focused a function is, the easier it is to choose a descriptive name. Conversely, if you can’t choose...
Long names Don’t be afraid to make a name long. A long descriptive name is better than a short enigmatic name. a long desc...
No more than three arguments.
ideal number of arguments for a function is zero ( niladic ). Next comes one ( monadic ), Followed closely by two ( dyadic...
love_message() is easier to understand than love_message($extra).
Common Monadic Forms There are two common reasons to pass a single argument into a function. You may be asking a question ...
Asking vs. Telling
Functions should either do something, or answer something, but not both.
Example if (set(&quot;username&quot;, &quot;unclebob&quot;))... What does that mean? Is it asking whether the “username” a...
Solution The real solution is to separate the command from the query so that the ambiguity cannot occurr. if (attribute_ex...
Prefer exceptions to returning error codes.
Example Function do_that() { If (time() < 123543214) { return “error”; } $doing = do_this; If ($doing == 'this_work') { … ...
منابع: Clean code book
Any question?
Upcoming SlideShare
Loading in …5
×

Clean code

545 views

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
545
On SlideShare
0
From Embeds
0
Number of Embeds
36
Actions
Shares
0
Downloads
7
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Clean code

  1. 1. کـــدنـویــســیــ بـا طـراوتـــــــ Clean Code تـوحـیـد ارسـطـو
  2. 2. برنامه‌ای که خوب نوشته شود، بهشت خودش است برنامه‌ای که بد نوشته شود جهنم خودش است
  3. 3. History of two writer
  4. 4. What is clean code?
  5. 5. یک برنامه باید سبک و سریع‌الانتقال باشد، روالهایش همانند رشتهای از مروارید به یکدیگر متصل باشند. روح و نیت برنامه‬ ‫باید همواره حفظ شود. نه باید چیزی زیاد باشد و نه چیزی کم، نه چرخه‌هایی که به آنها نیازی نیست و نه متغیرهای بیفایده،‬ ‫نه کمبودی در ساختار و نه انعطاف ناپذیری.‬
  6. 6. ی‫ک برنامه باید از «قانون کمترین سرگشتگی» پیروی کند. این قانون چیست؟ به این معنی است که برنامه باید همواره به‌نوعی به کاربر پاسخ دهی کند که کمترین میزان سرگشتگی را به همراه داشته باشد.‬ ‫یک برنامه، هرچقدر هم که پیچیده باشد، باید همانند یک واحد یکتا عمل نماید. یک برنامه باید به وسیله منطق درونی خویش‬ ‫هدایت شود و نه ظواهر بیرونی.‬
  7. 7. ‫اگر برنامهای این التزامات را برآورده نکند، در حالتی از بی‌نظمی و آشفتگی قرار خواهد گرفت. تنها راه تصحیح آن بازنویسی‬ ‫برنامه است‬.
  8. 8. اصول و اسلوب‌های کدنویسی باطراوت
  9. 9. In the early days of programming we composed our systems of routines and subroutines. in Fortran it was programs, subprograms, and functions. Nowadays only the function survives.
  10. 10. Clean code Functions <ul><li>The first rule: </li></ul>They should be small. <ul><li>The second rule: </li></ul>They should be smaller than that.
  11. 11. Bad!!! function student($op, $name, family, $age, $sex, $id) { if($op == 'add') { $query = “check student exist in data base” connect_to_database(); excute_query($query); disconnect_from_database(); if(student not exist) { $error = “student not exist”; return $error; ) $query = “add student to database where name = $name and family = $family …”; connect_to_database(); excute_query($query); disconnect_from_database(); } if($op == 'delete') { $query = “check student exist in data base” connect_to_database(); excute_query($query); disconnect_from_database(); if(student not exist) { $error = “student not exist”; return $error; ) $query = “delete student from database where name = $name and family = $family …”; connect_to_database(); excute_query($query); disconnect_from_database(); } $message = “operatin $op completed”; return $message }
  12. 12. Clean :) function add_student($name, family, $age, $sex) { $query = “add student to data base”; excute_query($query); $id = get_last_insert_id(); return $id; }
  13. 13. Functions should not be 100 lines long. Functions should hardly ever be 20 lines long. Smaller Than a Screenful
  14. 14. Functions should Do one thing.
  15. 15. Problems when function do more than one things Bugs and Complexity
  16. 16. Use descriptive names.
  17. 17. Example I changed the name of one example function from subscription_mail To send_subscription_email This is a far better name.
  18. 18. Half the battle to achieving that principle is: choosing good names for small functions, that do one thing.
  19. 19. The smaller and more focused a function is, the easier it is to choose a descriptive name. Conversely, if you can’t choose a descriptive name, Your function is probably too big And does more than ONE THING.
  20. 20. Long names Don’t be afraid to make a name long. A long descriptive name is better than a short enigmatic name. a long descriptive comment. Use a naming convention that allows multiple words to be easily read in the function names Like Camel Case or Underscores. sendSubscriptionEmail send_subscription_email Make use of those multiple words to give the function a name that says what it does.
  21. 21. No more than three arguments.
  22. 22. ideal number of arguments for a function is zero ( niladic ). Next comes one ( monadic ), Followed closely by two ( dyadic ). Three arguments ( triadic ) should be avoided where possible. More than three ( polyadic ) requires very special justification, and then shouldn’t be used anyway.
  23. 23. love_message() is easier to understand than love_message($extra).
  24. 24. Common Monadic Forms There are two common reasons to pass a single argument into a function. You may be asking a question about that argument as in: file_exists(“MyFile”) Or you may be operating on that argument, transforming it into something else and returning it. For example: file_open(“MyFile”) transforms a string into an InputStream return value. These two uses are what readers expect when they see a function. You should choose names that make the distinction clear.
  25. 25. Asking vs. Telling
  26. 26. Functions should either do something, or answer something, but not both.
  27. 27. Example if (set(&quot;username&quot;, &quot;unclebob&quot;))... What does that mean? Is it asking whether the “username” attribute was previously set to “unclebob”? successfully set to “unclebob”? It’s hard to infer the meaning from the call because it’s not clear whether the word “set” is a verb or an adjective.
  28. 28. Solution The real solution is to separate the command from the query so that the ambiguity cannot occurr. if (attribute_exists(&quot;username&quot;)) { set_attribute(&quot;username&quot;, &quot;unclebob&quot;); ... }
  29. 29. Prefer exceptions to returning error codes.
  30. 30. Example Function do_that() { If (time() < 123543214) { return “error”; } $doing = do_this; If ($doing == 'this_work') { … } …; }
  31. 31. منابع: Clean code book
  32. 32. Any question?

×