Reader small image

You're reading from  Software Architecture with C# 12 and .NET 8 - Fourth Edition

Product typeBook
Published inFeb 2024
PublisherPackt
ISBN-139781805127659
Edition4th Edition
Right arrow
Authors (2):
Gabriel Baptista
Gabriel Baptista
author image
Gabriel Baptista

Gabriel Baptista has been working with software development since the beginning of .NET. Today, his main contributions are managing numerous projects for retail and industry. He is an Azure Platform-as-a-Service (PaaS) solution specialist, teaches at Computing Engineering universities, and helps tech startups as a mentor.
Read more about Gabriel Baptista

Francesco Abbruzzese
Francesco Abbruzzese
author image
Francesco Abbruzzese

Francesco Abbruzzese dedicates his life to his two great passions: software and powerlifting. He is the author of the MVC Controls Toolkit and the Blazor Controls Toolkit libraries. He has contributed to the diffusion and evangelization of the Microsoft web stack since the first version of ASP.NET. His company, Mvcct Team, offers web applications, tools, and services for web technologies. He has moved from AI systems, where he implemented one of the first decision support systems for financial institutions, to top-10 video game titles such as Puma Street Soccer.
Read more about Francesco Abbruzzese

View More author details
Right arrow

Writing safe code in C#

C# can be considered a safe programming language by design. Unless you force it, there is no need for pointers, and memory release is, in most cases, managed by the garbage collector. Even so, some care should be taken so you can get better and safer results from your code. Let us have a look at some common practices to ensure safe code in C#.

try-catch

Exceptions in coding are so frequent that you should have a way to manage them whenever they happen. try-catch statements are built to manage exceptions and they are important for keeping your code safe. There are a lot of cases where an application crashes and the reason for that is the lack of using try-catch. The following code shows an example of the lack of usage of the try-catch statement. It is worth mentioning that this is just an example for understanding the concept of an exception thrown without correct treatment. Consider using int.TryParse(textToConvert, out int result) to handle cases where a parse...

.NET 8 tips and tricks for coding

.NET 8 implements some good features that help us to write better code. One of the most useful things for having safer code is dependency injection (DI), which will be discussed in Chapter 5, Design Patterns and .NET 8 Implementation. There are some good reasons for considering this. The first one is that you will not need to worry about disposing of the injected objects since you are not going to be the creator of them.Besides, DI enables you to inject ILogger, a useful tool for debugging exceptions that will need to be managed by try-catch statements in your code. Furthermore, programming in C# with .NET 8 must follow the common good practices of any programming language. The following list shows some of these:

  • Classes, methods, and variables should have understandable names: The name should explain everything that the reader needs to know. There should be no need for an explanatory comment unless these declarations are public.
  • Methods cannot have...

Identifying well-written code

It is not easy to identify whether code is well written. The best practices described up to here can certainly guide you as a software architect to define a standard for your team. But even with a standard, mistakes will happen, and you will probably find them only after the code is in production. The decision to refactor code in production just because it does not follow all the standards you define is not an easy one to take, especially if the code in question is working properly. Some people conclude that well-written code is simply code that works well in production. However, this can surely cause damage to the software’s life since developers can be inspired by that non-standard code.For this reason, as a software architect, you need to find ways to enforce adherence to the coding standard you’ve defined. Luckily, nowadays, we have many options for tools that can help us with this task. They are called static code analysis tools and using...

Summary

In this chapter, we discussed some important tips for writing safe code. This chapter introduced a tool for analyzing code metrics so that you can manage the complexity and maintainability of the software you are developing. To finish, we presented some good tips to guarantee that your software will not crash due to memory leaks and exceptions. In real life, a software architect will always be asked to solve this kind of problem.The chapter also presented tools that can be used to apply the best practices of coding we discussed. We looked at the Roslyn compiler, which enables code analysis while the developer is coding.You will find in Chapter 21, Case Study, a way to evaluate C# code prior to publishing an application, which implements code analysis during the Azure DevOps building process using Sonar Cloud.When you apply all the content you have learned in this chapter to your projects, you will find that code analysis will give you the opportunity to improve the quality of...

Questions

  1. Why do we need to care about maintainability?
  2. What is cyclomatic complexity?
  3. List the advantages of using a version control system.
  4. What is garbage collector?
  5. What is the importance of implementing the IDisposable interface?
  6. What advantages do we gain from .NET 8 when it comes to coding?
  7. What makes it possible for a piece of software to be described as having well-written code?
  8. What is Roslyn?
  9. What is code analysis?
  10. What is the importance of code analysis?
  11. How does Roslyn help with code analysis?
  12. What are Visual Studio extensions?
  13. What extension tools are available for code analysis?

Further reading

These are some books and websites where you will find more information about the topics of this chapter:

Further reading

The following are some books and websites where you will find more information about the topics covered in this chapter:

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Software Architecture with C# 12 and .NET 8 - Fourth Edition
Published in: Feb 2024Publisher: PacktISBN-13: 9781805127659
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Authors (2)

author image
Gabriel Baptista

Gabriel Baptista has been working with software development since the beginning of .NET. Today, his main contributions are managing numerous projects for retail and industry. He is an Azure Platform-as-a-Service (PaaS) solution specialist, teaches at Computing Engineering universities, and helps tech startups as a mentor.
Read more about Gabriel Baptista

author image
Francesco Abbruzzese

Francesco Abbruzzese dedicates his life to his two great passions: software and powerlifting. He is the author of the MVC Controls Toolkit and the Blazor Controls Toolkit libraries. He has contributed to the diffusion and evangelization of the Microsoft web stack since the first version of ASP.NET. His company, Mvcct Team, offers web applications, tools, and services for web technologies. He has moved from AI systems, where he implemented one of the first decision support systems for financial institutions, to top-10 video game titles such as Puma Street Soccer.
Read more about Francesco Abbruzzese