"Don't Use Fields in C#! Use Properties Instead" | Code Cop #003
Summary
TLDRIn this episode of Code Cop, Nick critiques bad coding advice found on platforms like LinkedIn, focusing on a post suggesting developers should prefer properties over class member variables. He explains why this advice is flawed, highlighting misunderstandings about encapsulation, validation, and clean code practices. Nick dives into technical details, showing how this approach could complicate testing, performance, and readability. He emphasizes the importance of critical thinking when following coding advice. Additionally, Nick promotes his new course on test-driven development, inviting viewers to join the discussion in the comments.
Takeaways
- 💡 The video focuses on debunking bad advice found on LinkedIn, Twitter, and blogs about writing clean code.
- 🤦♂️ The specific advice being critiqued is a LinkedIn post suggesting that properties are better than class member variables for clean code.
- 🧐 The presenter points out that properties are, by definition, class members, so the advice to prefer properties to class members is nonsensical.
- 👨💻 The bad advice in question suggests using private properties instead of private fields in classes, which the presenter strongly disagrees with.
- 🔍 The presenter explains that properties add unnecessary complexity and encapsulation for private members, which is counterintuitive when fields are sufficient.
- ❌ Using properties instead of fields results in performance degradation, as you might unnecessarily validate data or perform transformations every time the property is accessed.
- 🛠️ The underscore in private field naming conventions helps differentiate between class members and method parameters, and should not be removed just for aesthetics.
- 🚫 Reasons like encapsulation, validation, and lazy loading don’t apply when using private properties, and implementing these concepts in this way leads to bad design choices.
- 📉 Following this bad advice can make testing and mocking more complicated due to unnecessary logic in property getters, rather than in the constructor or a separate method.
- 🗣️ The presenter encourages viewers to critically analyze advice found online and invites them to discuss the points raised in the video in the comments.
Q & A
What is the main focus of the 'Code Cop' series?
-The main focus of the 'Code Cop' series is to review advice about writing clean code found on platforms like LinkedIn, Twitter, and blogs, and to critique and convert bad advice into good advice.
Why does Nick criticize LinkedIn in the video?
-Nick criticizes LinkedIn for promoting any content as long as it gets engagement, like a 'like', which can lead to the spread of bad advice.
What is the specific LinkedIn post advice that Nick is critiquing in this video?
-The advice is to prefer properties over class member variables, suggesting that properties are more versatile and cleaner than class member variables with underscores.
What is the example given in the video to illustrate the advice?
-The example is a class called 'Transaction' with a private field 'transactionPersistor' injected through the constructor, which the advice suggests should be replaced with a private property.
Why does Nick argue that the advice to use properties instead of fields doesn't make sense?
-Nick argues that properties are essentially class members and that using a property for a private field does not provide any additional benefits, as a field is still generated behind the scenes.
What is the issue with the property having the same name as the getter method according to Nick?
-Nick explains that the property name clashes with the name the compiler uses to generate the getter method, leading to a compilation error.
What is the course mentioned by Nick that was recently launched?
-The course mentioned is a six-hour course on 'D train' called 'From Zero to Hero Test Driven Development in C#'.
Who authored the 'From Zero to Hero Test Driven Development in C#' course?
-The course is authored by GE Herrera, who has an excellent YouTube channel and has worked with Nick personally.
What is Nick's opinion on the reasons given in the LinkedIn post for preferring properties?
-Nick finds the reasons provided in the post to be nonsensical, particularly for private members, and believes they lead to unnecessary complexity and potential performance issues.
What discount code does Nick mention for the 'From Zero to Hero Test Driven Development in C#' course?
-The discount code mentioned is 'td20'.
How does Nick suggest the advice could be misleading to developers?
-Nick suggests that the advice could lead to bad design choices and that developers might blindly follow it without questioning its validity.
Outlines
Этот раздел доступен только подписчикам платных тарифов. Пожалуйста, перейдите на платный тариф для доступа.
Перейти на платный тарифMindmap
Этот раздел доступен только подписчикам платных тарифов. Пожалуйста, перейдите на платный тариф для доступа.
Перейти на платный тарифKeywords
Этот раздел доступен только подписчикам платных тарифов. Пожалуйста, перейдите на платный тариф для доступа.
Перейти на платный тарифHighlights
Этот раздел доступен только подписчикам платных тарифов. Пожалуйста, перейдите на платный тариф для доступа.
Перейти на платный тарифTranscripts
Этот раздел доступен только подписчикам платных тарифов. Пожалуйста, перейдите на платный тариф для доступа.
Перейти на платный тарифПосмотреть больше похожих видео
5.0 / 5 (0 votes)