- عنوان کتاب: Clean Code Principles And Patterns, Python Edition
- نویسنده: Petri Silen
- حوزه: برنامه نویسی پایتون
- سال انتشار: 2025
- تعداد صفحه: 676
- زبان اصلی: انگلیسی
- نوع فایل: pdf
- حجم فایل: 12.4 مگابایت
این کتاب به شما یاد میدهد که چگونه کد تمیز بنویسید. این کتاب اصول و الگوهای طراحی و توسعه نرمافزار را به شیوهای بسیار کاربردی ارائه میدهد. این کتاب برای توسعهدهندگان مبتدی و ارشد مناسب است. آشنایی با برخی از دانشهای اولیه برنامهنویسی در پایتون ضروری است. همه مثالهای این کتاب به زبان پایتون ارائه شدهاند، به جز برخی از مثالهای مربوط به کد frontend که به زبان جاوااسکریپت/تایپاسکریپت هستند. محتوای این کتاب به ده فصل تقسیم شده است. همه مثالهای پایتون در این کتاب به پایتون ۳.۱۱ یا بالاتر نیاز دارند. این کتاب عمدتاً برای توسعهدهندگان نرمافزار است. به همین دلیل، برخی از مباحث به طور کامل پوشش داده نشدهاند. این شامل مباحث مربوط به معماری، DevSecOps، E2E و تست غیرعملکردی است. این مباحث بیشتر مربوط به معماران نرمافزار، متخصصان DevOps و مهندسان تست/QA هستند. من میخواهم آنها را در این کتاب پوشش دهم زیرا همیشه خوب است که درک اولیهای از مباحث مرتبط با توسعه نرمافزار داشته باشید. این کتاب اصول، بهترین شیوهها و الگوهای زیادی را ارائه میدهد. درک همه آنها در یک بار خواندن میتواند دشوار باشد، که هدف این نیست. شما باید مرتبطترین موضوعات را برای خودتان انتخاب کنید، آنها را بپذیرید و سعی کنید در کدنویسی روزمره از آنها استفاده کنید. همیشه میتوانید برای یادگیری موضوعات بیشتر به کتاب مراجعه کنید. برخی از اصول، الگوها و شیوهها میتوانند ذهنی و قابل بحث باشند، اما من فقط اصول، الگوها و شیوههایی را که خودم استفاده کردهام یا خواهم کرد، در این کتاب آوردهام. در آخرین فصل نتیجهگیری، موضوعاتی را که بر اساس تجربهام در پروژههای واقعی مهمترین میدانم، فهرست خواهم کرد. فصل دوم در مورد اصول طراحی معماری است که امکان توسعه میکروسرویسهای واقعی ابری را فراهم میکند. اولین اصل طراحی معماری که شرح داده شده است، اصل مسئولیت واحد است که تعریف میکند یک قطعه نرمافزار باید در سطح انتزاع خود مسئول یک چیز باشد. سپس، یک اصل نامگذاری یکسان برای میکروسرویسها، کلاینتها، APIها و کتابخانهها ارائه میشود. اصل کپسولهسازی تعریف میکند که چگونه هر مؤلفه نرمافزار باید وضعیت داخلی خود را پشت یک API عمومی پنهان کند. اصل تجمیع سرویس با توضیح مفصلی در مورد چگونگی ادغام میکروسرویسهای سطح بالاتر توسط میکروسرویسهای سطح پایینتر معرفی میشود. الگوهای معماری مانند منبعیابی رویداد، تفکیک مسئولیت پرسوجوی فرمان (CQRS) و تراکنشهای توزیعشده مورد بحث قرار میگیرند. تراکنشهای توزیعشده با مثالهایی با استفاده از الگوی ارکستراسیون saga و الگوی رقصپردازی saga پوشش داده میشوند. شما در مورد چگونگی جلوگیری از تکرار کد در سطح معماری پاسخهایی دریافت میکنید. اصل پیکربندی خارجیشده نحوه مدیریت پیکربندی سرویس در محیطهای مدرن را شرح میدهد. ما در مورد اصل جایگزینی سرویس بحث میکنیم، که بیان میکند سرویسهای وابستهای که یک میکروسرویس استفاده میکند باید به راحتی قابل جایگزینی باشند. اهمیت میکروسرویسهای خودکار از دیدگاه بیحالتی، تابآوری، در دسترس بودن بالا، مشاهدهپذیری و مقیاسبندی خودکار مورد بحث قرار میگیرد. در انتهای فصل، بحثی در مورد روشهای مختلف ارتباط میکروسرویسها وجود دارد. چندین قانون در مورد نحوه نسخهبندی اجزای نرمافزار ارائه شده است. این فصل با بحث در مورد اینکه چرا محدود کردن تعداد فناوریهای مورد استفاده در یک سیستم نرمافزاری مفید است، به پایان میرسد. فصل سوم اصول طراحی شیگرا را ارائه میدهد. ما این فصل را با مفاهیم برنامهنویسی شیگرا و الگوهای برنامهنویسی آغاز میکنیم و به دنبال آن اصول SOLID را دنبال میکنیم: اصل مسئولیت واحد، اصل باز-بسته، اصل جایگزینی لیسکوف، اصل تفکیک رابط و اصل وارونگی وابستگی. هر اصل SOLID با مثالهای واقعگرایانه اما ساده ارائه میشود. اصل نامگذاری یکنواخت، روشی یکنواخت برای نامگذاری رابطها، کلاسها، توابع، جفتهای تابع، توابع بولی (گزارهها)، سازنده، کارخانه، تبدیل و متدهای چرخه عمر تعریف میکند. اصل کپسولهسازی توضیح میدهد که یک کلاس باید حالت داخلی خود را کپسوله کند و اینکه چگونه تغییرناپذیری به تضمین کپسولهسازی حالت کمک میکند. اصل کپسولهسازی همچنین در مورد اهمیت عدم نشت حالت داخلی یک شیء به بیرون بحث میکند. اصل ترکیب شیء تعریف میکند که ترکیب باید بر وراثت ترجیح داده شود. طراحی تاکتیکی مبتنی بر دامنه (DDD) با دو مثال از دنیای واقعی ارائه شده است. تمام الگوهای طراحی از کتاب الگوهای طراحی GoF با مثالهای واقعگرایانه اما ساده ارائه شدهاند. اصل نپرس، بگو به عنوان راهی برای جلوگیری از بوی طراحی حسادت به ویژگی ارائه شده است. این فصل همچنین در مورد اجتناب از وسواس اولیه و مزایای استفاده از آرگومانهای تابع با اعتبارسنجی معنایی بحث میکند. این فصل با ارائه اصل تزریق وابستگی و اصل اجتناب از تکرار کد به پایان میرسد. فصل چهارم در مورد اصول کدنویسی است. این فصل با اصلی برای نامگذاری یکنواخت متغیرها در کد آغاز میشود. یک قرارداد نامگذاری یکنواخت برای اعداد صحیح، ممیز شناور و … ارائه شده است.
This book teaches you how to write clean code. It presents software design and development principles and patterns in a very practical manner. This book is suitable for both junior and senior developers. Some basic knowledge of programming in Python is required. All examples in this book are presented in Python, except some examples related to frontend code, which are in JavaScript/TypeScript. The content of this book is divided into ten chapters. All Python examples in this book require Python 3.11 or later. This is a book for primarily software developers. For that reason, some topics are not covered exhaustively. This includes topics related to architecture, DevSecOps, E2E, and non-functional testing. Those are topics most relevant to software architects, DevOps specialists and test/QA engineers. I want to cover them in this book because it is always good to have a basic understanding of topics closely related to software development. This book presents a lot of principles, best practices, and patterns. It can be difficult to grasp them all on a single read, which is not the purpose. You should pick the most relevant topics for yourself, embrace them, and try to put them into use in everyday coding. You can always return to the book to learn additional topics. Some principles, patterns, and practices can be subjective and open for debate, but I have only put into this book such principles, patterns, and practices that I have used or would use myself. In the last Conclusion chapter, I will list the topics that I consider the most important based on my experience in real-life projects. The second chapter is about architectural design principles that enable the development of true cloudnative microservices. The first architectural design principle described is the single responsibility principle, which defines that a piece of software should be responsible for one thing at its abstraction level. Then, a uniform naming principle for microservices, clients, APIs, and libraries is presented. The encapsulation principle defines how each software component should hide its internal state behind a public API. The service aggregation principle is introduced with a detailed explanation of how a higher-level microservice can aggregate lower-level microservices. Architectural patterns like event sourcing, command query responsibility segregation (CQRS), and distributed transactions are discussed. Distributed transactions are covered with examples using the saga orchestration pattern and the saga choreography pattern. You get answers on how to avoid code duplication at the architectural level. The externalized configuration principle describes how service configuration should be handled in modern environments. We discuss the service substitution principle, which states that dependent services a microservice uses should be easily substitutable. The importance of autopilot microservices is discussed from the point of view of statelessness, resiliency, high availability, observability, and automatic scaling. Towards the end of the chapter, there is a discussion about different ways microservices can communicate. Several rules are presented on how to version software components. The chapter ends with discussing why limiting the number of technologies used in a software system is helpful. The third chapter presents object-oriented design principles. We start the chapter with object-oriented programming concepts and programming paradigms followed by the SOLID principles: Single responsibility principle, open-closed principle, Liskov’s substitution principle, interface segregation principle, and dependency inversion principle. Each SOLID principle is presented with realistic but simple examples. The uniform naming principle defines a uniform way to name interfaces, classes, functions, function pairs, boolean functions (predicates), builder, factory, conversion, and lifecycle methods. The encapsulation principle describes that a class should encapsulate its internal state and how immutability helps ensure state encapsulation. The encapsulation principle also discusses the importance of not leaking an object’s internal state out. The object composition principle defines that composition should be preferred over inheritance. Tactical Domain-driven design (DDD) is presented with two real-world examples. All the design patterns from the GoF’s Design Patterns book are presented with realistic yet straightforward examples. The don’t ask, tell principle is presented as a way to avoid the feature envy design smell. The chapter also discusses avoiding primitive obsession and the benefits of using semantically validated function arguments. The chapter ends by presenting the dependency injection principle and avoiding code duplication principle. The fourth chapter is about coding principles. The chapter starts with a principle for uniformly naming variables in code. A uniform naming convention is presented for integer, floating-point, boolean, string, enum, and collection variables. Also, a naming convention is defined for maps, pairs, tuples, objects, and callback functions. The uniform source code repository structure principle is presented with examples. Next, the avoid comments principle lists reasons why most comments are unnecessary and defines concrete ways to remove unnecessary comments from the code. The following concrete actions are presented: naming things correctly, returning a named value, returntype aliasing, extracting a constant for a boolean expression, extracting a constant for a complex expression, extracting enumerated values, and extracting a function. The chapter discusses the benefits of using type hints. We discuss the most common refactoring techniques: renaming, extracting a method, extracting a variable, replacing conditionals with polymorphism, introducing a parameter object, and making anemic objects rich objects. The importance of static code analysis is described, and the most popular static code analysis tools are listed. The most common static code analysis issues and the preferred way to correct them are listed. Handling errors and exceptions correctly in code is fundamental and can be easily forgotten or done wrong. This chapter instructs how to handle errors and exceptions and return errors by returning a boolean failure indicator, an optional value, or an error object. The chapter instructs how to adapt code to a wanted error-handling mechanism and handle errors functionally. Ways to avoid off-by-one errors are presented. Readers are instructed on handling situations where some code is copied from a web page found by googling or generated by AI. Advice is given on what data structure is the most appropriate for a given use case. The chapter ends with a discussion about code optimization: when and how to optimize.
این کتاب را میتوانید از لینک زیر بصورت رایگان دانلود کنید:
Download: Clean Code Principles And Patterns, Python Edition
نظرات کاربران