by sam_lowry_ · 10y ago · 0 comments
A year ago, I rewrote 424 lines of Java+Spring+Hibernate in 18 lines of bash. This is less glorious, but if you compare the size of the deliverable, it's 39Mb for the J2EE webapp against… 772 bytes for the shell script.
At my current job, I maintain an app with 500,000 lines of code that could easily fit in 20,000.
Code bloat is indeed a recurrent problem.
https://news.ycombinator.com/item?id=4549544
I've written small Java apps --- one class only, i.e. one source file --- which needn't be any more complex, yet coworkers have said there's something uncomfortable about my code; but for some reason can't explain exactly why. I'd get comments like "wouldn't it be better if you made this (only used once and very trivial) line of code a separate function?" "could you use more classes?" (for a <100LoC script-ish thing that had almost no duplicated code nor much in the way of loops.) It's almost as if they can't get their heads around how simple something can be, so it somehow feels very wrong to them.
Then there's the extremist "premature optimisation is evil" attitude, which I think is completely misguided because more code and complexity is bad not only in terms of computer time but also programmer time --- it takes more time for programmers to design, write, read, and debug more complex code.
I'd get comments like "wouldn't it be better if you made this (only used
once and very trivial) line of code a separate function?" "could you use
more classes?"
The trouble is that there is a cargo-cult belief in unit testing in the Java (and .Net) worlds. Everything must have a full suite of unit tests, even if it's a one-off script. Therefore, in order to enable the mock objects needed for unit testing, you can't have "just" a class. You have to have an interface, which that class then implements. If you have any non-trivial piece of functionality, it must be encapsulated into its own method, otherwise, how are you going to test it in isolation.My belief is that it all stems from the fact that Java doesn't have a REPL, so programmers use "test-driven development" to get the same level of interactivity from their code.
It's not the same thing though. REPL gives you fast feedback once. Unit tests you build by following TDD are there to stay.
Many of the instances were things that we were only likely to make one example of either. I could see that argument being valid if there was a clear second implementation in mind, but an interface with only one implementing class is often just wasted time.
1. Lambdas: Lambdas are brief and to the point, the expressions reflect the terseness of `C/C++`. In many situations, you can totally do away with interface creation by just using a lambda instead. Consider all the lines of code saved in a legacy code by just using lambdas in code!
2. Default methods: Again, `Interface patching` is a curse that a lot of Java projects suffer from. Requirements change slightly and what your typical enterprise Java dev does is add patches upon patches and layers upon layers in an interface. With default methods, you can just add default methods to your existing interfaces without affecting the ABI of your app. Again, tons of redundant code saved here.
3. Type annotations: Again, your typical Java devs have to resort to ugly hacks to place any constraints on the object properties. With type annotations, all you have to do is:
@NonNull String str;
And lo and behold! The variable `str` cannot be assigned any `null` value. So, lots of validation code being saved here again.To the best of my knowledge, none of the above features exist in the `C#` language (yet), so I take it as a Java's edge over C#.
I never felt like interface patching was a problem for me, may be because I'm more of a FP than an OOP programer. Type annotations do sound interesting.
I have seen a little of the interface vs function conversions in modern Java, and it seems a decent enough small convenient feature in the context of Java, but it doesn't feel like a big win that I would cry out for in C# code.
Defaulting to not-null and immutable values is a trend in several recent languages, e.g. from Swift and Rust to F#. This IMHO looks like a good step forward and I welcome it, however it's hard to see how this would be comprehensively added into existing languages like Java or C# with an existing legacy of not working that way. Opting into Not-null in c# has been talked about many times (1) in various forms, even implemented using attributes (similar enough to type annotations). (2)
1) http://twistedoakstudios.com/blog/Post330_non-nullable-types...
2) http://stackoverflow.com/questions/792531/c-how-to-implement...
Once a program has been in production for a while and its use cases are more well defined it becomes easier for a single person to swoop in and rewrite it in a far more conscice way. In most of these cases I think the language is pretty irrelevant in terms of how much can be culled.
> The iterative bloat during development can happen for so many reasons. (Numerous changes in requirements late in the project,
... and people not cleaning their mess up afterwards;
> many developers working on numerous modules,
... without proper coordination, or, without the drive to keep things clean (i.e. proper manners);
> design for flexibility and expansion that are never used,
... and never cleaned up, i.e. left there to rot by the people who put them there;
> going too far down a path that turns out to be more work than an alternative)
... and not cleaning up.
> Once a program has been in production for a while and its use cases are more well defined it becomes easier for a single person to swoop in and rewrite it in a far more conscice way.
I think it's always easier for the guy/gal who wrote something to clean it up than for the next guy/gal, who doesn't know the code or its history as well.
Really, this is all just people not cleaning up (or being allowed to clean up) after themselves.
Now if we could all just be nice to each other and hold hands..
Most of the time there isn't any budget left after they have a functional app for cleaning things up. Despite impassioned hand wringing the client will place value on things they can see over things they can't. So any budget left over will almost invariably be directed toward additional features and not on proper engineering.
Sad but true.
Speaking to your points there's also the problem of not being able to see the forest through the trees for developers who have been with a project from the beginning. So large refactoring endeavors can be less obvious to them. They may also have fatigue and be less willing to rewrite everything. This combined with budget pressure is all it takes for the bloat to stick.
Once a piece of functionality gets established in production, over time the knowledge of the original requirements and the implementation is lost. The consequence is that nobody knows why the code does what it does (and usually it does something important) and due to it working fine in production there is strong pushback to making changes.
I definitely agree with your ethos though. I think there's a lot to be said for having some buffer between functionally complete code and a hard ship date so that you can leave things in a state that don't make you or someone else feel bad about things later.
Indeed, I once had the pleasure of replacing a BizTalk server with 50 lines of C#. It wasn't that the BizTalk server wasn't a bad idea. The project was for a big harbour, which have documents, cargo manifests, import papers and a ton of other stuff flying back and forth. The idea of having a central hub for data exchange and data conversion wasn't actually that fare fetched. It's just that the only part that was ever implemented was a the upload of a small 5-10 line xml file to the maritime administration, regarding the departure of ships.
After three years of running a BizTalk server, for that one purpose, and with no-one fully understanding Biztalk, we just replaced it with a custom Windows service.
If anything the takeaway is that you projects should be revisited once in a while for clean up.
After the dust settles you can see what it's actually used for. And sure, its not like the other use cases were bad or wrong, its just not how it ended up.
It's possible that the other use cases never got traction because of some longest yard effort required on the client's end to implement a workflow. But even that low of a bar ended up being too high.
One is that people use abstractions that are more verbose than necessary. The fact this is bad is pretty uncontroversial. Though very few people go out of their way to write bad code, it's just that everyone's view on the right amount of abstraction is different. What's fine to you might look like gibberish to me. I've radically simplified code by converting complex regexes to multiple steps, for example.
The other cause of bloat in mature code is the inevitable bug fixes, feature requests, and edge cases that accumulate over time. Having the attitude that a system is unnecessary complex is dangerous because you don't know which of these edge cases could doom your rewrite.
Even if you do manage to ship a simplified version of the system it's incredibly difficult to maintain the pristine state of the system. Unless you devote all your time to playing code police then cruft will creep back in when others touch the code. This is where having more abstractions can actually help, because good abstractions limit the impact a bad piece of code can have.
Sure it's great to have lots of little single purpose applications that can be mixed/matched in a variety of different ways.
But...
If everything in a system is built based on that premise, eventually the OS just becomes a lousy abstraction for a global namespace.
I think it's important to strike a middle ground. Build tools that work well for solving a 'class' of problems, then include public interfaces so they can be re-used for the higher level composition of systems.
In practice, even simple operations like "rm", or "mkdir", or even "cd" can fail in new and exciting ways. The blanket use of "errexit" and "pipefail" is really only the bare minimum of error handling; scripts that simply _stop_ as soon as a command exits non-zero are not generally an operable product that somebody unfamiliar with the innards of the system can be expected to work with.
It is also unfortunately easy to miss out on putting quotes around expansions, or to get caught out by the somewhat peculiar behaviour of variable scoping in functions. Some improved constructs are available in more recent versions of bash, but if the goal is to be _portable_ (without also shipping your target bash binary) you may not be able to use some or any of them.
The bash "language" (such as it is) is antiquated and clunky, because it seeks to be backwards compatible with the rich tapestry of history that is the Bourne shell. A truly robust, operable program written in this environment often does not come out a whole lot shorter (if it is shorter at all) than a program written in another language with better facilities.
Most recently, I just rewrote a decently written ~8 kloc C program in to a ~4 kloc Go program that does 5 times as much, 10 times faster. If I had more time, I could probably shrink the new Go program to about half its size. This new program includes the old, as well as a new simplified interface, both HTTP.
Moving clients to the new interface has had cascading C#/Scala code reduction of ~1 kloc each. This is the most important point! Simplicity breads more simplicity.
The big benefit is the drastic complexity reduction. Suddenly the junior developers look like superstars because they can understand it and their productivity soars.
I'm continually surprised at how otherwise smart engineers end up writing boated spaghetti that is 'correct' at the micro level but ridiculous at the macro level. A few poorly chosen abstractions can increase the cost of development by 10x easily.
I played with Forth. Sure you can do complex things without parenthesis, but if you squeeze something complicated into a single line equation, it will be harder for a human to follow than if you use parenthesis.
The original article also says "It's never clear how efficiently source will be translated into machine language," which is complete garbage: Most modern processors were designed precisely to optimize for the kinds of control and data structures that C creates, and C (though NOT C++) has close to a one-to-one mapping from code you write to assembly language output.
A key exception is those infix equations, which C can reorder in ways that are more optimal than a naive developer writing Forth would create, so this hardly seems like a recommendation for Forth.
This is all orthogonal to whether bloat exists or is a bad thing. I think that bloat exists and is a bad thing primarily because developers are some combination of lazy, unskilled, or pressured to complete features on constant deadline. And honestly, in most cases it's probably all three, though I would guess anyone reading HN is mostly afflicted with the last.
Not any harder than following Master Yoda speech.
OTOH, following something (say, an English text) (which is heavily parenthesised) may be (I'm sure there is a lot of references to back this notion) exceptionally hard (even if there is just one nested level (and more levels certainly add more confusion)). Choosing flat over structured often makes a lot sense.
6 8 7 5 4 + * - + .
-49 ok
Tell me that's easier to read than 8+6-(4+5)*7
Parenthesis can make things harder to understand if poorly used. RPN can make things harder to understand if poorly used.The reverse is also true for both. Forth does not prevent bad code.
Because of this effect you cannot trust your own sense of what is inherently easier to read, it will be biased by experience.
Take my above asides ", convoluted," and ", typically,". Neither are necessary for understanding the sentence, but neither make the sentence exceptionally difficult to understand for a literate reader.
The method in which parentheses (and other structuring notations) are used is more akin to grouping from algebra and mathematics.
EDIT:
Original: OTOH, following something (say, an English text) (which is heavily parenthesised) may be (I'm sure there is a lot of references to back this notion) exceptionally hard (even if there is just one nested level (and more levels certainly add more confusion)). Choosing flat over structured often makes a lot sense.
But it doesn't make sense when we remove the parenthetical statements:
OTOH, following something may be exceptionally hard.
Choosing flat over structured often makes a lot [of]
sense.
(one edit for clarity). That first sentence has lost all meaning. Following what? Following a car on foot can, indeed, be exceptionally hard. But following a slug is often exceptionally easy.A more reasonable version of your sentence:
OTOH, following something--say, an English text--which
is heavily parenthesised may be, I'm sure there is a
lot of references to back this notion, exceptionally
hard (even if there is just one nested level (and more
levels certainly add more confusion)). Choosing flat
over structured often makes a lot sense.
Now the remaining asides (demarcated using the various English notations of em-dash, comma or parenthesis) can be removed without losing the general sense of your sentence.Nesting with commas is just as confusing as nesting with parenthesis. It forces your reader to operate with more than one contexts at a time, and this is exactly what pushes up the complexity level.
Even in Lisp, I realised that I always tend to build flat DSLs instead of the nested, using the non-parenthesised punctuation wherever possible. And I am not alone, take a look at the LOOP macro for example.
https://github.com/freebsd/freebsd/blob/master/etc/rc (standing on https://github.com/freebsd/freebsd/blob/master/etc/rc.subr)
made me feel the same.
(...)
> Code bloat is indeed a recurrent problem.
But in your example, you're still depending on thousands (millions?) of lines of BIOS, OS, library, runtime code.
It's so many orders of magnitude different than the point Moore is making -- and also, more subtly different: it's not like Forth system don't have library/OS code -- but eg. fitting a rich editor/IDE and OS on a floppy is quite different from eg: Ruby. It's a completely different mindset.
Even if something like Ruby does a lot of things out of the box, if you were to strip it down to a subset that only allowed to be used as a caluclator repl, it'd still be quite complex compared to a Forth system (just think about all the code need to handle keyboard input, and talking to the monitor over VGA/HDMI etc).
None of that invalidates your point, that code bloat is a problem, I just think it highlights that there are different kinds of bloat.
Somewhere between colorForth and developing in in a "traditional" language with a "traditional" IDE on a "traditional" OS, we find things like Project Oberon and Smalltalk-80. Or VPRI's recent effort to create a full drivers-through-gui-up-to-and-including-applications system in 50kcloc total, making aggressive use of DSLs to manage the complexity.
Ruby code requires the whole ruby runtime.
Bash and Perl might be the only exceptions to this due to their incredibly terse syntax for a certain subset of tasks.
Curious. Were you paid to write the Java? If yes, how much did your employer spend to license that software?
Is it possible that bloat supports livelihoods?
The Java code itself probably took less than a few days to write. Java/Spring/Hibernate have no licensing fees (in most cases) and is an industry-standard stack.
I'm guessing the original author didn't write it in Bash because either (1) they didn't know Bash, (2) they didn't know if/when requirements would expand beyond what Bash can easily do, (3) they wanted platform independence, or (4) their team wrote everything else on the Java/Spring/Hibernate stack.
You can argue the merits of all those reasons, but I don't see bloat being profitable as a factor.
The difference in lines and executable size is likely due to an apples-to-oranges situation. Bash scripts seem really light weight if you're not counting all of their system dependencies and if you're writing a program that Bash excels at (such as moving data around).
"J2EE" does not suggest Oracle in any way?
My understanding is that companies that license proprietary software are heavily involved in writing these open standards. Incorrect?
Please forgive my ignorance re: Java.
I think Moore was writing about drivers written in C.
Is the GP's story about programmers at a hardware company?
Does it matter that most of the comments are about high-level programming, not writing drivers?
Just wondering...
Proprietary vendors have had a large part in writing these open standards, but they're mostly interested in creating a standardized, free commons.
GP's story is not about programmers at a hardware company, and it's probably not directly applicable to colorForth.
Would you write it in C++ differently if you did it again?
Typically bloated code comes from legacy and years of organic growth. I'd imagine almost any 30k LOC project in any language that has been developed more than just a few years could be rewritten in a small fraction of the original size, now that you actually know what the end result must do, a luxury you don't have on the first go.
Try to give him the benefit of the doubt. There's no indication that that's what he meant. Instead, he probably meant that one possible reason people write bloated code -- or at the very least, do not attempt to refactor it after writing it -- is that it provides them with job security. Which is a fair point.