5 Critical Coding Concepts That Will Move You From A Beginner To An Intermediate Programmer

5 Critical Coding Concepts That Will Move You From A Beginner To An Intermediate Programmer

Programming is more than “hello world,” and Fibonacci

I learned to program when I was 10 years old, first simple websites with HTML and CSS and Minecraft altering programs with Java. When I started studying computer science, I underestimated the curriculum of year two after a year of simple programming concepts.

Programming is much more than simple programs to calculate your GPA or the total amount of coins that Jack has earned on his holiday job.

Instead of learning 17 different programming languages and 15 convenient frameworks, software engineers should be taking a good look at the core concepts of programming.

“If you can’t deal with these concepts you probably need to find another profession.“ —My OOP Teacher

1. Object-oriented programming

The first concept on your path towards a professional programmer is object-oriented programming, usually indicated as OOP. This is the first thing we got taught after we understood the basic concepts of programming.

OOP is a model that organizes your programming around data and objects. So in this type of programming, we’ve got objects that basically are the model of the data and the instances of that object.

An example of an object could be a Student, and a student has attributes like an ID, a name, an age, and perhaps a GPA.

public class Student () {
}

In the code written above, we’ve created the most simple object without any attributes. You can achieve this in any language, from Javascript to Python.

With OOP, we can create subclasses, so let’s transform this Student class into a more general Person class.

public class Person {
   public Person(){
   }
}
// The code above generates a 'Person' class and constructs it.
// Now lets use the 'Person' to create a student
public class Student extends Person {
  super();
  public Student () {
  }
}

Using the ‘extends’ keyword, the ‘Student’ class inherits everything from the ‘Person’ class by using ‘super.’ To create an instance of a ‘Student,’ we use the following syntax:

Student newStudent = new Student();

And that’s the most basic use of OOP.

2. Functional Programming

Functional Programming is an important step into becoming an all-around programmer. With Functional Programming, we build software and write code purely based on functions.

So in comparison to OOP, there are no possibilities to share data with other blocks of code. However, everything available in the function is accessible.

Functional Programming is mostly used in mathematical operations, and by only working with functions, your program is a lot faster than when working with objects.

An example of Functional Programming:

#include <stdio.h> 

int multiply(int x, int y);     

int main() {    
   int result; 
   result = multiply(12, 2);         
   printf("result = %d",result); 
   return 0; 
}  
int multiply (int x,int y) {         
   int result; 
   result = x * y; 
   return result;           
}

This code is a purely function-based program that multiplies the entered numbers represented by x and y.

3. The Singleton Design Pattern

Now that you know two of the most important coding concepts: FP & OOP, we can dive a little deeper, and then we arrive at the design patterns.

A design pattern or pattern in computer science is a generic software structure that solves a common type of software design problem. The pattern does not provide a concrete solution but provides a template that can be used to tackle the design problem.

The singleton pattern is one of the first patterns you learn with computer science. This pattern is used to create an object most simply.

Let’s give you an example in Java:

public class SingletonObject {
  private static SingletonObject singleton_object = new   SingletonObject();


  private SingletonObject(){
  }


  public static SingleObject getSingletonObject(){
      return singleton_object;
   }


  public void TestMethod() {

    System.out.println("Test Example"):
  }



}

Now, what’s happening in our singleton class? At first, we create an instance of the singleton object, making the constructor private.

In the getSingletonObject() the method we return this instance and with the TestMethod() we can test it.

public class SingletonTest {
   public static void main(String[] args) {
      SingletonObject object = SingletonObject.getSingletonObject();
      object.TestMethod();
   }
}

This outputs:

$  Test Example

4. The MVC Design Pattern

MVC (Model — View — Controller) is a very commonly used concept in Object-oriented programming. This pattern contains 3 parts:

  • The Model (Which represents the data)
  • The View (Which outputs the data)
  • The Controller (Which controls the data)

Let’s retake the Person example, and we need to create three classes.

public class Person {
   private String name;

   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }
}

public class PersonView {
   public void printPersonDetails(String personName){
      System.out.println("Person: ");
      System.out.println("Name: " + personName);
   }
}

public class PersonController {
   private Person model;
   private PersonView view;

   public PersonController(Person model, PersonView view){
      this.model = model;
      this.view = view;
   }

   public void setPersonName(String name){
      model.setName(name);        
   }

   public String getPersonName(){
      return model.getName();        
   }


   public void updateView(){                
      view.printPersonDetails(model.getName());
   }    
}

With this pattern, you create a full working program, but it’s very structured by classes. Usually, we separate the classes into separate files and packages.

5. The Data Access Object Pattern

The Data Access Object Pattern is very commonly used when we are creating Web Server API. When you work with a Database, this ‘driver’ can be beneficial.

Again, this pattern consists of three parts:

  • The Data Access Object Interface
  • The Data Access Object Class
  • The Model Object

Let’s take a look at this Person Model Object Class:

public class Person {
   private String name;

   Person(String name){
      this.name = name;
   }

   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }

}

Nothing too special about that. More important is the Object Interface:

import java.util.List;

public interface PersonDao {
   public List<Person> getAllPeople();
   public Person getPerson(String name);
   public void updatePerson(Person person);
   public void deletePerson(Person person);
}

The next class is a bit of work, and we need to implement the methods:

import java.util.ArrayList;
import java.util.List;

public class PersonDaoImplementation implements PersonDao {

   List<Person> people;

   public PersonDaoImplementation(){
      people = new ArrayList<Person>();
      Person person1 = new Person("Bryan");
      people.add(person1);
   }

   @Override
   public void deletePerson(Person person) {
      people.remove(person.getName());
      System.out.println("Person: Name " + person.getName() + ", deleted from database");
   }

   @Override
   public List<Person> getAllPeople() {
      return people;
   }

   @Override
   public Person getPerson(String name) {
      return people.get(name);
   }

   @Override
   public void updatePerson(Person person) {
      people.get(person.getName()).setName(person.getName());
      System.out.println("Person: Name " + person.getName() + ", updated in the database");
   }
}

Now, you can use the implementation class to manipulate the data and retrieve it.

Originally Published on Medium