Discord.NET Bot Development (S2) · Analyzers and coding conventions · Episode 5 [OUTDATED, SEE DESC]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone and welcome to the fifth episode of the discord.net bot development series in this episode it's time to talk a bit more about analyzers and coding conventions keep in mind that this tutorial will not be focusing a lot on discord.net per se we're not going to add some really interesting new features to our bot but this is more about making our code more readable for everyone that might use it in the future which might be really convenient to do if you're going to work on open source projects but also closed source projects where you're going to share code with other people this episode may also be a bit shorter than other episodes since this will be a bit more about how can we make our code more readable to other people and how can we make sure that we are sticking to the coding conventions that have been agreed upon first what are coding conventions if we go to our browser i have opened up a page that will be in the description down below made by microsoft that goes a bit more in-depth about coding conventions what they ensure is that our code has a consistent look everywhere this means that when you stick to decoding conventions the way that the code is written for instance when we use case sensitivity how we name some variables and related will all be consistent across projects there's also a few things that microsoft say about it over here coding conventions serve the following purposes they create a consistent look to the code so that readers can focus on content not the layout they enable readers to understand the code more quickly by making assumptions based on previous experience they facilitate copying changing and maintaining the code and they demonstrate c sharp best practices so this is even about some best practices that you can do in c sharp if we read down there's a few things that we can go through naming conventions layout conventions commenting conventions and language guidelines this means that for each part we have separate conventions but to write code and constantly have this page open and to check every single small thing if you're meeting those conventions that might be a bit difficult and it's also quite inefficient because you would be spending more time on making sure you're following these conventions and actually programming and that's not really what you want to help us with that there exists something that's called analyzers one of the analyzers that we're going to use will be stylecop.analyzers it's one of the most popular analyzers for c-sharp and really make sure that we are meeting all of the conventions that are described in for instance this document let's take a look if we had the visual studio and we had to tools and nuget package manager and manage nuget packages for solution we can then here search for stylecop dot analyzers in here there's a library that we can use which has 43.1 million downloads and is made by sam harwell press the library make sure you select your project over here and install the latest stable version when it's done you will see that a lot of warnings will pop up that's good it means that it's working and that means that our analyzer is actually checking our code and is checking if we are in compliance with decoding conventions let's start by taking a look at our program.cs let's go all the way up and we will see a lot of green underlined arts if we hover over the first part it will say that using directive should appear within a namespace declaration this means that we should move this part inside our namespace now you could do this manually by copying this part over to here or you could hover over this part as the light bulb and say reorder using statements which will not only put all the using statements over here but it will also sort them alphabetically you might say hey but the s does not come before the d that's true this is because all system using directives actually appear above all other using directives so the system using directives are the only exception to the sorting now there's still a very small part that's underlined at the top of our file it says the file header is missing or not located at the top of the file say you're working on a closed source project in that case it might be nice to have a copyright mention or disclaimer at the top of your file this analyzer actually allows you to do that if you press on the light bulb it will give you an option to say add file header which will paste a nice copyright header over here but since this project is open source and we don't really have any copyright on this i will get rid of this part but now you might say won't that warning come back again that's true you're also able to actually suppress warnings and say like hey we have a good reason not to do this to do so you hover over the part that has a warning you press the light bulb and you say suppress or configure issues as you can see if we head back here the error associated with this problem is sa1633 so if we go back to the light bulb to press or configure issues we can suppress or configure that very sa1633 error or in this case warning now it gives you two options you can either adjust this in the source which will then allow us to disable this warning within this file or we could use a suppression file which will disable this warning over the entire project or some targets that we specify press on in suppression file this will create a global suppressions file where we can define some of the warnings that we do not want to see a nice thing here is that you can also give a justification as per why you do not want this to be here so here we can just say project does not have copyright licensed as mit you can also define a scope and a target you can also get rid of those two and just keep the justification which will just disable this warning entirely regardless of where which might be nice if we go back to the program.cs the next warning that we get is that the class there's two warnings actually here essay 1600 saying elements should be documented and sa1400 the element program should declare an access modifier the first warning says hey you need to document each element and in this case that's a class this is nice because in the future someone might not understand exactly what the purpose is of this class now of course for a program class it's the entry point of your application so that might be easy to explain but this general class what does that do so for here what we will just say is we will create a comment and we do this by pressing the forward slash three times and here we can now give a summary just like with commands for instance but as a summary we can explain like hey what's the purpose of this class well we will say the entry point of the bot just like that the next thing that we have is that we need to declare an axis modifier for our program class we can do this by either providing one in front of it or we can go and hover over it press on the light bulb and say declare accessibility which will then add internal in front of class which will then declare the access modifier of program to be internal the next warning that we see is main this main method should declare again an axis modifier and we will set the access modifier to private now we check our file make sure there are no warnings in it and that means we are now in compliance with decoding conventions there's also one final thing that you can do which is if you click on all of the using directives and you press on the light bulb you can get rid of all unnecessary using statements like the system using directive that we had at the top and now our file is really clean and in compliance with all the coding conventions the next file that we will take a look at is the general class again because we want these using draft tests to be in under the namespace we press the light bulb and say reorder using statements which will again put them in alphabetical order plus move them inside our namespace we can then get rid of the using directives that are not used and then we have a warning in general which will tell us that hey you need to document this element so we use three forward slashes and we say the general module containing commands like ping of course you can just pick your own description but this is a more general description that i am using but you could use a completely different one and you should also use a different one when you're working on a different module the next warning appears here because the ping method also needs to be documented so we pressed on a light bulb and we'll say generate method documentation now in here we can define the summary and now we can just say a command that will respond with pong just like that and now we have a summary for this command this also means that if you now hover over ping async you will see here that there's a nice description of this method it says a command that will respond with pong it's pretty nice if you hover over general you will see the same happening you will now see an actual description appearing over here pretty nice the next warnings appear at context it says prefix local calls with this let's do that hover over the light bulb press it and say prefix reference with this now a nice thing that you can do here is you can choose to apply this over the entire file you do this by simply saying fix all occurrences in document and then press apply which will fix this warning over the entire file in just a few clicks now we go to the info async method we press the light bulb and we say generate method documentation here we say a command to get some information about a user and now we can explain why there needs to be this socket go user parameter so we will just explain it we will say an optional user to get the information from now if we hover over the info async command you will see the description a command to get some information about the user and say for instance if we were here and we would say info async then we would also get an explanation about this socket go user over here you see socket go user an optional user to get the information from which is the very description that we have defined over here i will now also get rid of line 23 as it's no longer necessary and we just have this simple ping command the next file that we will take a look at is the command handler over here we go all the way up top we hover over these warnings and if the light bulb does not appear you can always choose to press ctrl period and then say reorder using statements now if we go to the lab again we'll also say remove unnecessary using directives and then we will have a warning at the command handler we create a new comment and we say the class responsible for handling the commands and various events if you hover over command handler you'll see the very description standing over there the command handler is also the only class that has a constructor and constructors have their own documentation as well hover over command handler over here press the light bulb and say generate constructor documentation here we need to provide a description of each of the elements that we inject so for the provider we will say the and what we can then do is we can refer to for instance a type we do this by saying a arrow to the left c and press tab and then we say i service provider so referring to the type like this and then we say that should be injected now if you hover over the provider you will see that it makes it nicely green just like the interface type over here so now it says the i service provider that should be injected for the client we will do the same d and then arrow to the left c tab and then we say discord socket client that should be injected for the command service we say d c command service it should be injected and for the i configuration we say the i configuration that should be injected just like that now if we go a bit more down we have the initialize async task now because the initialize async test comes from initialized service which the command handler inherits from we can actually go to the light bulb and say inherit documentation and in this case if we hover over it again it just copies the explanation of this method from the initialize service because if we go in here and we open up this one you can see the summary of the initialize async method which if we go back to the command handler we can see again over here we also have two warnings over here because we again need to prefix these methods with the dis let's do that again and then if we go a bit more down we again have two warnings braces should not be emitted basically meaning that if we press on a light bulb it will add back the curly brackets that we actually emitted over there and we'll do the same over here and we'll do the same over here and now there are zero warnings meaning that we are now in compliance with the coding conventions this is pretty nice now before we end this episode i will quickly show you how to configure the file header for instance if you're working on a closed source project to do so we simply get rid of the global suppressions file by right clicking it pressing the lead and pressing ok and then we go for instance to this namespace over here we press the light bulb and we say add style cup settings to the project this will create a new file called stylocopter.json and in here we can define the name of our company we'll just say coding with abraham then we go back to the command handler we go to the namespace over here press the lightbulb and you'll see that the warning still appears that's weird that's because we skipped a small part at the very top of this file because we need to do something so that this file is actually being used by the analyzer to do this we simply right click the stylecopt.json file press properties go all the way down to build action and we select here c sharp analyzer additional file you might ask why do we need to do this this is because this file is actually part of a nuget package therefore sometimes you need to manually add some other things because it's not something like a plugin therefore we have to specify the build action like this now if we go back to for instance the command handler we will see the warnings again we press the light bulb and say add file header which will then nicely add the header over here and now you can apply the same to the program.cs and general.css i will for now just get rid of the file headers again delete the stylecop.json file and tell my program to suppress this very warning by going to suppress or configure issues suppress sa1633 in a suppression file going to the file and saying in a justification not needed as project is licensed under mit and we get rid of both the scope as well as the target now if we close general.cs command handler and the program.cs and reopen them one by one you will see that the warnings will disappear and that now our code is fully in compliance with decoding conventions just to make sure everything is still working we launch the bot head over to discord and run our ping command and as you can see it still works perfectly fine thank you for watching i hope you've enjoyed this episode and that it makes a bit more sense about what coding conventions and analyzers are you will also see that some packages like discord.net also have their own analyzers if you want to definitely check them out because they can be very interesting and tell you some techniques that you don't even know yet before we end this episode i would like to thank everyone that has become part of our patreon page in these last couple of days we have reached our two goals the first one of five patrons where we would start doing live streams and coding nights and the second one of 10 patrons where we will go through asp.net core and create our own web dashboard for our discord bot which we are developing right now watermelon if you haven't already take a look at our patreon page and become part of our patreons and as of course one of the benefits of becoming a patreon is the shout outs i would like to say a special thank you to jason thompson run free steve martz jay freeb paintballer 007 fem duck kyle tekashi89 hallelujah and nicholas barclay thank you everyone for becoming a patron i hope you've enjoyed this episode and i hope to see you in the next one you
Info
Channel: Coding with Efehan
Views: 1,037
Rating: undefined out of 5
Keywords: discord, discord bot, c# bot, discord.net, discord.net bot, c#, tutorial, programming, c# discord bot, c# discord, analyzers, coding conventions
Id: 4HU1bIIdXaE
Channel Id: undefined
Length: 19min 30sec (1170 seconds)
Published: Mon Jun 07 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.