Best practices and coding conventions are things that companies really look for when they are interviewing for developers to join the team. Today we’ll go over a short list of things I consider to be best practices to follow. Most of the items on this list come from years of training and mentoring beginning engineers.
Instagram Firebase Course
iOS Basic Training Course
Completed Source Code
Follow me on Twitter:
hey what's going on guys welcome back to the channel hope you guys are having a good morning in today's video we are going to answer a question from one of the users of the channel his name is Casey West and this question kind of revolves around what exactly our employer is looking for when they ask you to provide sort of a code sample so let me kind of read out this question here first and then we'll kind of get into the details all right so Casey Wes says hey Brian I have followed you for quite some time now and taking your Instagram firebase course awesome by the way hello for paginations soon we'll get there and his question is in the second paragraph here I actually have a few different interviews coming up for iOS developer positions they have apps for a code sample and I was wondering what would you hope for what techniques you may look for if you were hiring I hope to hear from you I really appreciate you helping me reach my goal of becoming an iOS developer so Casey thank you very much for leaving this comment and to kind of give you an answer to your question a lot of different companies are kind of looking for different things when they're interviewing for developers right and basically when they're asking you to provide a code sample what they're really trying to do is to first check to see if you have clean code whether or not you follow best practices and also whether or not you follow like good coding conventions so basically today I have a project kind of prepared to kind of show you what I look for in terms of best practices and I'll go through the project with you guys right now all right so what I have prepared for you guys is this project inside of Xcode 9 beta and it's a very simple basic UI collection view controller app so I'm going to run this to demonstrate what exactly this simple application does and doesn't do very much and all its doing is renders out for collection of you cells in the red and then we have a couple of items inside of our navigation bar at the very top so again very basic and very simple just move it out of the way and I'm going to use this project to show you guys a couple of different things that I will consider best practices and good kind of coding conventions to follow so the first thing is what I have so I'll called bad variable names and right here we have bar X equals Harry Potter and let STR equal San Francisco California so why is this bad bad code basically when you are using variables such as X it's not very meaningful exactly what X means so instead of using X you should probably say something like either name or you know character name something like that now for San Francisco California this is another string so you should name this such this is something else such as location name or city name California state name makes a lot more sense when you're looking at it from kind of an outsider's perspective and you have a better understanding as to what the program does with more meaningful variable names so that's the first thing that I see way too often that I would hope people start to kind of learn to get away from all right so let's actually move on to the view controller code of our project and the second thing I want to talk about kind of revolves around long methods such as this one instead of viewed at load and when you have a lot of the tasks inside of one method you can usually separate them into two different methods to smaller methods and you'll see right here where we're setting up the navigation bar with the products title the sign out button on a lesson and the refresh button on the right and then on the bottom section right here we're setting up the collection view with the cells the headers and the background color of white and what we can do to make this a lot better is to separate this into a different method and inside of Xcode 9 you can do something called extract method which takes all that code and it puts it inside of this thing called extract it's funk and what you can do is to now rename this guy and you can call it perhaps setup nav bar and now viewdidload has this one function over here called setup nav bar which is all this code right here and you can do the same thing with this code but for whatever reason highlighting this section you can't use the extract method I don't know exactly why but I would instead create myself with file private functions set up collection view perhaps and I will cut all this code right here and put it inside of that something right there and then finally you can just call it with setup collection view and get the spacing correct and now viewed at load contains two smaller methods which is above right here and right here and your code is now a lot cleaner so let's move on to the third thing which is also something that you should be concerned about and inside of setup collection view we have this register method that registers these red cells for our collection view and it's using this raw string cell ID and then down here somewhere where it says cell for item and also DQ's that cell using that string the problem is if you change this perhaps to a lowercase I and then you run the application you'll notice that it just crashes because the string is different so let me just give you the cash right here so you see the crash and that's a major major problem that can be avoided if you just instead of using this raw string I'm going to change it back you can give the cell identifier a string perhaps like this cell ID equals cell ID and then you just use these cell ID instead of the raw string and this is what I do in most of my tutorials it's not all and this way it is very very easy to kind of just fix your codes it is for the most part it's kind of bulletproof down and that's the cell ID and you would do the same thing for the header ID so I'm just cut that and let header ID equals that perhaps and then you just use the header ID like so now you can change this to whatever you want and the program if you run it again it's not going to crash just because you change the strings characters so that's what you get you get your question view like that all right the next thing you want to kind of do and you really want to pay attention to is the actual cell classes that you might be registering perhaps for a collection view or a table view and the cell right here the cell class name is actually cells let me just jump to the definition and I see this a lot and it's kind of surprising at the same time because these cells should have a more meaningful name such as perhaps product cell right that's a more meaningful name and this should probably be product header or whatever you want to call it are moving on here take a look at in console and you'll see a lot of these print statements that say returning the number of cells in the section as well as a D queueing film and using it to rendering the list and basically it's here and here that were printing out these statements and I'd say that's way too often and basically the thing about this is that you should actually delete these print statements as you are getting more and more used to how I guess uikit works and basically when you have a lot of these unnecessary print statements you start to obscure the ones that are important and a print statement that you really need to pay attention to kind of it gets lost inside of this sea of useless information that's one really important thing that I guess developers senior developers look for okay so let me go ahead and try to move onto you something else that I see way too often unfortunately and I want to show you what it is by opening up this person dot Swift file right here and right off the bat a lot of you guys probably already noticed that this file right here follows the incorrect naming convention and this really needs to be a capital P like that and I'm not exactly sure why people don't follow this naming convention but I see this a lot you want to make sure the model object also starts off with a capital letter so make sure to do that and let's dive a little bit deeper into this person struct model and some people like to name their variable or properties for those trucks with all caps I'm not exactly sure why but it really needs to just be lowercase like that and follow camel casing is what we call it so what does camel casing look like well this job title is whole case but the problem is the first letter needs to be lowercase like that and sometimes else the job underscore title like this and this isn't the proper naming and mentioned that Swift follows so you know you want to make sure you just have lowercase as the first letter and then the capital letter for every word okay and the reason why you know how to follow that naming convention is if you go into perhaps the viewcontroller and jump to the definition so what is this gauge up to definition these properties in here collectionview is lowercase like that and then capital G like so alright so back to person one very subtle thing that you should notice is that this last variable age is a double okay so what does that really mean well the age can be specified and now as a hope they let P equals person like that with the name of perhaps myself and you tuber and the age you can specify ad out of 99.5 about 99 years old and a half and you can actually do that but for the most part when you guys are constructing these model objects and you're talking about a person's age you usually should use something like an integer instead and try to build this this no longer works because this needs to be an integer of 99 now what is the point of that right what difference does that really make well if you use a double the actual amount of memory that it takes to hold a double and memory is a lot larger than just a regular int I can't remember is 8 bytes or 64 bits and whatever but it's a lot more than what you need so you should actually just use an int because that's what's commonly referred to for age and finally let me go ahead and move on to something else that is probably considered more nitpicky and I want to take a look at these two braces right here and because apples conventions all of this race convention where the brace is actually on the same line as where the I guess need motto object is declared you want to follow this convention instead of this right here and the reason why this is important is because inside of even apples projects as they give demos the way that they write their code so let me just pull out this right here their augmented reality code they use that convention where the brace starts on that same line like that and like that other method names do the same thing if I can define the method name right here so this is the starting brace okay so the if statements that you write for example this right here if you know this equals world and let me just use it as a brace here I see a lot of people using this right here but it's my opinion that you should probably follow whatever Apple decides to use all right so that's going to wrap it up for today's video make sure to leave a comment down below and say what kind of best practices you guys follow and what kind of best practice is that I should be aware and don't forget to hit that like button if you enjoyed today's video and also subscribe to this channel if you haven't done so already in the next video I think I'll probably talk about what types of projects you should include inside your portfolio as you are applying for new jobs that's going to be a birthday thank you so much for watching and I'll see you in the next video bye