开发者

good programming practice

开发者 https://www.devze.com 2023-02-01 14:49 出处:网络
I\'m reading Randall Hyde\'s Write great code (volume 2) and I found this: [...] it’s not good programming practice to create monolithic

I'm reading Randall Hyde's Write great code (volume 2) and I found this:

[...] it’s not good programming practice to create monolithic applications, where all the source code appears in one source file (or is processed by a single compilation) [...]

I was wondering, why is this so bad?


Thanks everyone for your answers, I really wish to accept more of them, but I've chosen the most synthetic, so that who reads this question finds immediatel开发者_如何学Pythony the essentials.

Thanks guys ;)


The main reason, more important in my opinion than the sheer size of the file in question, is the lack of modularity. Software becomes easy to maintain if you have it broken down into small pieces whose interactions with each other are through a few well defined places, such as a few public methods or a published API. If you put everything in one big file, the tendency is to have everything dependent on the internals of everything else, which leads to maintenance nightmares.

Early in my career, I worked on a Geographic Information System that consisted of over a million lines of C. The only thing that made it maintainable, the only thing that made it work in the first place is that we had a sharp dividing line between everything "above" and everything "below". The "above" code implemented user interfaces, application specific processing, etc, and everything "below' implemented the spatial database. And the dividing line was a published API. If you were working "above", you didn't need to know how the "below" code worked, as long as it followed the published API. If you were working "below", you didn't care how your code was being used, as long as you implemented the published API. At one point, we even replaced a huge chunk of the "below" side that had stored stuff in proprietary files with tables in SQL databases, and the "above" code didn't have to know or care.


Because everything gets so crammed.

If you have separate files for separate things then you'll be able to find and edit it much faster.

Also, if there's an error, you can locate it more easily.


Because you quickly get (tens of) thousands of lines of unmaintainable code.


Impossible to read, impossible to maintain, impossible to extend.

Also, it's good for team work, since there are a lot of small files which can be edited in parallel by different developers.


Some reasons:

  • It's difficult to understand something that's all jammed together like that. Breaking the logic into functions makes it easier to follow.
  • Factoring the logic into discrete components allows you to re-use those components elsewhere. Sometimes "elsewhere" means "elsewhere in the same program."
  • You cannot possibly unit-test something that's built in a monolithic fashion. Breaking a huge function (or program) into pieces allows you to test those pieces individually.


While I understand that HTML and CSS aren't 'programming languages,' I suppose you could look at it in terms of having all of the .css, .js and .html all on one page: It makes it difficult to recycle or debug code.


Including all the source code in a single file makes it hard to manage code. The better approach should be to divide your program in separate modules. Each module should have its own source file and all the modules should be linked finally to create the executable. This helps a lot in maintaining code and makes it easy to find module specific bugs.

Moreover, if you have 2 or more people working simultaneously on the same project, there is a high probability that you will be wasting lots of your time in resolving code conflicts.

Also if you have to test individual components (or modules), you can do it easily without bothering about the other independent modules (if any).


Because decomposition is a computer science fundamental: Solve large problems by decomposing them into smaller pieces. Not only are the smaller problems more manageable, but they make it easier to keep a picture of the entire problem in your head.

When you talk about a monolithic program in a single file, all I can think of are the huge spaghetti code that found its way into COBOL and FORTRAN way back in the day. It encourages a cut and paste mentality that only gets worse with time.

Object-oriented languages are an attempt to help with this. OO languages decompose problems software components, with data and functions encapsulated together, that map to our mental models of how the world works.


Think about it in terms of the Single Responsibility Principle. If that one large file, or one monolithic application, or one "big something" is responsible for everything concerning that system, it's a catch-all bucket of functionality. That functionality should be separated into its discrete components for maintainability, re-usability, etc.


Programmers do a thousand line of codes right, if you do things on separate side. you can easily manage and locate your files.


Apart from all the above mentioned reasons, typically all the text from a compilation unit is included in final program. However if you split things up and it turns out all code in particular file is not getting used it will not be linked. Even if its getting linked it might be easy to turn it into a DLL should you want to decide using the functionality at run time. This facilitates dependency management, reduces build times by compiling only the modified source file leading to greater maintainability and productivity.


From some really bad experience I can tell you that it's unreadable. Function after function of code, you can never understand what the programmer meant or how to find your way out of it.

You cannot even scroll in the file because a little movement of the scroll bar scrolls two pages of code.

Right now I'm rewriting an entire application because the original programmers thought it a great idea to create 3000-line code files. It just cannot be maintained. And of course it's totally untestable.


I think you have never worked in big company that has 6000 lines of code in one file... and every couple thousand lines you see comments like /** Do not modify this block, it's critical */ and you think the bug assigned to you is coming from that block. And your manager says, "yeah look into that file, it's somewhere in it."

And the code breaks all those beautiful OOP concepts, has dirty switches. And like fifty contributors.

Lucky you.

0

精彩评论

暂无评论...
验证码 换一张
取 消