Skip to content
Learnearn.uk » A Level Computer Science Home » Efficient Code

Efficient Code

Intro

Writing  Efficient Code

Efficiency in coding is very important. It allows you to speed up your development time and makes your code easier to read, debug and maintain. There are a number of different ways in which you can make your code more efficient, including:

  • Use of loops for repeated actions
  • Use of data structures instead of separate variables
  • Use of compound data structures
  • Use of functions & procedures
  • Use of in-built features / external code libraries
  • Use of recursion
  • Use of object orientated coding.
  • Separation of data from the code

Loops

Loops

Pretty much the first efficiency technique any beginner programmer learns is how to use a loop to improve efficiency.

  • You can use Unconditional loops(e.g. while loops) to repeatedly ask a question until a valid response is inputted.
  • You can use Iteration (for loops) to perform the same(or similar) action for a number of different items in a list.

 

Structures

Data Structures

Data structures allow the user to store multiple pieces of the same type of data using one single identifier name, rather than through multiple variables.

  • They work with loops making repetition more simple to perform.
  • The are quicker and easier to debug/maintain.

Compound Data Structures

Compound data structures, including 2 Dimensional arrays  and dictionaries within dictionaries, allow for more efficient, maintainable and extendable code than separate arrays.

 

Functions

Functions & Procedures

Functions and procedures make your code more efficient because:

  • They allow repeated use throughout the file (and in order files)
  • They make your code more readable
  • Can be called within other functions making them even more powerful.
  • Functions allow the editing of the internal workings of a function without affect other  logic outside of the function.

 

Libraries

External Libraries

Making use of external modules and optional downloadable modules allows for far more efficient coding, especially in larger projects.

  • You can implement functionality without reinventing the wheel.
  • You can use features that you don’t know how to implement
  • If an update of bug fix is rolled out, all users of the module can benefit from the improvements.

Of course there are risks associated with use external sources of a code, including:

You don’t know what the code is doing – it could be doing something malicious or malfunctioning in some way.
You can’t update the library yourself if there is an issue – you have to wait for a fix to be implemented.
Some libraries have a large storage /memory / CPU overhead in comparison to custom code implementations.

 

Recursion

Recursion

Some programming problems can only be solved , or be solved more efficiently through the use of recursion, where a function calls itself, for example:

  • Walking through directory trees
  • Implementing Stacks
  • Binary search
  • Divide and conquer sorting algorithms, such as merge sort.

For more information see the page on recursion.

 

OOP

Object Orientated Programming

In some situations, such as when you are trying to model real life objects or environments, then using object orientated programming techniques can massively improve the efficiency of your code. You code can become far more readable and adaptable.

 

Separation

Separation of data from code

When writing programs it is often beneficial to store data in a separate file, rather than in the program itself. This means that if the data needs to be changed then it can be done quickly and without risking damaging the code base. Using external data storage also makings implementing multilingual programs much easier, as when the program is loaded/installed a separate language file can be specified.