Coding to perfection

Being a developer you see a lot of code and in my persute to be the best, I look to perfect my code. When you want to be the best you need to not only have the best code, but it also need to look the best and be functional. There are certain things you need to do when coding to perfection to make it the best for not just yourself, but for others to read as well. You have to bare in mind that other people will be judging you on your code for a job maybe, and then also using your code which you want to be easy read for them.

So one part would be making you code condensed, so you don’t have functions that will be repeated over and over. The other thing people do is blot the code by having a lot of IF ELSE statements. My thought is always, if you have to use it more that once then make it into a reusable function. This just saves time copying code. You can make functions take a load of parameters and with some languages make them optional, so if the two uses you need it for aren’t parallel in what you need, you can make it optional. The only problem is when do you stop? You can make things as dynamic as you like, but there has to come a point when the function is so loosely made to what it needs to do, that you can’t understand it and could break any time.

//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js

(adsbygoogle = window.adsbygoogle || []).push({});

When making your variables and even id’s, you need to make logical names. When you code, you code for others to read it, especially if you are in a team. So the names of the variable need to be obvious for the person reading it to know what it is used for. Say it is for a title, then you could call it PageTitle. This is then straight away easy to see what it is for and even what type of variable it is. I always like to also camel case my variables as you can see. This is because I think if you don’t, then the word blurs into one word e.g. pagetitle or PageTitle. Some other would do it like page_title, which I feel is also ok to use. Even though you may need to be specific with you variable like the last example, you may need to make them a bit open, just in case you need to use them again for another thing. For example, things like HTML classes, you may have a class for a title of the about section. Now using a class called ‘AboutTitle’ may seem the best idea, as it tells the person reading it that it’s the class for the about title, but if you can re-use the CSS for that class then the best option is to call it PageTitle. This then still tells the person that it’s the CSS for the page title, but offers you the choose to use it on other titles.

When I have done some tutorials and courses, one thing that does get picked up is style. This isn’t a necessary thing to do, but the small things make a difference and if you are coding for perfection then you need to go by it. I am a .NET Web Developer, and so you may notice I reference to web coding, but the concentus applies to all languages. For style it is all about keeping things consistent. There is no 100% proper way to display your code, as it all comes down to how you code and what is readable  to you. The general theme though is what most people would keep to, like a unwritten standard. Here are some of the things to keep in mind.

Classes and IDs

When writing the classes and ids for elements, not only should you write them with logically names, but I also keep them to their own uses. For classes I use them for CSS, so if you need to know the style of the element then you can use its class or it’s parents class to search the style sheet. Then for ids, I use them for any server side uses. So if I have a function that uses an id you can get the id and search it on either side. This reduces the amount of time spent searching the class and getting nowhere then have using the id and also confusion.

Indents and next lines

This is best for readability, to next line elements and indent them. When you next line each element, you can then see when the next element starts, instead of having to read all across the line counting the number of openings and closes. This is then made even easier by indenting the elements. When you have an element and then inside another element, if you indent the inner one then you can see that  it is inside the other element easily. This can then be repeated as the more elements you put in, so you can then tell what is in where and where they begin/end.

//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js

(adsbygoogle = window.adsbygoogle || []).push({});

Example:

     

            Example

     

Commenting

fundamental part in all languages and programming applications, is to comment your code. When you are coding your script you know how it works, but someone else may look at it and think what the hell!?. So when you go through you code you should comment what does what and what the variable are getting passes. A good trick in .NET is with functions to put in C#, /// . This then auto generates a summary field so you can put in what it does, plus it also produces tags for the variable and if it’s a function then a field for what it should return. This is then shown when you hover over that functions name in another page, so you can then see what it is doing. Some times it is also good to do in the the visual side as well like HTML as if you have some complicated amount of it, you may want to give direction as to what it all is.

Organisation

A minor one, but then every little helps. I know some times you can’t help it in web developing as some functions for build order need to be in different places, but others you can. You should give some order to your code, like how you comment to give direction, you can also place your code in sections so you can see where you would have to look. For example in the front end of a web page, you would put all you JavaScript/JQuery in the Head and then the HTML in the body. This can then even be broken down so in the Head, you have you meta information, then your imports, then you JS. This then gives structure and again for someone reading you code, they can refer to what they need in an instant. The same context can be given to the back end of code and other pure scripting languages. You put your global variables declared at the top, then your OnLoad function underneath, finally put other functions in a decent order.

Theses are all the kind of practices I try to do when coding, which I would call coding to perfection. As it is then logical, functional and readable for anyone. Doing these simple things, that you would get into routine with anyway, would improve speed of coding, reading code and even the speed of your application. If any of you think there are other great techniques then please tell me as I would relish the chance to make my code even better.

Advertisements

Leave a message please

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s