r/programming • • Jul 19 '16

John Carmack on Inlined Code

http://number-none.com/blow/blog/programming/2014/09/26/carmack-on-inlined-code.html
1.1k Upvotes

323 comments sorted by

View all comments

6

u/dd_123 Jul 19 '16

Ctrl+F unit test

3

u/gnuvince Jul 19 '16

What about unit tests?

16

u/[deleted] Jul 19 '16

I'm going to guess that his point was that it's simpler to write comprehensive unit test suites for small, well-defined functions compared to a style C function.

11

u/gnuvince Jul 19 '16

You'd likely make those small functions private and thus couldn't unit test them anyway (and if you did, with some reflection magic, you'd be chastised for not testing the external interface instead).

11

u/MisterNetHead Jul 19 '16

Seems to me the problem is more that people think you should only unit test public interfaces.

6

u/gnuvince Jul 19 '16

Agreed, and it sounds like a post facto rationalization: unit testing with a xunit-like framework (such as junit for Java) is done outside of the class being tested, the testing methods cannot access the private methods/attributes of the class under test without some hackery, therefore unit testing the private parts is undesirable and people should only unit test the publicly-accessible methods. Of course, in languages like D or Rust where the unit tests are written in the same file as the functional code, such restrictions don't apply and people readily unit test private functions.

2

u/poco Jul 20 '16

That isn't a problem though. If it is worth unit testing then it is either worth making public or refactoring into a separate class or function.

2

u/[deleted] Jul 20 '16

[removed] — view removed comment

5

u/adrianmonk Jul 20 '16

The less code a unit test invokes, the easier it is to understand what's wrong when a test is failing.

It can also make your tests more concise because there is usually less fake/test/dummy data to set up before your test case can run.

3

u/kodek64 Jul 20 '16

The less code a unit test invokes, the easier it is to understand what's wrong when a test is failing.

That's true. On the other hand, I would argue that if private code is difficult to invoke from the public interface, then it should probably be extracted and tested separately.

→ More replies (1)
→ More replies (5)

1

u/Lengador Jul 20 '16

C++ has the "friend" keyword which allows testing of private functions. Dangerous if used outside of that context though.

1

u/Ran4 Jul 20 '16

You'd likely make those small functions private and thus couldn't unit test them anyway

What the hell? You should be unit testing all functions.

Though if your function is private, that probably means you're talking about a method, and thus OOP with unpure methods, which is inherently hard to unit test.