Archive

Posts Tagged ‘programming’

Conscious Degradation Of Style

December 18, 2011 6 comments

One (of many) of my pet peeves can be called “Conscious Degradation Of Style” (CDOS). CDOS is what happens when a programmer purposely disregards the style in an existing code base while making changes/additions to it. It’s even more annoying when the style mismatch is pointed out to the perpetrator and he/she continues to pee all over the code to mark his/her territory. Sheesh.

When I have to make “local” changes to an existing code base, if I can discern some kind of consistent style within the code (which may be a challenge in itself!), I consciously and mightily try to preserve that style when I make the changes – no matter how much my ego may “disagree” with the naming, formatting, idiomatic, and implementation patterns in the code. That freakin’ makes (dollars and) sense, no?

What about you, what do you do?

James YAGNI

December 15, 2011 5 comments

In the software world, YAGNI is one of many cutely named heuristics from the agile community that stands for “don’t waste time/money putting that feature/functionality in the code base NOW cuz odds are that You Ain’t Gonna Need It!”. It’s good advice, but it’s often violated innocently, or when the programmer doesn’t agree with the YAGNI admonisher for legitimate or “political” reasons.

When a team finds out downstream and (more importantly) admits that there’s lots of stuff in the code base that can be removed because of YAGNI violations, there are two options to choose from:

  • Spend the time/money and incur the risk of breakage by extricating and removing the YAGNI features
  • Leave the unused code in there to save time/money and eschew making the code base slimmer and, thus, easier to maintain.

It’s not often perfectly clear what option should be taken, but BD00 speculates (he always speculates) that the second option is selected way more often than the first. What’s your opinion?

An Array Of Vectors Of Tuples

November 25, 2011 1 comment

Yepp, that’s what I concocted recently to implement an algorithm. Pretty fugly, no?

If I told you what the algorithm is, I’d have to dispatch a missile carrying drone to seek out and kill you.

The Expense Of Defense

November 19, 2011 3 comments

The following “borrowed” snippet from a recent Steve Vinoski QCon talk describes the well worn technique of defensive programming:

Steve is right, no? He goes on to deftly point out the expenses of defensive programming:

Steve is right on the money again, no?

Erlang and the (utterly misnamed)  Open Telecom Platform (OTP) were designed to obviate the need for the defensive programming “idiom” style of error detection and handling. The following Erlang/OTP features force programmers to address the unglamorous error detection/handling aspect of a design up front, instead of at the tail end of the project where it’s often considered a nuisance and a second class citizen:

Even in applications where error detection and handling is taken seriously upfront (safety-critical and high availability apps), the time to architect, design, code, and test the equivalent “homegrown” capabilities can equal or exceed the time to develop the app’s “fun” functionality. That is, unless you use Erlang.

TDD Overhype

November 8, 2011 Leave a comment

There is much to like about unit-level testing and its extreme cousin, Test Driven Design (TDD). However, like with any tool/technique/methodology, beware of overhyping by snake oil salesman.

Cédric Beust is the author of the book, “Next Generation Java Testing“. He is also the founder and lead developer of TestNG, the most widely used Java testing framework outside of JUnit. In a Dr. Dobb’s guest post titled “Breaking Away From The Unit Test Group Think”, I found it refreshing that a renowned unit testing expert, Mr. Beust, wrote about the down side of the current obsession with unit testing:

  • Unit tests are a luxury. Sometimes, you can afford a luxury; and sometimes, you should just wait for a better time and focus on something more essential — such as writing a functional test.
  • There are two specific excesses regarding code coverage: focusing on an arbitrary percentage and adding useless tests.
  • TDD code also tends to focus on the very small picture: relentlessly testing individual methods instead of thinking in terms of classes and how they interact with each other. This goal is further crippled by another precept called YAGNI, (You Aren’t Going to Need It), which suggests not preparing code for features that you don’t immediately need.
  • Obsessing over numbers in the absence of context leads developers to write tests for trivial pieces of their code in order to increase this percentage with very little benefit to the end user (for example, testing getters).

Being a designer and developer of real-time, multi-threaded code that runs 24 X 7, I’ve found that unit testing is not nearly as cost effective as functional testing. As soon as I can, I get a skeletal instance of the (non-client-server) system that I’m writing up and running and I pump deterministic, canned data streams through the system from end-to-end to find out how the beast is behaving.  As I add functionality to the code base, I rerun the data streams through the system again and again and I:

  • try to verify that the new functionality inserted into the threading structure works as expected
  • try to ensure that threads don’t die,
  • try to verify that there are no deadlocks or data races in the quagmire

If someone can show me how unit testing helps with these issues, I’m all ears. No blow hard pontificating, please.

Admittance Of Imperfection

November 4, 2011 2 comments

One of the traits I admire most about Bjarne Stroustrup is that he’s not afraid to look “imperfect” and point out C++’s weaknesses in certain application contexts. In his “Technical Report On C++ Performance“, Mr. Stroustrup helpfully cautions real time programmers to think about the risks of using the unpredictable/undeterministic features of the language:

Of course, C++ antagonists are quick to jump right on his back when Bjarne does admit to C++’s imperfections in specific contexts: “see, I told you C++ sux!“.

Here’s a challenge for programmers who read this inane blog. Can you please direct me to similar “warning” clauses written by other language designers? I’m sure there are many out there. I just haven’t stumbled across one yet.

Persistent Discomfort

November 3, 2011 Leave a comment

As part of the infrastructure of the distributed, multi-process, multi-threaded system that my team is developing, a parameterized, mutex protected, inter-thread message queue class has been written and dropped into a general purpose library. To unburden application component developers from having to do it, the library-based queue class manages a reusable pool of message buffers that functionally “flow” from one thread to the next.

On the “push” side of the queue, usage is as follows:

  • Thread acquires a handle to the next empty Message buffer
  • Thread fills Message buffer
  • Thread returns handle to the queue (push)

On the “pop” side of the queue, usage is as follows:

  • Thread acquires a handle to the next full Message buffer (pop)
  • Thread processes the Message content
  • Thread returns handle to the queue

So far, so good, right? I thought so too – at the beginning of the project. But as I’ve moved forward during the development of my application component, I’ve been experiencing a growing and persistent discomfort. D’oh!

Using the figure below, I’m gonna share the cause of my “inner thread” discomfort with you.

In order to functionally process an input message and propagate it forward, the inner thread must do the following work:

  • Acquire a handle to the next input Message buffer from queue 1 (pop)
  • Acquire a handle to the next empty output Message buffer from queue 2
  • Utilize the content of the Message from queue 1 to compute/fill in the Message to queue 2
  • Return the handle of the input message to queue 1
  • Return the handle of the output message to queue 2 (push)

For small messages and/or when the messages are of different types, I don’t see much wrong with this inter-thread message passing approach. However, when the messages are big and of the same type, my discomfort surfaces. In this case (as we shall see), the “utilize” bullet amounts to an unnecessary copy. The more “inner” threads there are in the pipeline, the more performance degradation there is from unnecessary copies.

So, how can the copies be eliminated and system performance increased? One way, as the figure below shows, is to move message buffer management responsibility out of the local queue class and into a global, shared message pool class.

In this memory-less queue design, the two pipeline end point threads explicitly assume the responsibility of acquiring and releasing the Message buffer handles from the mutex protected, shared message pool. The first thread “acquires” and the last thread “releases” message buffer handles. Each inner thread, i, in the pipeline performs the following work:

  • Pop the handle to the next input Message buffer from queue i-1
  • Process the message
  • Push the Message buffer handle to queue i

The key to avoiding unessential inner thread copies is that the messages must be intentionally designed to be of the same type.

As soon as I get some schedule breathing room (which may be never), I’m gonna refactor my application infrastructure design and rewrite the code to implement the memoryless queue + global message pool approach. That is, unless someone points out a fatal flaw in my reasoning and/or presents a superior inter-thread message communication pattern.

Shocking!

October 28, 2011 1 comment

In the brilliant YouTube video, “An Introduction to Erlang (for Python programmers) “, the narrator presents the following facts about Erlang that are shocking to “normal” programmers trained in object-oriented languages:

So, why would anyone want to learn Erlang? Because….

In my case, the fact that concurrency, distribution, fault-tolerance, and hot-swapping are built in to the language make me a huge Erlang fan. On the downside, I’m not a fan of “managed” languages.  However, Erlang’s positives far outweigh its negatives. What do you think?

Awhile ago, I started a focused effort to learn how to program idiomatically in Erlang. However, because:

1 I’m a slooow learner.

2 C++/Java are the languages that my company’s products are written in.

3 ……

I’ve abandoned  the “deep dive” into the language. However, I’m definitely still watching from the periphery because my gut tells me that Erlang will continue its rise into prominence.

Codan The Barbarian

October 16, 2011 Leave a comment

In the Indigo release of Eclipse‘s C++ Development Tools (CDT) plugin, the Codan static code analyzer runs in real time as you type in your code. As shown below, you can customize the rule set that Codan enforces via the “Preferences/C++/Code Analysis” dialog window.  (My fave is the “ambiguous problem” entry.)

The figure below shows a few examples of Codan in action. While typing in code, a gold (warning) or red (error) bug icon appears adjacent to the line number of the crappy code you write.

Some of Codan’s warnings and errors are also detected by good compilers, but it’s kind of neat that you can discover and correct your defects before running the compiler/linker. This feature is a boon for large programs that take a while to compile and link.

As a long time developer, I’m thrilled to death to have open source tools like Eclipse available to dolts like BD00. I remember the old days when there were not many commercial tools available, yet alone high caliber, open source tool suites like Eclipse.

Different Goals And Unfair Comparisons

October 13, 2011 Leave a comment

At the behest of a work colleague, I revisited the Java programming language. I re-read James Gosling‘s 1995 white paper that introduced the language and I perused several Java-related wikipedia articles. Using those sources and Bjarne Stroustrup‘s “Evolving a language in and for the real world: C++ 1991-2006” paper, I developed the following side by side “design goals” lists:

In spite of these radically different design goals, people (including myself) continue to insist on comparing the languages in passionately irrational ways. If both language designs were driven by a common set of goals, then objectively valid comparisons could be made.

Java was intentionally designed from a blank sheet; unfettered from the ground up. Au contraire, C++ was intentionally designed by extending C and attempting to fix it’s short comings without breaking millions of lines of deployed code. It’s like comparing apples to oranges, dontcha think?

Categories: C++ Tags: , , , ,