Ratio of real code to supporting code

2019-06-25 13:35发布

I'm finding only about 30% of my code actually solves problems, the rest is taken up by logging, tests, parameter checking, exceptions, error handling and so on. Do you find that in your code, and is there an IDE/Editor that allows you to hide code that's not interesting?

OTOH are there languages which make the support code more manageable and smaller in size?

Edit - I think we're all aware of the difference between business logic and other code. I'm not saying that the logging etc is not important. The things is, when I'm coding I'm either implementing business logic, or I'm making sure things don't break. For me that's two different ways of thinking, do others develop like that, and is there an IDE that supports that way of developing?

11条回答
趁早两清
2楼-- · 2019-06-25 14:13

You could just make a static class in a utilities assembly that checks your parameters and things. For instance in the Spring Framework (which is where I got the idea) it has an Assert class and it makes it really fast to make sure that string params aren't empty or that object params aren't null. It cleans up validation code and reduces duplicate code which is a win win.

查看更多
【Aperson】
3楼-- · 2019-06-25 14:21

Supporting code is just as important as the "real code". The quality of your product is determined as much by supporting code as anything else.

Consider an automobile. In terms of just getting from point A to point B, that requires nothing more than a go-cart: a frame, a seat, an engine, a few tires. But modern cars have a lot more than just the basics. Highly efficient engines using electronic engine timing. Automatic transmissions. Bucket seats. Heating and A/C. Rack and pinion steering. Power brakes. Anti-lock brakes. Quiet, comfortable cabins protected from the weather. Air bags. Crumple zones and other advanced safety features. Etc. Etc.

Details and execution are important, even in software. If you find that your "supporting code" tends to look more like kludges and hacks, then it's time to rethink your fundamental approach. But ultimately the fit and finish determines quality of the end product as much as anything else.

Edit: The questions you should ask yourself:

Is your "supporting code":

  • An umbrella duct taped to a pole or a metal and glass cabin frame?
  • A piece of pipe tied to the front of the car or an energy absorbing bumper integrated into a crumple zone?
  • A grappling hook on a rope tied to the frame or 4-wheel anti-lock power brakes?
  • A pair of goggles and a thick coat or a windshield and a heating system?

Answers to these questions will probably affect how much you care about your "supporting code".

Edit: Response to Dave Turvey's comment:

I'd encourage rereading the original question, one of the examples of "support code" listed is "error handling". Consider this for a moment. Imagine it in the context of, say, an automobile, a microwave oven, or even an operating system. Should error handling be relegated to second class citizenship because it serves a "support" function in some abstract sense? In an automobile the safety features are part of the fundamental design of the vehicle and comprise a substantial part of the value of the car. The safety features and "error handling" of a microwave oven (indeed, of the microwave oven's embedded software as well) are an important part of its value as well. A microwave oven that was improperly shielded could cook food just fine, under the right circumstances, but it would pose a hazard to the operator.

The implicit featureset of every tool (software or otherwise) includes this list:

  • Robustness
  • Usability
  • Performance

Everything anyone has ever built or used has had these features. Failure to understand this will translate to failure to execute well on these features which will make for a poor quality product of low value and low commercial interest. There is no such thing as "support code", there is only a misunderstanding of the nature of what it means for a feature to be complete. A "feature" that works in the abstract only under laboratory conditions is an experiment, not a part of a product.

The idea of pure, pristine features floating on a bog of dirty, ugly support code is the wrong image of software development. Instead, think of elegant, superbly-integrated machinery that is well-built, intuitive to use, and powerful.

查看更多
兄弟一词,经得起流年.
4楼-- · 2019-06-25 14:22

I use the #region directive in Visual Studio to collapse blocks of code that are not the primary focus, e.g. unit tests. With log4net logging statements are only ever one line. I haven't found any approaches to reduce the parameter checking code although it sounds like C# 4 has some kind of contract framework that will help there.

查看更多
太酷不给撩
5楼-- · 2019-06-25 14:24

It's like you want to take a trip to the top of Pike's Peak. You can take the Winnebago, you can take your SUV, or a motorcycle, or ride up on your bike.

Some ways are a more or less expensive, faster, etc. Sometimes you end up taking along a lot of stuff the isn't there strictly for accomplishing vertical progress; it's nice to have a beer in the cooler. But it pays to remember that you're responsible for everything that goes with you to the top.

查看更多
我只想做你的唯一
6楼-- · 2019-06-25 14:26

If my IDE could hide "not interesting code" I would definitely turn the feature off. You wouldn't want that happening, I bet :)

There are certainly languages that minimise the amount of supporting code, but I don't think you could switch from Java to lets say JavaScript simply because in JavaScript you wouldn't have to declare every exception... I think it's quite necessary to have your supporting code where it is.

Oh, and you could have your program formally specified and mathematically proven, then you wouldn't need to support your code too much ;D

查看更多
登录 后发表回答