CSC 204 Lecture 1 Principles of Good Programming
CSC 204 Lecture 1 Principles of Good Programming
It sounds a little harsh, but it's one of the most important principles to adopt
when you’re writing computer code. What does KISS mean?
It means you should be writing code as simple as possible. One of the rules
of basic programming is to never get caught up in trying to be overly clever
or showing off with a thick block of advanced code. If you can write a script
in one line, write it in one line.
Pretty simple. It's easy to read, and you know exactly what is going on.
One programming principle in this spirit is to use clear variable names.
Take advantage of coding libraries and use existing tools. Make it easy to
come back after six months and get right back to work. Keeping things
simple will save you so much needless suffering down the line.
function addNumberSequence(number) {
number = number + 1;
number = number + 2;
number = number + 3;
number = number + 4;
number = number + 5;
return number;
}
Instead of duplicating lines, try to find an algorithm that uses a loop instead.
DRY code is easy to maintain. It's easier to debug one loop that handles 50
repetitions than 50 blocks of code that handle one repetition each.
3. Open/Closed
This principle of programming means that you should aim to make your
code open to extension but closed to modification. It ensures that you
create code that doesn't need to be modified even when requirements
change. This is an important principle when releasing a library or
framework that others will use.
For example, suppose you're maintaining a GUI framework. You could
release a version for coders to modify and integrate your released code
directly. What happens when you release a major update four months later,
though?
Their code will break. This will likely make your cohorts very unhappy. They
won't want to use your library for much longer, no matter how helpful it may
have been in its heyday.
5. Single Responsibility
Classes and modules often start off this way. Be careful not to add too
many responsibilities as classes get more complicated. Refactor and break
them up into smaller classes and modules.
For example, the code that handles the database doesn't need to know
how to render the data in the browser. The rendering code takes input from
the user, but the logic code handles the processing. Each piece of code is
completely independent.
The result is code that is easy to debug. If you ever need to rewrite the
rendering code, you can do so without worrying about how the data gets
saved or the logic gets processed.
This principle means you should never code for functionality on the off
chance that you may need something in the future. One of the most
important principles of computer programming to learn is that you shouldn't
try to solve a problem that doesn't exist.
Only apply DRY programming principles when you need to; if you notice
chunks of code written over and over, implement a layer of abstraction.
Don't think too far ahead at the expense of your current code batch.
8. Document Your Code
With all of this talk about the principles of coding, it can be easy to forget
about the human on the other side who may eventually be getting into your
code themselves.
Any senior developer will stress the importance of documenting your code
with proper comments. All languages offer them; you should make it a habit
to write them. Leave comments to explain objects, enhance variable
definitions, and make functions easier to understand.
Here's a JavaScript function with comments guiding you through the code:
else {
return number + 5;
}
}
Leaving comments is a little more work while you're coding. It takes time
and steals your attention away from the real work at hand. You understand
your code pretty well anyway, right? Who cares? It's worth remembering
that nothing is disposable, even in the world of tech. What is a computer
programming principle at the end of the day if the person on the other side
ends up getting lost?
We recommend going the extra mile and leaving comments anywhere you
worry that things will become murky or unclear, especially when
collaborating with others. Don't frustrate your fellow developers by forcing
them to decipher your syntax.
Try writing a program, leaving it alone for six months, and returning to
modify it. You'll be glad you documented your program instead of having to
pour over every function to remember how it works.
9. Refactor
It's hard to accept, but your code isn't going to be perfect the first time.
Refactoring code means reviewing your code and looking for ways to
optimize it, making it more efficient while keeping the results exactly the
same. This is a consideration for writing cleaner and quality code.
It doesn't mean you didn't succeed the first time you wrote your program;
you're inevitably going to get more familiar with a project over time. Use
that knowledge to adjust yourself as you make progress.
Aside from all the fundamental programming principles, leave your ego at
the door and forget about writing clever code. When we say this, we mean
the kind of code that looks more like a riddle than a solution. You're not
coding to impress strangers. You're in this profession to solve problems.
Don't try to pack a ton of logic into one line. Leave clear instructions in your
comments and documentation. If your code is easy to read, it will also
usually be easy to maintain.