Writing nice code

2023-09-02 — Jahan Rashidi

One of the most important things when writing code is its readability. If it's easy to understand, it'll be easy to maintain and improve. Here are some tips for writing nice code.


Descriptive names
This is an obvious one, but a lot of people prioritize keeping the names of their variables and functions short. Sure, stoi may be fast to write, but it is infinity easier to guess what stringToInt will do instead. Unless extremely well known, abbreviations should be avoided; and also comment in the full name if you do choose to use them.


Comments
Something that tends to get drilled in during programming classes (at least the ones I've taken) is to extensively comment your code. This is wrong. You should write your code so that its meaning speaks for itself. Of course, commenting wont hurt, but make sure to not use it as a crutch.

Comments do still have their place for whenever the action a piece of code does is hard to guess. Commenting in the names of algorithms you use is always a good idea, and I like to place links describing them in comments as well. You should also link to any code you've copied from somewhere else; first off to credit them, but also as it will allow people to see the bigger context around a piece of code if it was taken from somewhere, which can be of use.

Documenting code is something you should be doing as well, especially for things that will be accessed from other namespaces or classes. Most code editors will automatically display comments written before definitions when their names are hovered. Document what the function does, what it'll return, and how it should be used.


Nesting
Try to avoid having your code nest to deeply, as it makes it a lot harder to read. One way I like to do this is to utilize break statements in if... else's like this:
void doingStuff()
{
 if(something){
  ...
  return;
 }
 ..else code..
}

Granted, this is a somewhat uncommon circumstance, but I always try to look out for opportunities like this.

Another way is to split large chunks of code into multiple functions. This is turn also makes it easier to understand since names will be assigned to the different parts. For example,
void petAnimals(Animals animals){
 if(animals.dog != undefined){
  for(int i = 0; i < floor(random() * 100); i++){
   animals.dog.pet();
  }
 }

 if(animals.cat != undefined){...

could be better written as

void petAnimals(Animals animals){
 petRandomTimes(animals.dog);
 petRandomTimes(animals.cat);
}

private static const int MAX_PETS = 100;
void petRandomTimes(Animal animal){
 if(animal == undefined) break;

 for(int i = 0; i < floor(random() * MAX_PETS); i++){
  animal.pet();
 }
}
This isn't the best example, but it demonstrates the idea.


Stick to the standard
Most programming languages have standards for styling code, like what case to use, if open brackets should be on a new line, etc. It's best to stick to these standards as it allows others versed in the language to read it better. It shouldn't be too much of an issue if you deviate it, but at least stick to something - eg. if you start writing your variables in camel case, stick to camel case.


Keep it concise
Don't try to code golf your programs, but do avoid writing unnecessary code. If the result of something is only going to be used once, avoid assigning it to a variable. Especially important, don't write the near-same line of code over and over. Always utilize a loop over copy and paste.

If you find yourself copy and pasting entire functions, split it into multiple functions. That way the parts that are the same between them can be shared between them.


Line breaks are your friend
In most programming languages, line breaks are ignored by the language. A good idea is to split long statements over multiple lines as to make it easier to read. A general rule of thumb is that no line of code should pass 70 characters (this limit is usually increased when coding in LISP, due to the heavy nesting).

Also, if two pieces of code do something separate, add a line break between them like you would a paragraph. They call it a programming language for a reason.


Don't do extra
If a function says it will add two numbers, don't have it assassinate the president as well. Functions should stick true to their names and not do anything unexpected by someone who doesn't know the code. If it does do something extra, add it to the name. int addAndAssassinate(int a, int b) may take a bit longer to write, but it will avoid any unexpected things to happen.