3

Maybe this answer already exists here, but I do not know if there is any technical term for that.

I'm starting to develop in C ++ and as the program grows in size, it gets harder to move through the source code.

However within the code there are several parts that are already 'solved', that is, I do not need to change them anymore, but they are there, in the middle of the source code, and I have to go through them all the time, which disturbs the concentration and often confuses.

For example, a simple "if" can occupy a whole screen (more or less 30 to 40 lines). I do not need to mess with it anymore, but it's not a function, it's not a class, just a part of the code that has to be there.

Some editors allow me to collapse the part of a code, for example the if, something I already do, sometimes it gets lost and the code reappears.

Maybe I cut the program into logical parts and put it in separate files, then inserting them into the main source code through an 'include', but that would be ugly ...

Anyway, I come here to ask: how to deal with large source codes in the best way?

  • 5
    There's no real way to answer this question. The basic tools are well known: breaking functions up into smaller ones, aggregating similar functionality into classes, using multiple files with each containing related content, etc. Because your question lacks specifics, it's difficult to know one way or the other how to employ these techniques or which techniques your code needs. – Nicol Bolas May 21 '18 at 1:24
  • OpenGrok is your friend – solarflare May 21 '18 at 2:44
  • 3
    The term you are looking for is refactoring. It is not one technique, but a collection of many. Extracting code snippets into functions is probably the most frequent used refactoring technique, but not the only one you should learn. I recommend to get a copy of Fowler's book. – Doc Brown May 21 '18 at 18:00
22

For example, a simple "if" can occupy a whole screen (more or less 30 to 40 lines). I do not need to mess with it anymore, but it's not a function, it's not a class, just a part of the code that has to be there.

Have you heard of functions? They are these wonderful little guys that can stand in for piles of code that you're sick of looking at. Give the pile of code a name, stuff it in a function with that name, and then just call it by name. Now you don't have to collapse anything to hide these tedious but necessary details.

I bet you'll find them addicting. The more you use them the smaller they get until you have all these wonderfully tiny functions that hide all the boring stuff behind well thought out names that ensure that when you do bother to look inside them you find pretty much what you expected.

  • 5
    I provided a link to an example. If you still can't see how to apply this technique please add some example code to your question. I'll show you how. – candied_orange May 21 '18 at 0:38
  • 7
    @RogérioDec: "Also, using functions, I'm required to redefine multiple variables as global so they are viewed by the function." That's what function parameters are for. And if the contents of that function need more than 3-4 variables declared outside of it, then that suggests your code is well on the way to becoming spaghetti. Disciplined programming practices are your best way to avoid this. – Nicol Bolas May 21 '18 at 1:20
  • 2
    @CandiedOrange, you are right, I had not read the example of the link you posted. I read it now and it makes sense to break this by functions in the format indicated in this link. Thank you very much. – Rogério Dec May 21 '18 at 2:39
  • 1
    If a function needs more than 4-8 parameters, then it makes sense to convert that function into a C++ class, and those parameters into class members. I understand that there are huge differences in neurodiversity among C++ programmers (some programmers need to read linearly; some need to read verbally; some need to read imaginatively) but a good way to learn to be cooperative is to practice reading large C++ code base developed by other people or organizations. It appears that, with sufficient practice, a C++ programmer can become proficient in reading in multiple styles. – rwong May 21 '18 at 4:16
  • 4
    @rwong just because a function has 4 to 8 parameters does not mean the entire function needs to be turned into a class, it is far more likely the function needs to be decomposed into multiple functions first, or that there is some other issue with the code. Try simpler more direct methods of abstraction before trying to plaster classes with only a single function everywhere. – opa May 21 '18 at 20:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.