Coding styles and the style choices for your project

Coding styles are a contentious discussion point amongst programmers and your style choices will affect your entire team.

As “the project” grows, 2018-09-24 saw the introduction of the library structure which maximises reusability in any platform.

The output of today’s work were the initial libraries and a working engine to record suggestion ideas.

https://philipalacey.com/project/suggestion-data.php

 

A perfect time to go over the style discussion as the choices made now affect the project ongoing.

This article outlines the setup of the library and table structures which enables developers to write one liners and draw upon massive capabilities.

  • Style
  • Naming
  • Libraries
  • Wrap up

 

[read more=”Read more” less=”Read less”]

Style

Coding styles

Coding styles are a very subjective discussion amongst programmers and can be quite contentious.

Remember that this dicussions is for the programmers not the front end users.  The code will be in the background.

When you go to work for a company you should ask them for their coding style guide so you can learn to write in the company’s style.

So with no right or wrong answers where are there choices?

 

Fonts

When you write a document, which font do you use.  Some prefer Times New Roman, some Arial, yet for some none of the above are appropriate.

Programmers are helped by things that line up, so you can spot patterns and identify mistakes in the code easier.

For this reason, fixed width / monospace fonts such as Courier, Courier New, Lucida Console or Consolas are available.  Which font you choose for your development environment makes no difference to the code.

In the same way a dark background for your coding environment or a light background is down to your personal preference.

Yet for your design team the font and colours you choose for your users to interact with your solution will make a big difference.

 

Good code vs. readable code vs. commented code

There is no denying that Linus Torvalds is an extremely talented programmer.  His solutions are tidy, elegant and work. He gave the world Linux for free!!

The speed with which he produces solutions and his general approach to coding is to just get it done.  Something to be admired.

Yet for all that his social skills seem to rub people up the wrong way.  What follows next is a long video but explains an approach from one of the smartest programmers to have ever lived.

Linus here is in a room of some of the most talented programmers of Google and the introduction, before Linus makes the stage, in the first few seconds speaks volumes.

So in the discussion of coding styles, in his opinion, good clean working code is better than readable code.

If the code is clear, then it shouldn’t need comments, reminders or explanations embedded in the code.  Yet not all programmers are made the same.

In my opinion mere mortals need reminders, prompts and a bit of help to stop us making mistakes.

For this reason I include a lot of commenting and use a lot of techniques to explain what Linus would definitely describe as redundant or wasteful.

He’s right.  Yet I’m the one who has to troubleshoot / fix my own code if it goes wrong and I may need a little help to speed up the process.

 

White space

A bit of a misnomer in terms of coding styles.  White space refers to spacing and alignment within the code itself.

White space can make code easier on the eye stylistically but it is a choice to include it or not.

I personally use white space to make sure it’s easier to track to though long blocks of code, purely for visual aesthetics.

One example argument for indenting is do you use 4 spaces or a single tab character?

 

Naming

Camels

Camels, underscores and dashes

There is naming of lots of things in programming.  Variables, functions, objects, tables, etc. all need names.  The choice is a stylistic one and becomes a habit for programmers.

Let’s pick a long name for something.  “This is a name”  Here are three variations for programmers.

  • CamelCase : ThisIsAName
  • Underscores :  This_is_a_name
  • Dashes : This-is-a-name

When space is a premium and every letter counts, camelcase is the clear winner.  Hence the hash tags on twitter depend on this choice.

 

Bactarian camels at work

Image from http://bit.ly/2MXxxr5

CamelCase was invented by the Swedish chemist Jöns Jacob Berzelius in 1813.  NaCl instead of Sodium Chloride!

In certain areas a dash isn’t an option as the – represents the minus in subtraction, so is best avoided for safety.  Space and tab are not options as they are used by every programming language.

if ( thisLooksAppealing ){
    youLikeCamelCase = true;
}
else if ( this_looks_appealing ) {
    you_like_underscores = true;
}

In my opinion there are times to use one and times to use the other but I don’t expect any programmer to agree.

Whatever choice you make, your team is going to have to consistently agree with you.

There has been research into the area which proved equally inconclusive as to what’s the best choice.

 

Hungarian Notation

Variables

One of the most contentious discussions in coding styles.

Image from http://bit.ly/2IaRVV3

Invented by Charles Simonyi, a programmer who worked at Xerox PARC circa 1972–1981, and who later became Chief Architect at Microsoft.

The notation is a reference to Simonyi’s nation of origin

 

A variable is like an imaginary box you put things into.  Imagine a plastic bag and an envelope.

When you are programming a variable can store anything but to remind you what kind of things go in this box can be helped by Hungarian Notiation.

 

Hungarian Notation is a process where programmers in traditionally included the type of variable as part of the name of the variable.

More importantly when someone else is reading your code it’s easier to figure out what should be in there.

 

Variable types

VB Script and PHP variables have no type at all.  Actually they are of type variant, which doesn’t really help.  Anything at all can go in the box.

Languages like C, C++ and so on require you specifically declare what’s going into the variable.

Without Hungarian Notation difficulties arise with adding a number and string, which might be a programming mistake.  To compensate for this you can use a style approach to make life easier.

 

Including a three letter code at the start of the variable name dictates the contents.  There are generally only a few types

  • str  = string variable (numbers and letters)
  • int = integer or number variable
  • dte = date time variable
  • ary = an array of data
  • obj = object

So strTest would be a test variable that held a string.

When I first started programming this approach was a REQUIREMENT of the course.  Yet now it’s considered redundant by programmers and lecturers alike.

For me, your chosen coding styles should include Hungarian Notation but that’s my opinion.

 

Table names

Database tables

When you create a database table, let’s say User.  You then add columns to it.  Id, Firstname, Lastname, Email.

Now when you have a second table, let’s say SocialMedia you may also have Id, AccountType, AccountName, UserId.

Already you can see why the naming conventions of using CamelCase or Hungarian Notation need to be considered.

 

Select statements on two tables

Databases also present another problem.  In the SQL language no matter the database you may need information from these two tables at the same time.

The following query

Select Id, Id from User, SocialMedia Where Id = UserId

Already you can see the challenges and this query wouldn’t work.  So you have to extend the query to make it clear.

Select u.Id, sm.Id from User as u, SocialMedia as sm Where u.Id = sm.UserId

Depending on how under pressure you are to get things done this might let to mistakes and mistake are very bad.

So using two techniques stylistically I stop myself making mistakes.  I include the table name into the column names.

Select intUser_Id, intSocialMedia_Id from User, SocialMedia Where intUser_Id = intSocialMedia_UserId

It removes the ambiguity, stops the extra dot notation and makes it very clear which Id variables belongs to which thing.

Again, this is a coding styles choice to make reading the code easier and also to improve the chances of not making a mistake in the first place.

 

Libraries

Libraries are like having an expert in the room.  You can “include” a library and all the functionality contained within becomes instantly available to you.

Libraries are the key to resusability.

However over time your libraries mount up as you have more and more functionality available.

 

I’m going to start with a bit of Hungarian Notation and CamelCase for the functions anyway… fn_FunctionName

Let’s say you have 200 libraries included, which one has that function?

 

Development environments such as Visual Studio Code have a “search all” function to just search every library…

What if two libraries use the same name but have never been included together on the same page.

 

So in the discussion of coding styles, why not give ourselves an easier out.

Like the approach of table names the approach I use is to include the library name in the function.  As a result I know which library to go hunting in to find it.

fn_General_FunctionName and fn_Db_FunctionName now help you out as they give you an idea which library to go hunting in.  Also no fear of double naming.

 

 

Wrap up

Coding styles are the choice of the first developer.  So if that’s you, your choices will have far reaching consequences.

Teams need to follow the first laid out approach, so before you choose and approach make sure you’ve considered your options.  Producing your own coding style guide will help your team understand the code you want produced.

I rebuilt the first page demo using libraries today and the lines on the page went from 150 (first image) to 66 (second image) and in my opinion far easier to read.

 

With a bit of work and tweaking I have the libraries and a few functions working and we now have the ability to log ideas into the database.

Total time 7 hours but a lot of new library constructs available.

https://philipalacey.com/project/suggestion-data.php

 

If there’s anything in this article you’d like to chat to me about you can contact me here or on social media.

[/read]

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.