How many lines of source code should a function have?

This is a question that I'm really curious about: How many lines do other people write in their functions? For me, it's always very different. To be honest, I like to have several larger functions based on several smaller ones. The larger ones are always around 45 lines long at the most, but I also like to support these larger functions with semi-larger functions (which are around 20 lines long at the most). What's your opinion?
And yes, you old C geeks, I know that I need 1212931 1-line functions because otherwise my program runs 0.0000000000000001 milliseconds slower, but I think that for visual reasons it always becomes very confusing.
(The same applies to files)

(1 votes)
Loading...

Similar Posts

Subscribe
Notify of
6 Answers
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
CSANecromancer
1 year ago

I know I need 1212931 1 rows of long functions because otherwise my program runs 0.0000000001 milliseconds slower

This shows very well that you still have no idea what good functions are.

Simplified: A function takes over exactly INTRODUCTION Working step. No more, no less.

The reason for this is not in any milliseconds you’re fabuling about, but is that it makes debugging and waiting possible even after 6-12 months (or longer). If the program doesn’t load a file correctly, then I’m looking for the FileReader used – and not for any 200 line feature named “LadeMalDenGanzeKram”.

However, I also talk about the professional (work) area. Private and a hobby you can of course handle how you are funny. Look at this. https://www.ionos.de/digitalguide/websites/webdevelopment/was-ist-clean-code/ There are some employers or Developer teams worth it.

CSANecromancer
1 year ago
Reply to  Dessischment

I find the first sentence not very nicely decorated

Don’t be so mimicful if you’re different from

her old C cracker

ensign.

and tell me how to write your private code or if you start 9/11 2.0 as soon as you see a function longer than 20 lines

I can do it.

No, I’m too old as if I’d open a barrel again if a boy’s mouth thinks he knows everything better than any other developer before him (no sarcasm). So if someone thinks to present me a method with 200 lines, then I’ll rework it and mention it in the next stand up.

You said you had four years of experience. Now, of course, I don’t know if you’ve only been working for small shops or even in real developer teams. I’m in the latter area and can tell you from this: “Lone ranger” with their super-own code don’t really last long in the industry. Because what you see as a great code is for others (such as me) an interpretation that needs to be revised every time so that others can work with your code. These revisions cost time and money. And if you constantly cost these costs because you want to make yourself more real, instead of holding you to the Coding Guides (reasonable software forgings have something like this), then you have a very quick discussion with your supervisor.

The reason for this is not that no one can suffer from you, but that one in team together working. And that also means that the code is written in a way that is everyone is understandable. Personal Gusto is on the back. For example, in my work, I feel our source formatting somewhat exaggerated. But hey – I’m paid for working with such a code and incomprehensible it isn’t. Only oversized – according to my personal taste.

In the private sector, I have programmed classic spaghetti code quite sooner (last millennium – no sarcasm again) and later the giant functions you gave me were used. After the first few WTF moments in which I was looking for death in my own code, I then started to break down and reduce my methods. I prefer methods that fit on the screen at once and can be viewed directly, without scrolling.

I would have liked to comment on your code example now, but I don’t think that’s what I’m doing. There seem to me ad hoc methods and Getter/Setter thematically mixed. But I’ll try it like this:

There’s one Class ObjectList, that would also include a Getter on Data. If I now want to add an object to the list, this is called with me (you note the big and small words: the small written is an object – not the class!) objectList.Data.add(xxx); Thus, there would be no createListElement() and also no upodateObjectList() (this happens automatically) and the createBaseObjectList() then lies simply in the constructor with its functionality.

I hope I could help so far – even if I’m one of those old C crackers.

CSANecromancer
1 year ago

Therefore, I would like to know what you think should change the coding again (as I said it would be interesting).

So the “you” is all right, I don’t need to be buried.

There is this saying: “Too many chefs spoil the porridge.” The same problem is with software development in teams. We are in our team:

  • A software architect who is team leader at the same time and surprisingly makes the job really good. No joke: Good team leaders are fucking rare.
  • Two Senior Developer/Architects (I am one of them). Focus: Everything a “C” has in its name (C, C++, C#, JavaSc)ript) and protocols/APIs.
  • A Software Developer, Focus on Frontend (JavaScript, Angular).
  • A cryptologist and former developer responsible for cryptographic algorithms (IT Security Division).
  • An apprentice(!) in the 3rd year.
  • A tester.
  • A senior tester.

And we’re just one of three developer teams. Now must GENERAL will be coordinated so that a reasonable software will come out at the end. Difficulties include:

  • the cryptologist formulates their algorithms in pseudocode with mathematical notes. Our azuubi has little chance to understand.
  • the other senior developer prepares for retirement. His willingness, for example, to adapt his coding style to the rest of the team is still present, but not extremely pronounced – to say so.
  • I have the focus of Backend. This means that sometimes it becomes a bit more tricky when the front-end developer and I need to exchange details. I don’t speak an angular and he’s not so fit in C++.
  • And our testers not only have to overcome the linguistic barrier (Ukrainian or Python), but also have a completely different technical aim than we developers.

With us, for example, there is also a standard case that no one simply checks such code into the common code base, but that it before always a review of at least one other team member.

Now imagine the case that happened exactly like this:

Our Azubi has written his code more or less “free schnapps”, similar to you described it. So his methods were also quite “thick” and the principle of singular steps in the methods he did not follow.

Now he wanted to check the code and needed a review.

The cryptologist no longer makes full reviews, and it is now too far from the code base. The other senior developer just said, “I’m not doing this” and left the task open. The team leader has held an external training. The front eagle could not make the review because it was a backend change.

So I did the review.

And as expected (he is still Azubi, there is still some puppy protection) his code has not worked. But I couldn’t tell him what place the mistake was. I had to use file names, method names AND Line numbers work – just because the methods were so huge.

Spolier: He has not yet been able to fix his bugs because he himself has difficulty finding himself in his code. He’s about a strong refactoring at the moment.

As an Azubi – unsightly, because he has unnecessarily consumed time. If he had complied with the code guidelines, the problem would have been solved weeks ago. But as paid developer you can’t allow that too often or long.

So what are my ideas for improving the coding?

  • As a team leader also really use people who do the job want. Not only because of money or to profile and for heaven’s sake, no people from sales or marketing (I have to experience already). They don’t even speak the same language as the developers and testers.
  • The old ones (i.e. my littleness) simply have to show some foresight with the young. Unfortunately, not a few senior developers are mentally driven quite firmly in their patterns. You just have to jump over his shadow.
  • The boys should be aware that they are not the new superstar developers that the entire IT world has only waited for. It may help to make clear: The old sack that just wants to explain how the coding works has Origin of the Internet and with it Other and still working with DOS 5.0 – there was no Windows operating system yet!

Otherwise, my most important tip for everything is: GOODS. Never take anything as simple as given, but always ask: “GOODS Do you want me to do it that way? GOODS have these directives been introduced? GOODS is software developed today?” Execution of these topics – the will take you on.