loading...

Write code the right way

rexben001 profile image Rexben ・3 min read

Image

Source:https://www.pexels.com/photo/man-in-white-shirt-using-macbook-pro-52608/

As I will always say, “Programming is one of the greatest inventions of man. It’s helped to solve a lot of problems, bridge gaps, surmount barriers”. “The whole essence of programming is to solve problems”. To think that there are no problems around you that needs a solution is to think wrongly.

Apart from writing codes, it is best to write a healthy code. How can you identify a healthy code, right?

For your code to be healthy, it must pass through these three tests. The tests include semantic tests, syntax tests, and pragmatic tests.

SYNTAX TEST

The syntax of a computer language is the set of rules that defines the combinations of symbols that are considered to be a correctly structured document or fragment in that language. It can be said to be a set of rules that are needed to be followed to have a working code. If you don’t follow the syntax of a particular programming language, you would always get a syntax error.

In order to fix a syntax error, the best thing to do is to read the documentation of that programming language.

The syntax of a programming language A is different from programming language B. The rules that govern the English Language is different from that of French or German or Chinese, same with programming languages.

For instance, print Hello world in JavaScript is different from Python and Elixir.

To print “Hello world”:

Python ==> print(“Hello world”)

JavaScript ==> console.log(“Hello world”);

Elixir ==> IO.puts “Hello World”

So, for your code to be healthy, it must first and foremost pass the syntax test.

SEMANTIC TEST
In programming language theory, semantics is the field concerned with the rigorous mathematical study of the meaning of programming languages. It does so by evaluating the meaning of syntactically valid strings defined by a specific programming language, showing the computation involved.

In a nutshell, semantics is concerned with the meaning of the code. Is it doing what you want it to do? You don’t know if your code is semantically correct until you compile or interpret the code with a compiler or interpreter respectively. 

Your code can be syntactically correct and be semantically correct. When you run or test your code, it might not raise any syntax error and it might still not give you the right output.

For instance, you wanna collect two inputs from the user and add them up

Python

a = input(Enter first value: )  
b = input(Enter second value: )  
c = a + b  
print(The sum total of, a, and, b, is, c)

This code above won’t give a syntax error but it is not giving out the right output.

If a = 10 and b = 5, the output will be 105 instead of 15.

So, for your code to be healthy, it must pass the syntax test and semantic test.

PRAGMATIC TEST 

Pragmatism often refer to writing clean code and managing the code in a manner as efficient as possible, to make it easily understandable, by the person that writes the code, and by the people that will read the code in the future.

So, your code might be both syntactic and semantically correct but not pragmatically correct. Does it follow best practices?

Some of the best practices include avoid duplication of codes, put into consideration the Single Responsibility Principle, use well descriptive names when naming variables, classes, methods, interfaces, add comments to your code, it must be properly indented, etc.

Example

Python

a = int(input(Enter first value: ))  
b = int(input(Enter second value: ))  
c = a + b  
print(The sum total of, a, and, b, is, c)

To make the code pragmatically correct

def sum():

 firstVal = int(input(Enter first value: ))  
 secondVal = int(input(Enter second value: )) 

 total = firstVal + secondVal  
 print(The sum total of, firstVal, and, secondVal, is, total)  
sum()

For a code to be healthy, it must be syntactically (follow rules), semantically (do what it’s meant to do) and pragmatically (follow best practices) correct.

Posted on by:

rexben001 profile

Rexben

@rexben001

Experienced Software Engineer with a demonstrated history of building software solutions. Knowledgeable in building full-stack applications and using VCS.

Discussion

markdown guide