Coyote Rental Solutions

The client experienced first hand the difficulties of the renting process. As a potential tenant, he found it extremely difficult to convince his future landlord to trust him. It seems that everyone…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Design Principles in Programming

Layers of Abstraction

Abstraction is the idea of hiding the implementation details of some kind of functionality. Basically the success of one method executing should not be dependent on the success of another method executing. You assume each method you’re using did its job properly.

The function buildCar() is not worried about each build function’s implementation. It will assume they all work, and build a car object using those functions. The perks of using abstraction it avoids code duplication, and it helps with code maintenance.

An example of abstraction in Ruby is the different methods for enumerables (find, select, map, etc.). Another example of abstraction that’s used in your everyday lives is the Internet Protocol Stack, which is how computer networks work. You can Google the OSI Model for more information on this topic.

Code Reusability

This topic ties in with abstraction in many ways. The idea here is to avoid code duplication, just like in abstraction. If you often find yourself repeating the same code over and over again, there’s probably a better way to do it. For example, if you’re dealing with file input/output a lot in your program, it’s probably a good idea to create a class whose sole responsibility is to deal with files. Now, whenever you have to operate on a file you have all of the methods already defined in another class, letting you work on the more exciting parts of your program.

Code Readability

Pretty straightforward section. You want your code to be readable. If another person is reading your code, they should be able to figure out what it’s doing without too much effort. Making your code easy to read will also help you debug in the future because you don’t have to waste time understanding what you wrote. Additionally, adding comments can never hurt. You’ll thank yourself later.

Code Efficiency / Runtime Complexity

In my opinion, having efficient code, next to security, is one of the most important principles to be aware of as a programmer.

Nested loops and nested if statements is generally something you want to avoid. In the above example, the outer loop executes 10 times. Then the inside loop executes 5 times, but there’s a problem there. Each time the outer loop executes the inner loop executes 5 times. These code executions add up. In total, the inner loop will execute 50 times in addition to the 10 times the outer loop executes. On top of that, the inner loop has an if statement that evaluates every time the inner loop executes. In the end you have 70 evaluations the computer has to handle for this small bit of code. Imagine what would happen if you kept increasing 10 to 1000, or to 100,000 and beyond.

If you want to learn more about runtime efficiency, a good place to start is with sorting algorithms and recursion. You can gain a solid understanding on this topic through some basic research.

Bad Code:

“Good” Code:

Add a comment

Related posts:

Moped Insurance for my 16yr old son?

I am looking for moped insurance for my son. I am going to be paying for it and i would like to do it monthly. Does anyone know of some cheap insurers? And also should i insure it in my name or his…

Test Driven Development

Test-driven development (TDD) adalah sebuah proses pengembangan perangkat lunak yang berpacu pada pengulangan siklus pengembangan yang singkat. Persyaratan-persyaratan dari client diubah menjadi…

10 Things To Remember When Going Through Tough Times

My name is Mo Seetubtim. I’m the founder & CEO of The Happiness Planner. We design beautiful inspirational planning & journaling tools that help you find happiness from within and live a truly…