Coding standards in R are really important because they help you make your code readable and allow you and other people to understand what's going on in your code. Now of course just like it is with any other style you know whether it's your clothing or whatever it is, it's difficult to get everyone to agree on one set of ideas. But I think there are a couple of very basic, kind of minimal standards that are important when you're coding in R. All right, so I'm just going to talk a little bit about some of the coding standards that I think are important when you're writing R code. And I think will help make your code more readable and more usable by others if that's what you're trying to, to achieve. So the first principle that I think is very important in pretty much any programming language, not just R. Is that you should always write your code using a text editor and saved as a text file. Okay so a text file is a kind of basic standard that usually doesn't have any sort of formatting or any sort of kind of special appearance. It's just text, right? And usually typically, typically it's going to be ASCII text. But if you're on, in places outside um,uh, the US or the UK using non-English languages, there may be other standard text formats. But the basic idea is that a text format can be read by pretty much any basic editing program. These days, you know when you're writing something, there's a lot of different tools that you can use to write. If you're writing a book or a, or a webpage or something like that there's all kinds of different tools that you can use to write to use, to write those things. But when you're writing code you should always try to use a text editor because that's like kind of like the, the kind of least common denominator. And it makes it so that everyone will be able to access your code and improve upon it. The second principle, is, which is very important for readability, is to indent your code. So indenting is something that's often hotly debated in lots of mailing lists and other types of discussion groups, in terms of how much indenting is appropriate. Now, I'm not going to talk about that, although I do have some recommendations. But I think the most important thing is that you understand why indenting is important. So indenting is the idea that different blocks of codes should be spaced over to the right. A little bit more than other blocks of codes so, you can see kind of how the control flow, how the flow of the program goes based on the indenting alone. So coupled with indenting is the third principle which I think is very simple which the limit the width of your code. So, you have indenting, it's possible to kind of indent off to the right forever. So you need to limit on the right hand side how wide your code is going to be, and usually this is kind of determined by number of columns of text. And so one possibility is you limit your text to about 80 columns. Of text and then, and so that you're, the width of your code never exceeds that. So, let's take a look for, at a quick example here. So, here you can see I've got our studio open here. With this simple code file with some R code in it. And, and, first of all, let me just mention that the editor in our studio is a text editor. So, it will always save the R files that you write as text format files. so, we've already got that kind of handled. But you can see, the indenting scheme here is equal to one space. So every indent is one space. And you can see that all of the code is kind of mashed together here on the left-hand side. It's difficult to tell kind of where the if blocks are, where the else blocks are, where does the function kind of end and begin. And so the indenting scheme kind of makes the code not very readable in this case. So we can change the indenting in our studio. And if I just go up to the preferences menu here and go up to code editing and let me just change it to four and you can see that the column the margin column is set to 80 characters. So it will show you the margin when you've reached 80 characters. And so I'm going to select all here with the command a and then command i to indent this. So now you can see that the indenting is a little bit nicer now. You can see, kind of where the function begins and ends. You can see where the if blocks start and end. and, and the kind of structure of the program is much more obvious. so, I'm going to change this one more time, though. And, and my, my personal preference for indenting is to use eight spaces. So I'm going to change this to eight. Hit OK and Select All > Cmd+I. And now you can see I I prefer the eight spaces just because it really makes the structure of the code very obvious. And the spacing is nice and clear and it makes the code very readable in general. So you can see that indenting is very important. And the biggest problem you might have is with too little indenting. If you don't indent at all or if you only use a very small amount, the code becomes kind of very mashed together. So I recommend at least 4 spaces for an indent. And I prefer eight spaces for an indent, just because it makes the code much more readable and spaces it out much more nicely. One of the advantages of having something like an eight space indent is coupled with an 80 character margin on the right hand side. Is that it forces you to think about your code in a slightly different way. So for example if you have eight space indents if you are going to have a four loop nested within another four loop within another four loop. Every time you nest another four loop for example you have to indent over eight spaces. And by the time you get to maybe your fourth nested four loop. You're pretty much hitting the right hand column at the 80 column margin, right? And so the nice thing about the eight space indent, coupled with the 80 column margin. Is that it prevents you from kind of writing very basic, making kind of very kind of fundamental mistakes with code readability. So for example with eight space indent, and 80 column margin. You might be able to do easily more than two nested four loops. And but I think that's really the kind of the, the boundary of what is readable in terms of code. Typically, except for some special cases, a three, you know, a three nested or four nested for-loop is difficult to read, and it's probably better off splitting off into separate functions or something like that. So you know, a good indenting policy not only, makes the code more readable. But it actually can force you to think about writing your code in a slightly different way. And so that's a really nice advantage of, of, of having a logical indenting policy with, coupled with a, you know, a right hand side restriction, right? So the last, thing I want to talk about is to limit the length of your functions. Alright, so functions in R can, can theoretically go on for quite a long time and, of course, just like in any other language. But just like in any other language, I think the logical thing to do with the function is to limit to kind of one basic activity. So, for example, if your function is named, read the data. Then your function should simply read the data. It should not read the data, process it, fit a model, and then print some output, alright? So you should, the logical kind of steps like that should probably be split into separate functions. There are a couple of different advantages to doing this. First of all it's nice to have a function written on a single page of code. So you don't have to scroll endlessly, to see you know where all the code for this function goes. If you can put all the function, the ent, the entire function on like, one screen of the editor, then you can look at the whole function and see what it does all at once. Another advantage of splitting up your code onto logical sections, to logical functions is that if you use functions like trace back or, or the profile or the debugger. These often tell you, you know, where in the function call stack you are when a problem occurs. And if you have multiple functions that are all logically divided into separate pieces. Then when a bug occurs and you know it occurs in a certain type of function, or a certain function, then you know kind of where to go to fix things, right? So if you, but if you just have a single function that just goes on forever and a bug occurs, then the only thing that the debugger or the traceback or the profiler can tell you is that there's a problem in this one function. But it doesn't, it's difficult to tell you where exactly the problem occurs. So splitting up your functions has a secondary benefit which is that it can help you in debug and profiling. So limiting the size of your functions is very useful for readability and for kind of debugging. Of course, it's easy to go overboard and to have, you know, 100 different three line functions. So that's not really what you want to do. So you just want to make it so that the, the separation of different functions is logical and each function kind of does does one thing in particular. So those are my basic guidelines for writing code in R there of course there are many other things that you might be able to think about. But then you bordering into areas that we might not we might kind of disagree on and so I'm not going to talk about it too much more in terms of coding standards. But the basic ideas are, always use a text editor, always indent your code, I'd say at least four spaces. Limit on the right-hand side how, how wide your code can be. And and always limit the size of your functions so that you can, so that they're kind of grouped into logical pieces of your program. So with those four things I think you'll, your, your code will be much more readable. It will be readable to you, it will be readable to others. And it will make kind of writing our code much more useful to everyone.