Live data from Hacker News

Ask HN: Have you ever inherited a codebase nobody on the team could understand?

news.ycombinator.com

211–220 of 222 posts

Re: Ask HN: Have you ever inherited a codebase nobody on the team could understand?

#211
I inherited a large database that "supported" an application, when in fact the application layer was built into the database. I took over from a begrudging dev who was involuntarily transferred from dev/dba to just dev. In reviewing the hundreds of ETL jobs, I found one that started off as SQL that invoked visual basic script in an external file share, which at some point invoked a small obfuscated machine code script located on yet a different external file share. Googling didn't tell me what the machine code actually did. The disgruntled dev said that if I wasn't smart enough to figure it out then I should quit. He had put this mess together with the idea that it would be job security. Finally, his boss forced him to admit that the machine code stripped a text field of spaces. The job was re-written in straight SQL and ran much faster.

Re: Ask HN: Have you ever inherited a codebase nobody on the team could understand?

#212
post #196

Earlier quoted context omitted.

> Rename variables EXACTLY what they are. I have this coworker who has this terrible habit of giving the most meaningless names imaginable. Local variables are often just named "tmp". Or maybe tmpNum if it is a number. We are writing a program to implement various tasks as background threads, and the classes have names like "Process01", "Process02", etc. At one place, he declared five or six constants with SQL querie…

Naming things correctly and consistently is at least 50% of good coding, and at 90% of programmers fail at it.

Well, you know what they say: There are two hard problems in programming, cache invalidation, naming things, and off-by-one errors[1]. ;-)

But there still is a difference between trying to come up with good names and failing on the one hand, and naming a variable "tmp" to indicate that it is a local variable, because I totally cannot see that when looking at the code...

[1] I do have to admit that I totally love foreach-loops, when I was still coding in C, I ran into / caused my fair share of off-by-one errors, they were nightmares to figure out.

Re: Ask HN: Have you ever inherited a codebase nobody on the team could understand?

#213
post #41

Earlier quoted context omitted.

This sounds like a great opportunity to just go build the thing they needed then offer it back to them as a service or for a licensing fee. Then you have the option to offer it to other companies as well.

This is usually quite risky and difficult. You might not have access to the data sources, you might not have the context to interpret them in a meaningful, and then the company you are targeting is probably your only potential customer (or you have to cold sell to every competitor, so now you have a sales job, right?)

Also, after a project fails the business is always close to 0% likely to work with you on it, even if it isn't your fault (I was brought in as a clean-up man and my billing was insignificant).

Re: Ask HN: Have you ever inherited a codebase nobody on the team could understand?

#214
I inherited a code base that other people thought they understood. People have very different tolerance levels for complexity and lack of control. People here complain about bad coders but the incentives out there are all aligned against quality: short stints and glorified short-term-thinking (a.k.a. Agile) and proprietary code. Imagine if you were bound to work on a codebase for most of your career, were given all the time you need to do your best and had all your code in the open, forever part of your reputation. Would you write better code?

Re: Ask HN: Have you ever inherited a codebase nobody on the team could understand?

#215

I’m a consultant and make a living saving bad projects. That’s literally why I get phone calls for work. Keep in mind that I work in high level modern languages, I’m sure there’s some crazy proprietary cpu running a robot in a Detroit factory. In any case there’s never been something that I’ve run into that I’ve not figured out. It takes time, and the hard part usually is not figuring out what it does but the weird e…

That's what I've been doing for the past 7 or so years. Turning around struggling products, gradually improving and then sometimes help a re-launch as "greenfield based on lessons learned" when the time is right. It's not the most sexy work in the early stages but very rewarding when you help turn a failing situation around. Sometimes it's bugs and bad algorithms or data structures. Sometimes it's misunderstood requi…

> It's not the most sexy work in the early stages but very rewarding when you help turn a failing situation around.

And most often that struggling but working product still brings money and real value to people or business, not like most of greenfield shiny start-up..[cough!].. throw-away projects.

Re: Ask HN: Have you ever inherited a codebase nobody on the team could understand?

#216
post #100

I know of a very popular London startup who's entire database is in Spanish due to the initial dev work being outsourced to a development company in Spain. All table, column, and procedure names are in Spanish. A refactor is too risky and they are growing too fast, so all the engineers have to pick up basic... programmer Spanish? That's a more literal example of not being able to understand the codebase I guess.

One of my first dev jobs years ago was at a non-technical company that had outsourced some previous development work, and the resulting HTML templates had all classes and styles in Spanish. The first "encabezamiento" class name (Spanish for "header") threw me for a loop, but luckily the patterns were similar enough!

Re: Ask HN: Have you ever inherited a codebase nobody on the team could understand?

#217
Org-mode and literate programming.

Bring all the code into a set of org-mode files each file has one code block, which emits back the original code. You can verify this using diff or (better) putting the initial repo into git if it's not already and seeing if your emitted code causes any unstaged changes to appear. Start dividing the blocks into more logical chunks (includes, declarations, definitions, one block per function implementation, etc.). Use a hierarchy like:

  * foo.c
  #+BEGIN_SOURCE c :tangle foo.c :noweb tangle
    >
    
    >
  #+END_SOURCE
  ** includes
  #+NAME: foo-includes
  #+BEGIN_SOURCE c
    #include
    #include "bar.h"
    #include "foo.h"
  #+END_SOURCE
  ** file variable declarations
  #+NAME: foo-file-variables
  #+BEGIN_SOURCE c
    int x, y, z;
    some_struct a, b, c;
  #+END_SOURCE
  ** Function bodies
  #+NAME: foo-functions
  #+BEGIN_SOURCE c :noweb tangle
    >
    >
  #+END_SOURCE
  *** baz: int->int
  #+NAME: foo-baz
  #+BEGIN_SOURCE c
    int baz (int i) {
      // some logic
      return some_val;
    }
  #+END_SOURCE
Those variable names are useless, figure out what they actually mean and document them in the org file. Consider renaming them. There are no test cases, create a tests section in the org file and start writing up simple test programs (or use a unit test framework, but you may need to do some significant refactoring before you can do that).

A benefit of org-mode here is that sometimes you want to test some functionality (and have no unit tests yet). So you try to test baz from above. But once you build that source file and a second foo_test.c file you find out that foo has a function, quux, which has dependencies outside of just the header files and foo.c itself. To build this thing you have to build bar.c and maybe even the whole Linux kernel. "Shit!", you say, "How do I handle this?" Well, org-mode to the rescue:

  * Testing Foo
  ** Testing baz
  #+BEGIN_SOURCE c :tangle foo_baz_test.c :noweb tangle
    >
    >
    >
    // maybe some other things like the header references.
    // some test code that is able to focus in on just the baz function
  #+END_SOURCE
You've fully isolated that one chunk, and quux is no longer being included in the build for this test. Whatever problems it has don't impact you (for this test). Once you figure out how to isolate or mock quux's dependencies so that you aren't including the full Linux kernel, you can add it to the set of tests. Now the foo file is fully brought under testing and you can more effectively refactor it. And the mocks and all you've made allow you to move to a proper unit test framework if you want, versus the ad hoc initial framework we've produced here.

Even if you don't care about testing your code, the above process (sans the testing part) will allow you to perform a disection on your codebase and get it documented and specced out properly.

Re: Ask HN: Have you ever inherited a codebase nobody on the team could understand?

#218

Earlier quoted context omitted.

Do you have any specialized tools (code navigation tools, for example) that you use when first encountering these large piles of code? I'd love to hear some recommendations; I have to deal with large (only sometimes bad, but always large) piles of vendor code. I'm currently staring a pile of 900kloc of pretty nice code but it's a /lot/ of code.

Not OP, but I imagine this is fairly language specific. This is where Java shines. Keep in mind I mostly work with services not applications. My process looks like this: Step one: Identify sources of reflection, this is the triskyest. Hopefully the only dependencies are open source, so you generally know what they do and grep can usually find the rest. Step Two: go code spelunking. Find your entry points. Find your m…

I'm working in Linux wifi drivers, all in C. Giant complicated protocol with giant complicated code. I've been tinkering with Microsoft VSCode, CLion, and Sourcetrail. Vim + Ctags seems to work well in the beginning but only gives pinpoint answers (can find trees, but little view of the forest). Still experimenting.

Re: Ask HN: Have you ever inherited a codebase nobody on the team could understand?

#219
post #81

Oh yes, many times! I used to joke, "Why do I always inherit stuff like this?" and my mentor would respond, "Because companies with good code bases don't hire very often; their people are all so happy." How I have dealt with it: 1. Never complain. Never bad mouth any of my predecessors. Whatever they did wrong, I probably did somewhere else just as badly. We all have. 2. Never be bashful about what is wrong. Be objec…

>9. Bitch about all of the above at home at night. You may ruin your marriage, but at least you'll still have a job.

It is good to keep in touch with a bunch of fellow nerds as a support group to vent out.

Re: Ask HN: Have you ever inherited a codebase nobody on the team could understand?

#220
post #152

Earlier quoted context omitted.

Do you have any specialized tools (code navigation tools, for example) that you use when first encountering these large piles of code? I'd love to hear some recommendations; I have to deal with large (only sometimes bad, but always large) piles of vendor code. I'm currently staring a pile of 900kloc of pretty nice code but it's a /lot/ of code.

It depends upon the platform, the language and the toolsets. For instance, for C, a trick I've used is to (if I can) use different C compilers and crank the warnings/errors to 11 and fix every complaint (or try to---it can be daunting to attempt this all at once). Other tricks---run the code through linters or other stylistic nit-pickers and fix those too. I haven't yet used a code reformatter, but that's a quick way…

I'm all in C, too: kernel drivers from wifi chipset vendors. There's one C compiler we can use (gcc) and we're even restricted to working with a specific version (because cross compiling). We have to be very careful about changing any of the code because we have to integrate any changes into the next drop of the vendors' code. Static analysis is our best bet. Is a really interesting problem.
Post reply on HN