Introduction to Abject-Oriented Programming

Abject-oriented programming is a set of practices for encouraging code reuse and making sure programmers are producing code that can be used in production for a long time. The number of lines of code in the application is a common measure of the importance of the application, and the number of lines a programmer can produce in a day, week, or month is a useful metric for project planning and resource allocation. Abject-oriented programming is one of the best ways to get the most lines of code in the shortest time.

abject: utterly hopeless, miserable, humiliating, or wretched: abject poverty.

Inheritance
Inheritance is a way to retain features of old code in newer code. The programmer derives from an existing function or block of code by making a copy of the code, then making changes to the copy. The derived code is often specialized by adding features not implemented in the original. In this way the old code is retained but the new code inherits from it.

Programs that use inheritance are characterized by similar blocks of code with small differences appearing throughout the source. Another sign of inheritance is static members: variables and code that are not directly referenced or used, but serve to maintain a link to the original base or parent code.

A pseudo-code example of inheritance:

function getCustName(custID)
{
    custRec = readFromDB("customer", custID);
    fullname = custRec[1] + ' ' + custRec[2];
    return fullname;
}

function getCustEmail(custID)
{
    custRec = readFromDatabase("customer", custID);
    fullname = custRec[1] + ' ' + custRec[2];
    /***************
    * 4/15/96 git : email address stored in
    * second fax field
    ***************/
    return custRec[17];
}

The function getCustEmail was inherited from getCustName when email addresses were added to the application. Inheriting code in this way leverages working code with less risk of introducing bugs.

Subtyping is a kind of inheritance where the variable types are changed when inheriting from the original code.




Modularity
A modular program is one that is divided into separate files that share a common header comment block. A module usually consists of:

  • Copyright notice
  • Legal disclaimers
  • Three to five lines of asterisks
  • Change history
  • Description of what the module was originally supposed to do
  • Three to five more lines of asterisks
  • Big block of whitespace surrounded by asterisks or other character, including the name of each function or subroutine, the author’s name or initials, and the date originally written
  • The code

Modules are usually kept to a reasonable size to reduce coupling and improve module strength. If a module gets too big divide it into smaller pieces, copying the copyright notice, legal disclaimers, etc. from the original. Comments can always safely be inherited when deriving one module from another so it’s safest to copy all of the original comments.

Components and Libraries
Abject-oriented programming lends itself to using plug-in components — bits of code found in books or on the Internet. Using a search engine a smart programmer can save time by finding pre-made software components to do almost anything. The best components are black boxes: the programmer doesn’t know or care how the component works. Many large applications are built with a combination of inheritance from other applications and components gleaned from the web.

Encapsulation
The idea behind encapsulation is to keep the data separate from the code. This is sometimes called data hiding, but the data is not really hidden, just protected inside another layer of code. For example, it’s not a good practice to scatter database lookups all over the place. An abject practice is to wrap or hide the database in functions or subroutines, thereby encapsulating the database. In the getCustName function above the database is not queried directly — a function is called to read the database record. All getCustName and getCustEmail (and the many other functions like them) “know” is where in the customer record to find the bits of data they need. How the customer record is read is encapsulated in some other module.

Some programming languages make the programmer declare variables as private or public or protected. That is not an abject practice, though. There’s no way for the author of a module to know which internal variables of their module may be needed to implement features in the future. Programmers should make all variables public (or global), and let the rest of the code decide what should and shouldn’t be private.
Polymorphism
When learning abject-oriented techniques, programmers frequently get stuck on polymorphism. It sounds hard but the idea is simple and easy to implement. Code is polymorphic when it gives different outputs for different kinds of inputs.

To give an example, the functions above can be rewritten as a single polymorphic function by inheriting the code that already works and then encapsulating it into a new function:

function getCustData(custId, what)
{
    if (what == 'name') {
        custRec = readFromDB("customer", custId);
        fullname = custRec[1] + ' ' + custRec[2];
        return fullname;
    } else if (what == 'email') {
        custRec = readFromDB("customer", custId);
        fullname = custRec[1] + ' ' + custRec[2];
        /***************
        * 4/15/96 git : email address stored in
        * second fax field
        ***************/
        return custRec[17];
    }

    /* ... etc. */
}

Polymorphism is related to the idea of non-determinism and Turing’s finite state machines, which you may remember from your Comp Sci classes.

Is-A vs. Has-A
This is a subtlety of good abject-oriented design. When first learning abject principles programmers tend to do everything with inheritance (the is-a model). With more experience programmers find that the has-a relationship is often more appropriate. In the code example above, every customer has a name, but custRec is a database record.

Virtual Classes and Functions
A virtual class or function is code that the application will eventually need, but it isn’t written yet. This is commonly accomplished by providing a base class that the final code will be based on:

function calcSalesTax(price, isTaxable, state)
{
    /****************************************
    *
    * TO DO:
    *
    * get tax rate for the customer state
    * eventually from some table
    *
    *
    ****************************************/

    /** 02/07/99 git -- use WA rate for now **/
    return price * (7.25 / 100.0);
}

A fragile base class is a class or module that has been in the application for a long time, and any change to it will break the rest of the application.

Overloading
Overloading is when a module or chunk of code does more than one thing. An example would be a subroutine to get a customer’s name, email address, and state sales tax rate. Using overloaded functions cuts down on method dispatching, which is one of the reasons other programming styles can result in slow code.

Documentation
It’s said that code should be written for people to read, so it follows that documentation is written for no one to read. Documentation should be written for every new module and then maintained as changes are put into production, or at least the next time there’s a lull in the workload.

A good time to write documentation is when someone in the department gives two-weeks notice: use that time to make sure the departing team member documents all of their code.

Cluttering up the source code with lots of comments explaining what the code is trying to do is distracting and slows down the compiler. That’s why abject shops that follow “best practices” keep documentation in a document management system where programmers can’t accidentally delete it.

Version Control
Not really a programming practice per se, but abject shops tend to follow similar version control practices. Keeping previous versions of code around, and tracking changes to code, is important even if only one programmer works on the application. Experienced abject programmers follow a system similar to this:

  • Always add your initials and the date of the last revision to the source file’s header.
  • When you are editing a file and realize that your changes are big enough to make reverting hard, save a copy with a .bak extension.
  • Keep multiple backups around by appending your name or initials and the date to the backup file name: custdata_git20040321.bak.
  • Always store backups in the same directory or folder as the original code, to make it easier to see each file’s history.

Conclusion
You’ll probably find that most established shops already follow some or all of these abject-oriented practices. Fads like agile and “extreme” programming come and go, but the abject style has stood the test of time. Managers are familiar with abject practices and will expect you to be able to work with their abject-oriented code base.

28 comments ↓

#1 Floyd on 06.14.07 at 2:10 pm

I thought overloading was the practice of making all variables global so that they can be reused for a different purpose elsewhere in the program. Or is that garbage collection?

#2 admin on 06.14.07 at 8:26 pm

Thanks for all the great comments. I’ve learned a few things today!

@Dave: Database design is a big subject that I would like to write about someday.

@Chetan: Thanks for mentioning the commenting out technique. That’s called making a method protected.

@Zach: I assume you are joking about subversion — this is a technical discussion, not about management.

@Onur Yalazi: Sorry, we’re not using Perl.

@A: One of my colleagues pointed out that overloading is when data is passed to a function or module in globals because too many function parameters overload the compiler. My mistake! And @Floyd — when variables are kept in globals there’s no need for garbage collection.

@Mark: I’m not familiar with the technique of making code explain itself. Is that the same as printing status messages from subroutines, like print “got here!” so you can tell where the crash happened? If that’s what you mean that is commonly done in AOP, and not just with C#.

@Mysingen: I worked at a place with an “expert consultant” who did a lot of code generation with awk and sed and “shell scripts.” Unfortunately he left before documenting his work but it still works most of the time.

@Ron: GREAT idea about the directories.

@booboo: Please no ad-hominy attacks, everyone has different opinions on programming. That’s why some programmers use Visual Basic and others use VBScript — it’s all about choice.

@Dumbo: I read somewhere (probably MSDN?) that GOTO is much faster than a loop or IF statement.

#3 John on 06.15.07 at 9:10 am

Wow – there are a lot of inaccuracies / bad practices put forth in this article. Make all variables global? Btw- it’s ad hominem – not ad hominey. I think once you’ve spent more time in an object oriented language you’ll get a better idea of concepts like overloading, encapsulation, inheritance – etc.

#4 Wow on 06.15.07 at 10:40 am

What the author has found by writing this article is the fact that a lot of people calling themselves professional developers don’t know anything about programming. If you can’t see the humor in this then you need to seriously go back to the books and just stay there while you are at it. He actually points fun at the people who made comments above by saying “Abject-oriented programming lends itself to using plug-in components — bits of code found in books or on the Internet.” too funny

#5 Andrew E on 06.16.07 at 9:30 am

I laughed and laughed at everything…until I got to Version Control. Then I encountered great shame.

#6 Steve on 06.16.07 at 9:44 am

It’s funny how many people are taking this seriously. Any “real world” programming department and any company is guilty of any or all of the above. If we can’t laugh at ourselves then who CAN we laugh at?

#7 Graham on 06.16.07 at 10:15 am

I have to object to the “Document the code” part. If someone is just leaving the company now, why would you get them to document at that point. In my experience if the code isn’t documented when its written, it’s garbage. How long did this person work with the code for? 2 weeks is likely inadequate. Document when you code – just answer “why” you did it that way, and we’re good to go.

#8 Anton on 06.16.07 at 11:35 am

Guys, this article is the biggest piece of crap I’ve every seen. No offense, but seriously. Make all members public? Comment out code? These are things I used to do when I just got out of college. Over time, you see why people tell to avoid these. I love the part about making code reusable. “Duuhhh” is the word that comes to mind. How is this different from what programmers have been trying to achieve since the first computer program took its first breath. If there was an easy way of having every piece of code be always easy to reuse, believe you me, it would have been a standard practice and would have been taught to the author of this redundency as a part of the curriculum of the school he seem to have recently graduated from. Alas, there isnt a simple way. Software is complicated. Some pieces can be made reusable, others cannot. Others can be made reusable, but are not because of various timeline and budget constraints imposed on programmers by the business types.

I say, let’s not beat the proverbial dead horse to the pulp, and let’s concentrate on figuring out how to apply what’s proven itself over time. And if someone with actual experience can suggest something really new, let her back it up with actual examples that proove why her way is better. Claiming that making all variables public is the way to go because you were able to hack code quicker is not what most of us look for when we spend our precious weekend time reading programming articles.

#9 John Bokma on 06.16.07 at 12:48 pm

Really good article.

What worries me though, is that some people take it very serious…

#10 A on 06.16.07 at 12:50 pm

You guys must be kidding. AO is against almost all the things I have ever learned about programming!!

You sure you guys are not kidding?

#11 Henk on 06.16.07 at 1:19 pm

It was a good laugh until I read the comments and realized that most of the people didn’t get the joke :S

I have to confess, tough: I have made some nasty Polymorphistic functions in my time :D

#12 F on 06.16.07 at 1:58 pm

I don’t know what’s the most funny; the article or all the people not understanding the jokes in it.

#13 Lance Walton on 07.05.07 at 3:11 am

This is a work of satirical genius. However, as I read it, I got a horrible feeling that some people wouldn’t understand that it was meant to be humour and might take it as advice. When I read the comments, I see that the first part of my fear was real. I suspect the second part will be as well…

#14 Ton on 07.24.07 at 7:48 am

Abject-oriented programming sounds fantastic – why have I not heard of it before? I’ve been telling all my teammates about it, and we’re going to try it on our next project. Luckily, many of its proposals are quite close to practices we already follow. It seems our team has been doing a lot of things right – our management will be delighted to hear this!

#15 pragma on 08.03.07 at 12:00 pm

Ton, is your company hiring? I have been teaching some 20-odd students all about Abject-Oriented, imprinting it in their heads this perfect way of life. With these professionally-educated students, you could excel like never before!

;)

#16 Technical Related Notes » Blog Archive » links for 2007-06-17 on 08.18.07 at 10:04 am

[…] Introduction to Abject-Oriented Programming — Typical Programmer (tags: programming software) […]

#17 Introduction to Abject-Oriented Programming « Programming News on 10.04.07 at 4:43 am

[…] read more | digg story […]

#18 Zustandsforschung » Top 10 Liste für besseren Code on 10.15.07 at 5:28 am

[…] Introduction to Abject-Oriented Programming […]

#19 links for 2007-12-12 « Ashish Kulkarni’s Journal on 12.12.07 at 11:18 am

[…] Introduction to Abject-Oriented Programming Abject-oriented programming is a set of practices for encouraging code reuse and making sure programmers are producing code that can be used in production for a long time … NOT! (tags: programming humor) […]

#20 programming » Blog Archive » Introduction to Abject-Oriented Programming on 05.07.08 at 11:01 am

[…] read more | digg story […]

#21 Introduction to Abject-Oriented Programming on 06.17.08 at 8:46 am

[…] http://typicalprogrammer.com/?p=8  […]

#22 Cargo Cult Coding and Abject-Oriented Programming on 07.17.08 at 12:34 pm

[…] I stumbled across a really nice article the other day: Introduction to Abject-Oriented Programming […]

#23 Abject Oriented Programming | Programming Is Fun on 10.25.08 at 2:40 pm

[…] Oriented Programming Posted Today by joelschultz abject: utterly hopeless, miserable, humiliating, or wretched: abject poverty. Share and […]

#24 Simon’s Work Blog » Abject Oriented Programming on 02.21.09 at 3:28 am

[…] You can read the full article here. […]

#25 Twitter Trackbacks for Typical Programmer - Introduction to Abject-Oriented Programming [typicalprogrammer.com] on Topsy.com on 08.28.09 at 10:45 pm

[…] Typical Programmer – Introduction to Abject-Oriented Programming typicalprogrammer.com/?p=8 – view page – cached Abject-oriented programming is a set of practices for encouraging code reuse and making sure programmers are producing code that can be used in production for a long time. The number of lines of code in the application is a common measure of the importance of the application, and the number of lines a programmer can produce in a day, week, or month is a useful metric for project planning and resource allocation. Abject-oriented programming is one of the best ways to get the most lines of code in the shortest time. — From the page […]

#26 Lo Chan (fishtron) 's status on Friday, 18-Sep-09 23:05:26 UTC - Identi.ca on 09.18.09 at 4:05 pm

[…] to Abject-Oriented Programming" http://typicalprogrammer.com/?p=8 (via […]

#27 Typical Programmer - How to develop unmaintainable software on 10.14.13 at 3:44 am

[…] I’m always surprised to find big projects written in the last few years that aren’t under source code version control. When you don’t use version control the next programmer to come along has to figure out which files are part of the current system and which are obsolete or backups. The next programmer won’t have any commit messages or change logs to get the code’s history from. I covered how not to use version control in my article Introduction to Abject-Oriented Programming. […]

#28 如何开发不可维护的软件? | 爱低俗|俗是一种态度 on 10.24.13 at 10:51 pm

[…] 我经常很惊讶的发现,在过去的几年里编写的大型软件工程竟然不在源代码版本控制中。如果你不使用版本控制,下一个承接你项目的程序员就没法确定哪些文件属于你当前的系统,哪些是淘汰的以及哪些是备份文件,他也无法从提交信息或者修改日志中获得任何关于代码的历史信息。我在我的文章《面向卑鄙的编程介绍 | Introduction to Abject-Oriented Programming》中,介绍了“如何不使用版本控制”。 […]