Archive for Month: October 2015

Patterns: Names as Documentation

While it’s usually less of a problem in C, in my Java days I saw any number of functions with names like “solve” or “act”. These functions were usually overloaded, so that “solve” meant one thing for integers and a wholly different thing for strings. Continue Reading…

Delayed Post

Post some time during the day (I had a nap).

A Hundred Posts

I’m traveling today, so I’ll save the content for tomorrow.

Hooray! I’ve been blogging for 100 full posts (actually, this is 103).

That’s more than the average blogger ever puts out, because it takes a degree of focus to make this happen.

Ah. Basking in the glow of success.


Okay, back to work.

Patterns: Return values should mean something

I don’t know how many hundreds of functions I’ve dealt with with either a void or boolean return type. While a boolean return type at least tells us whether the function ever completed properly, a void return carries absolutely no meaning. Where’s the debugging or error-handling value in that?


Constructor functions take a number of arguments and return a pointer to an allocated space. Even so, there is a simple rule for meaningful returns:

Constructors return either a newly allocated pointer or NULL

Of course, if you work with a slightly more complex constructor, you can return the pointer in a parameter. In these cases, you should still make the constructor return something meaningful.

Personally, in these cases, I use a tri-state return type. If the function is successful, I’ll return the size of the allocated space (in bytes). If it has an error, I’ll return a negative value correlating to the type or location of the failure. However, if the function works EXCEPT that the malloc does not successfully allocate any space, I’ll return 0.

Simple Access Functions

A simple access function returns some meaningful data value from a structure. In these cases, we return the value that we retrieved.

Anything that can possibly fail (even when it can’t)

If it’s possible for a function to fail, we return that value inside of a parameter and use the basic rule:

Return 0 on success and non-zero on error

This basic rule applies generally, across the board. Even when the operation has no chance of failure (a state which is less common as I construct better code), we return the 0 value.

Debugging and Logging errors

As systems grow in complexity, the number of locations where an operation could fail increases.

Further, as the size of a function increases, the number of ways it could fail increases.

When we employ meaningful return types, we create a path directly to the area where the problem occurred. So long as we know that function panda failed with an error value of -5, we know where the error was triggered (even if the system is thousands or millions of lines long). Even better, if we designed our return codes around specific tests, we know exactly how the function failed.

This means that, without ever touching the debugger, we have identified the location of our failure and can immediately begin determining the sequence of events that led to failure.

As Martha Stewart would say, “It’s a good thing”.

New Creatures United in Christ

Grace and Peace to you from God our Father and the Lord Jesus Christ.

Today, we will continue our reading of the book of Ephesians. In this chapter, Paul reveals to us the nature of our unity and the New Spirit that dwells in us.

Reading: Ephesians 4

I therefore, the prisoner of the Lord, beseech you that ye walk worthy of the vocation wherewith ye are called,

With all lowliness and meekness, with longsuffering, forbearing one another in love;

Endeavouring to keep the unity of the Spirit in the bond of peace.

As Christians, we are always called to be peace-makers. In the Love with which we are Loved, we extend that love to our brethren in Christ.

There is one body, and one Spirit, even as ye are called in one hope of your calling;

If a priest comes forth preaching a Spirit which we have not received or a Christ different from the Christ we know, he is not of the body. As The Father, the Son, and the Holy Ghost are one God, so is the body of Christ one body.

One Lord, one faith, one baptism,

This verse speaks to several things. First, we are united under One Lord Jesus Christ. We share in one faith by the Holy Spirit, and we are reborn in one baptism. What disunity can we have so long as we share all these things?

One God and Father of all, who is above all, and through all, and in you all.

The concept of “Aristotle’s God” asserts that there is a logical need for God. Some say it’s because God had to be there to start the universe, but more accurately it’s because everything we observe appears to rely on some other thing for its existence. Without gravity (or whatever force it is that performs the function of gravity), nothing would exist the way it does. Without a parent, no single organism could exist. Without logic, we could not have science or mathematics.

However, it is understood that infinity doesn’t exist in our observed natural world. The Infinite and Eternal are concepts that cannot be understood in logical terms, and are logically irrational. That means that, at some point in the chain, there must be something which “starts” the logical chain.

Think of it as a case of the chicken and the egg. It doesn’t matter which one came first, because one of them surely must have.

We Christians point to verses such as this and Acts 17:28 (“For in him we live, and move, and have our being; as certain also of your own poets have said, For we are also his offspring.”) to argue that our Lord is the logically necessary entity, which does not require any other thing for its existence. It is God whose properties provide existence to all things.

That’s a bit tangential, because this verse speaks directly to the Christians of Ephesus. God is always with us and sustaining us.

But unto every one of us is given grace according to the measure of the gift of Christ.

Because of the massiveness of the Gift of Christ, we know that the grace given to us must be equally massive.

Wherefore he saith, When he ascended up on high, he led captivity captive, and gave gifts unto men.

(Now that he ascended, what is it but that he also descended first into the lower parts of the earth?

10 He that descended is the same also that ascended up far above all heavens, that he might fill all things.)

We know that the disciples leaned on the Old Testament to support their teachings. Jesus himself was thoroughly versed in the Scriptures from a young age, such that the teachers were astounded by his knowledge. It’s important that we do no less when we speak.

The same Christ who is now ascended above all things descended down into death for us, so that in his resurrection He might grant to us gifts and free us from our slavery to sin.

11 And he gave some, apostles; and some, prophets; and some, evangelists; and some, pastors and teachers;

12 For the perfecting of the saints, for the work of the ministry, for the edifying of the body of Christ:

Unto each of us is granted a certain set of spiritual gifts. The Apostles were granted one set, which made them prophets, evangelists, teachers, and ministers all in one. Among us, we are given to be prophets, evangelists, teachers, ministers, and servants in the service of Christ. (Note that I do not speak of “worship leader” ministers, but those who provide comfort, guidance, and service to the brethren)

We must embrace and employ these gifts, because to do otherwise is to anger the giver of the gift. It is like the parable of the Talents; no matter how great or small your gift, you must nurture it and employ it towards God’s glory.

13 Till we all come in the unity of the faith, and of the knowledge of the Son of God, unto a perfect man, unto the measure of the stature of the fulness of Christ:

14 That we henceforth be no more children, tossed to and fro, and carried about with every wind of doctrine, by the sleight of men, and cunning craftiness, whereby they lie in wait to deceive;

15 But speaking the truth in love, may grow up into him in all things, which is the head, even Christ:

The mark of childishness is flightiness. Children run from thing to thing, for they lack the discipline and inner-strength required to “stick to it”. They are easily misled, for they lack the wisdom and awareness required to defeat clever cons.

We are more than children who are in Christ. We are granted stability in faith, which causes us to seek after the Lord in all things.

Let us speak truly amongst ourselves, out of the love that we share in Christ.

16 From whom the whole body fitly joined together and compacted by that which every joint supplieth, according to the effectual working in the measure of every part, maketh increase of the body unto the edifying of itself in love.

Apart, we are less than our fraction of the whole. What would we gain if we were all teachers, and none of us ministers? Or when we are all ministers, without Pastors or Prophets to guide our hands?

So, in the unity we share, we are to apply our gifts together in the works which God has prepared for us.

17 This I say therefore, and testify in the Lord, that ye henceforth walk not as other Gentiles walk, in the vanity of their mind,

18 Having the understanding darkened, being alienated from the life of God through the ignorance that is in them, because of the blindness of their heart:

19 Who being past feeling have given themselves over unto lasciviousness, to work all uncleanness with greediness.

What do we see in the Modern Humanist (Atheist or otherwise)? We see a great deal of vanity, for they fashion themselves the lords of creation, holding the keys of life and death. They are blinded in their hubris, so that their ignorance merely grows as time goes on. Their fruits are abortion, erosion of moral standards, destruction of the good, and exploitation of all. Their greed destroys them.

We see similar in all those who lack the love of Christ.

20 But ye have not so learned Christ;

21 If so be that ye have heard him, and have been taught by him, as the truth is in Jesus:

Fundamentally, because we know Christ and have been taught by Him, our minds are different. We are no longer like the other Gentiles.

22 That ye put off concerning the former conversation the old man, which is corrupt according to the deceitful lusts;

23 And be renewed in the spirit of your mind;

24 And that ye put on the new man, which after God is created in righteousness and true holiness.

The process of sanctification is long, for no matter how long we are alive we still are trapped in these bodies of sin. However, we are creatures with a New Spirit. We are different from the Old Man, now, and so we need not focus unduly on the old sinful ways.

By our renewal in Christ, we are new men. We are given the gift which makes us now righteous and holy.

25 Wherefore putting away lying, speak every man truth with his neighbour: for we are members one of another.

Because we are new men, we need no longer lie to one another, for we are brethren in Christ, and of one body.

26 Be ye angry, and sin not: let not the sun go down upon your wrath:

As new creatures, we should avoid the quick and violent anger of the old man. This is not to say that we can never be angry (because even Christ Himself was angry and wrathful in his time on this earth), but we must not dwell in our anger.

On another side of the coin, this verse demands that when we do grow angry, we must resolve that wrath swiftly.

27 Neither give place to the devil.

When we allow our minds to be filled with sinful ideas (of which the internet, television, and other media abound), we begin to dwell on those ideas. As we dwell, these thoughts sink into our subconscious minds, strengthening the wicked impulses natural to us.

Never give place to the devil.

28 Let him that stole steal no more: but rather let him labour, working with his hands the thing which is good, that he may have to give to him that needeth.

Our God is not communist or socialist. This is why he discourages theft, for theft discourages the laborer to labor and produces nothing else. However, giving generously (out of desire to give) is encouraging and empowering to the giver and receiver.

When more people produce, total value increases for the entire population. This means that there is more to go around.

Therefore, let us all produce according to our ability, so that we may prosper and share that prosperity with one another as we choose.

29 Let no corrupt communication proceed out of your mouth, but that which is good to the use of edifying, that it may minister grace unto the hearers.

Interestingly, this verse has nothing to do with “swear words”. More accurately, this verse encourages us to avoid thoughts and speech that will hold anyone back. We must speak the truth in love, working to teach and encourage one another to do good works.

30 And grieve not the holy Spirit of God, whereby ye are sealed unto the day of redemption.

Why would we want to grieve the Holy Spirit of God? Honestly, the new creature does not want to do any such thing.

However, it is in our physical nature to be sinful creatures. When we sin, we grieve the Holy Spirit who is in us.

31 Let all bitterness, and wrath, and anger, and clamour, and evil speaking, be put away from you, with all malice:

These emotions are natural to us, and to a degree we need anger to function in today’s world. However, these worldly emotions should never be turned toward our brethren while we coexist in unity with the Spirit.

32 And be ye kind one to another, tenderhearted, forgiving one another, even as God for Christ’s sake hath forgiven you.

Let us Pray

Mighty Lord, you have given us a Unity that we cannot fully understand. We pray for the day when all of us who are united in the Holy Spirit in the name of Jesus Christ are united again, without the superficial issues that divide us.

Bring us together in the Word.

In your Holy Name, we pray,



May the good Lord go before you to lead you, behind you to encourage you, beside you to befriend you, beneath you to uphold you, above you to protect you, and within you to inspire you.

Go in the peace and power of Almighty God. Amen.

Patterns: Protect the Iterators!

Some patterns are more obvious than others. This is one of the more obvious patterns, but it’s violated often enough to deserve enunciation.

Iterator Abuse

Just about every program out there uses an iterator for one reason or another. Without them, we can’t build for loops, and more than a few while loops require iterators as well.

They let us perform simple operations on buffers and arrays.

They let us control flow of information.

Fundamentally, an iterator is a control line. It is essential that we maintain the integrity of our controls, because without that control we exponentially increase the complexity of the problem.

Iterator Abuse is the act of violating the integrity of an iterator, which destroys the line of control and makes the program act in complicated or unexpected ways. This abuse is performed in a number of ways:

  • Reusing the iterator before its control function has been completed
  • Modifying the iterator inside of the loop (usually by a non-standardized unit)
  • Passing the iterator into a function without protection
  • Using your only copy of a pointer as an iterator
  • etc.

What can you do?

Iterator abuse is one of the more easily preventable issues in our programs. We have to adhere to a few simple rules:

  1. Only use specially-marked values as iterators (the classic name is “i” or “j”)
  2. Only use iterators for iteration
  3. Iterate each value only ONCE per cycle (no i++; i+=j)
  4. In cases where we want to modify the iterator by a non-standardized unit (for example, by a number of bits equivalent to a structure), use extreme caution
  5. If iterating inside of a for loop, never modify the iterator
  6. If iterating over a pointer, iterate over a copy of that pointer instead of the original
  7. Either avoid non-standard loop termination (break, etc.) or avoid referencing the iterator outside of the loop
  8. Don’t pass iterators into functions which might change the iterator value

Lesson: Iterators are oft-abused values. Remember that their purpose is to establish simple loops, and don’t go crazy with them.

Patterns: Object Oriented C Code

Most modern languages are designed around the object oriented design principles. They contain syntactic elements that codify and require these principles for code implementation.

Unfortunately, as is common with modern minds, we leave everything up to the computer. This results in a gigantic block of generalities, the result of which is slow code that is difficult to maintain. For most modern programmers, the choice is between efficient code and object oriented code.

However, we can apply the Object Oriented Design Principles to C code relatively easily. After all, the principles were originally theorized for use in languages like C.


We’ve already discussed one aspect of encapsulation (that is, the unitary nature of objects), but the principle also includes “data hiding”. In “proper” encapsulation, the structures are perfectly opaque to the end user, so that the only way to access the data is through carefully-constructed methods.

The techniques for constructing methods are already well understood, but how does one create hidden data in C?

Fun fact: If it’s not in the header file, the end-user can’t touch it.

Fun fact #2: We can use typedef to create pointers to objects in code files which do not themselves appear in the header.

Fun fact #3: While the computer can translate between the typedef pointer and a pointer to the original object at link time, the user’s code cannot.

Thus, if we define struct panda in a code file, but the header only contains methods and the following:

typedef struct panda *PANDA

the end user can only access the structure by passing PANDA to the access methods.

Abstraction and Polymorphism

We perform abstraction in only two ways:

  • Function pointers – used to look up objects
  • Hiding data with “typedef”

Function pointers are too complicated to go into now, but basically we can write functions that accept pointers to functions, the results of which produce data that the top-level function can work with. This allows us to create essentially polymorphic functions.

Because abstraction is relatively complicated in C, it encourages programmers to rely on patterns and techniques instead of function-overloading and “let the computer figure it out” mental patterns. That means we don’t employ template classes, interface classes, or abstract classes (which I would argue ultimately make programming much harder), but we can still create functional polymorphism and abstraction if we so choose.

Note: We do create “templates” in C. These are not OOP templates, but rather code which is generally applicable to a number of tasks. We can simply use these framework templates to produce new classes quickly.


What exactly does inheritance mean?

Functionally, inheritance ensures that one class contains as its first (and thus, most easily addressed) member another class. All the aspects of the parent class are carried into the child class, so that the child is merely an extended version of the parent. As such, all the parent functions should be able to work with the child class.

We can do the exact same thing in C using type casting.

We know that we can tell a C program to treat data elements as some other kind of data element. When we do this, C “maps out” the space that the casted element should occupy in the original element and treats that space as the cast element.

It looks something like this:


Sure enough, so long as we ensure that the “parent” structure is the first element in the “child” structure, we can use type casting to perform inheritance. It’s really that simple.

Lesson: Object Oriented Design principles were designed during the C era for use with languages such as C. All of the principles (at least, the ones that don’t cause performance issues) are possible in C.

Patterns: Code Completion using VI

Generally, I don’t much care for IDEs. In my experience, IDEs add increasingly specific features to maintain their relevance, resulting in bloated software which takes too many resources to accomplish a relatively simple task.

However, as functions get more complex, it becomes increasingly difficult to make your code match the original prototypes. This is where Vi’s “yank” and “mark” features truly shine.

Yank to Mark

We know that the vi equivalent to “copy-and-paste” is “yank-and-put”. What many people do not realize is that you can open any number of files in a single vi instance, allowing you to yank from one file and put into another.

The final piece of this puzzle is the “mark” feature. It is possible for us to create a mark on a given line with the following command:



We can then return to that line with the command:



This allows us to yank large sections of code from one place to another using the command:



This is the “yank to mark” feature.

Application 1: Code Completion

We can use the yank-to-mark feature to copy entire function prototypes back into our new code. To do this, we first navigate to the spot where our function call will go. We can then go to the header file containing the required function prototype and yank it out. Finally, we “put” the prototype into our new code, then swap the prototype values out for our new values.

For example, suppose we have a prototype that looks something like this in a file file.h:

int function(

int Value_one,

char *Value_two,

struct astructure Value_three


If we want to call this function in main.c, we can open vi like so:

vi main.c file.h

We then yank everything from “int function(” to “)” using our “yank-to-mark” feature and put it into main.c.

At that point, it is a simple matter to replace the return value and all parameters with the values that we should pass into the function, all while ensuring that the types match.

Application 2: Headers match Code

For many programmers, it is a pain to ensure that your headers and code files match exactly. This is easily achieved with yank, however.

We can copy all the code from the start to the end of the code file using the command:

gg (go to start)

yG (yank to end of file)

We can then put everything from the code file directly into the header.

We can delete all the code inside our function prototypes using the “go-to-matching” command. We can highlight the start of a code block (marked by the { brace) and hit the % key to go directly to its matching closing brace (}). Thus, we can delete all the code using the command:


Then, we can insert a semicolon to complete the function prototype.

Using this feature, we can create header files in seconds which EXACTLY match the code files to which they belong.

Note: If you’re using doxygen-style comments, you should write them directly into your code files. This way, when you update the function definitions in the code, you can perform another simple yank to replace the old prototype without having to adjust your comments again. It is a real time-saver and it allows you to see your comments wherever the function is declared.

Lesson: IDEs such as VI contain powerful tools while remaining minimally invasive. Learn to leverage these simple tools to speed up development.

What You See Is All You Get (WYSIAYG)

I’ve spent a number of years refining my arguments against modern object oriented programming languages. Continue Reading…

Patterns: Always compile with debug tags

99 little bugs in the code

99 little bugs in the code

Take one down, patch it around

117 little bugs in the code

-Alex Shchepetilnikov, Twitter Jul 2013 Continue Reading…

Facebook Auto Publish Powered By :