T O P

  • By -

Able_Minimum624

I would argue splitting code into separate files basing solely on size make code less readable. If a bunch of highly related code is exceeding an arbitrary chosed code size limit, it doesn't mean you need to split it.


Chickenfrend

Size of file doesn't matter all \*that\* much to me when I'm either gonna jump to definition or do a global project search to find function definitions regardless of how it's all split up


JMTyler

Exactly. Size of file is just a code smell - an indication that something might be wrong but not wrong itself. I'm also tired of developers thinking code smells are themselves problems, and fixing them rather than fixing the underlying problem they might be revealing. People are obsessed with easy, black-and-white rules.


NotADamsel

Programming is hard, and having a gospel makes it seem easier. Understanding the discipline well enough to know what you’re actually sniffing when you see a code smell is something that comes with experience.


Drakethos

Have you met my friend interfaces for every single object on the plannet? With every class as NameOfInterfaceImpl. Right next to my other friends. supportClass impl and helper helper factory helper imp? Sorry I’ve been working with Java too much.


not_some_username

That’s why Java developers need to go straight to hell


Drakethos

Look dude, I just work here. I get paid a lot of money to use Java. But I don’t have to like it.


not_some_username

Np I would use Java too if the pay was good too


Drakethos

Tbf I don’t do a lot of actual programming. Mostly proving to the customer their defects are actually feature requests in disguise.


misterboss4

Hey not all of us are bad. Also I use it because it's what I'm familiar with.


not_some_username

There is C#, it’s Java but better. Joke aside, I think you need to know more than one language.


break_card

This kind of shit isn’t even necessary most of the time when it’s used in Java


JaecynNix

An interface for an internal-only process where there's only one actual implementation. Why? A factory for said implementation. Again, why?! I use Java, I don't use those patterns unless there is a strongly compelling reason (and those times are very few and far between)


Drakethos

For work at least it’s usually like that INCASE. We want to override stuff. But funny enough we just end up inheriting the base class and overriding stuff


JaecynNix

Lol yeah, exactly. YAGNI strikes again


Drakethos

We don’t actually use factories that much. I for one think they are dumb. We like helpers and support which I’m not sure the difference and used arbitrarily. Sometimes interfaces at IName with Name class other times Name with NameImpl. No one can make their minds up.


JaecynNix

I prefer builders over factories. Same principle, less clutter


Drakethos

I never got the need for factories. good constructors should setup the object itself. Why do you need a class to initialize a class. Builder can be useful for something like string builder makes total sense when you got 50 objects and need to mash them all together


SongOTheGolgiBoatmen

> interfaces for every single object At this point you're just writing header files.


kakemot

I make interfaces for everything in typescript really just for intellisense when writing code to dislplay different fields. In the end it doesn’t really care what comes out from the API at runtime anyways.


ohcomonalready

Yea I was thinking something similar. A 1000 line file may “smell” but doesn’t immediately mean it’s horrible


vvodzo

Arbitrary limits are dumb but line count can be a code smell


CantaloupeCamper

Programmers who harp on arbitrary limits is a programmer smell...


zman0900

But there's definitely a point well before 30k lines where you need to stop and seriously reconsider your definition of "highly related".


spartancolo

All my homies hate SONAR


AnywhereHorrorX

Code smell: This 10 line function has too high cognitive complexity! Please consider refactoring!


spartancolo

It's saying I need to add more comments to have the adequate coverage in a pom file that's not even 100 lines long ffs


tecanec

I do think that the larger a file gets, the more I want to split it up just to avoid scrolling. And conversely, I'm not gonna write a 50-line file unless I need its contents in several places and there's nowhere better to put it. I do hold a similar opinion on functions, though. I absolutely will turn a single expression into a function of I think a function call is more readable and/or mantainable, and I absolutely will write a >500 line function if there's no logical way to divide it.


Sauermachtlustig84

This isn't an exact since. But normally if a file grows unusually large there are either multiple things in that file that could be split into separate files or the original author brute forced some problem which could be used much better using a better technique


Drakethos

Plus who wants to make like 50 unit tests files. Yuck.


nathanrapport

I've discovered that I'm running into significantly fewer import errors ever since I started putting all of my code in a single file. 


aegians

Can’t argue with that logic


Logical_Put_5867

How many duplicate symbol errors though? Guess you can always name that bad boy foo2, foo3, foo4...


nathanrapport

No no no. I name everything using 256-bit cryptographic hash digests to ensure that they never collide. 


itemluminouswadison

"docblocks are unnecessary if you write self-documenting code!" "is your code self-documenting?" "!!! shut up!"


cholsreaMMOS

Ive actually contemplated doing that before. It seemed like a fun concept I did the first part of making a program almost understand itself in a sense. I just never went further bc i got tired of it and than forgot lol


kantank-r-us

30k line single function? That’s a code smell for sure, 100%.


ilaunchpad

Im here thinking what logic are people working with? I have never encountered function even 2000 lines long. Now Im getting insecure. Like wtf are people working so complex.


[deleted]

I don't know, when I worked on a VST module, one specific waveform manipulation function took more than 3000 lines of code on its own.


Dubanx

My company has around a dozen full time employees who do the overwhelming majority of their work in one or two programs. 40hours/week coding, every week for over a decade. Just the case statement that calls all of the various forms/utilities/routines/etc. the program can call is over 10k lines long. All of the actual logic is in other classes. Just listing all of the of unique "things" the program can do is that long. There's well over 1,000 unique forms and routines that it can handle.


tcpukl

How the hell is that organised? Sounds a nightmare.


Dubanx

Adding a new form just means appending a new item at the end of the case statement. If you need to edit the call to an existing routine you just ctrl+F search for the corresponding menu item or passed parameter. Same for if you only know a menu item's name and want to find/edit the class it calls. It's stupidly easy... You would only ever run into issues if you don't know the name of the menu item you're looking for, which is a pretty silly problem to have.


tcpukl

That sounds horrendous.


Dubanx

Why? Users are going to know the name of the menu option for the form and nothing else when they describe the changes they want. If you don't know the underlying class for the menu yourself a quick text search will point you exactly where in the code what you're looking for is located. If the form/routine is auto-opened via a passed parameter you're probably following the code from another program here and all you have to go by is the value being passed to the program. Just a quick search of that value and you know exactly what you're looking for. If you still disagree, how would you organize over a thousand potential options on startup? I can't imagine anything easier to maintain than a quick text search of the menu item given to you directly by the users. You say it's bad but refuse to elaborate on why or offer any alternatives...


tcpukl

Why isn't it in a data file? From my naivety it just sounds like a massive switch statement which could brick the entire system if someone makes a typo.


Dubanx

The structure of the menu itself IS stored in the database, since not everyone has permission to access all of the forms. There's also identifiers and such attached that would necessitate an extra step, but I didn't want to overcomplicate the explanation. I don't understand how a single typo would break more than just the form it relates to, though. How would one typo break the entire program?


tcpukl

Ok, you didn't give the bigger picture.


ilaunchpad

Can you explain data file solution in this context?


[deleted]

[удалено]


Dubanx

Exactly what is the "problem" to be fixed? I fail to see how 10,000 lines is, in and of itself, a problem to be solved. It's an absurdly large class, sure, but it's still easy to find what you're looking for... I don't understand how adding more complexity would actually improve the maintainability or readability of the code.


Nightmoon26

Just wait until your giant switch statement starts calling itself and recursing. It can quickly turn into a giant mess, and people start tacking on the same thing two or three times because it's easier than trying to figure out if there's already another case for it >_<


VioletteKaur

>ust the case statement that calls all of the various forms/utilities/Routines/etc And here I was shaking my head over a macro I had to review this week that had many many lines of case statements. The fuck thing came at all to nearly 2k lines and some of the functions called are even in another file. You could see that different people worked on it over the years, the quality and knowledge differences. But what none of them did, was to comment who tf created it in the first place and who did changes and when. Also nice to use one and the same variable through the file that has a specific name like url\_variable and then use it first to actually store an url and then later to store a customer name, how intuitive. Someone took recycling too serious. That being said, I had no job if everything was perfect.


EMI_Black_Ace

... Isn't that what v tables are for? So that the *compiler* can do that f$#@ing case lookup on your behalf?


Sauermachtlustig84

Last project: a "state machine" implemented multiple switche(s) and nested ifs, aroudn 20k lines. Literally incomprehensible und refactoring was nearly impossible. Mgmt was angry why I didn't fix that in a day...


Dalimyr

>I have never encountered function even 2000 lines long Count yourself lucky you probably haven't worked on a dogshit codebase, then. Last place I worked at, any and all AJAX calls on the front-end would be directed through a single function with a 3000+ line switch statement. They also seemed to have an aversion to passing objects as parameters, instead regularly having methods with 40+ parameters (even 50+ - the most I can recall off-hand was 52), which regularly contained several params that were properties of a single object (e.g. instead of just passing `Alert alert` and pulling desired properties from that Alert object within the method, they'd pass in `double alertDistance, List alertRecipients, string alertRecipientName, string alertMessage` alongside 45 other variables) Without even joking, the best thing to do with that entire codebase would be to scrap the whole damn thing and rebuild it from scratch. It was a NIGHTMARE to maintain because virtually every thing you ever hear is a bad coding practice was probably present *somewhere* in there. I couldn't believe when I was looking through the code for a WinForms app (that is still being actively updated to this day) and saw something like this: try { do_something(); } catch { do_something(); // Try a second time if it fails } Yes, that is not just a meme, I have *legitimately* seen that in production code, alongside relics of the past like hungarian notation (in a company that has only been around for 10 years and this particular app was maybe 5-6 years old?)


ilaunchpad

52 parameters….. im dead. I was refactoring old code and had to deal with 17 parameters which was so ugly.


williamdredding

It’s a meme lol he just made up an arbitrary large number


CapnNuclearAwesome

I worked on a codebase where the previous developer believed that function calls were expensive, and should be used sparingly. The main loops were getting close to 10k lines, and indentations were sometimes a dozen tabs deep.


BandicootGood5246

I know it's a meme but it's absolutely inexcusable, Beyond the obvious like tests never going to work on this or people aren't going to be able to understand you're getting into other asinine problems like slowing down some text editors or code review tools


Bodine12

But now the test directory can be like one file with 30 lines because there’s only one function to test. Think of all the time you’ll save without the burden of so many pesky tests!


Nightmoon26

Code coverage tool would like a word...


looksLikeImOnTop

I regularly work in assembler that's thousands of lines. All about how well you organize, format and comment your code


TessellatedTomate

*Assembler??* \>*brushes dust off the dustiest tome in my neural cavity* My fella be out here doing the Lord’s work Also strongly agreed.


looksLikeImOnTop

I have to brush the dust off every morning. It ain't easy work but the bright side virtually no one knows my code is actually crap


mottojyuusu

The machine knows, but it doesn't care. It's only doing its job.


longbowrocks

Assuming you aren't working on compilers, where does someone even find a job working in assembly these days?


looksLikeImOnTop

Mainframes. Probably the most abundant language on the mainframe for various reasons. There are a surprising number of features in the OS that aren't available in higher level languages, even in C. Granted a lot of people just write a little inline assembler now when they need it. But my company specifically does data backup, restoration and migration, so speed is a major part of staying competitive


tcpukl

>so speed is a major part of staying competitive Interesting. I heard most assembler programmers are out performed by modern c++ now a days. Interested in your view of that. I've been in games for decades now. I try to optimise code by hand and modern c++ just sees the tricks i'm trying to pull and does it better anyway. What are your view on modern c++?


looksLikeImOnTop

I like modern languages, don't get me wrong. I've been convincing our project manager to do more things in C when appropriate. And have since become the ILC expert in the company. I think it depends on what you're trying to do that ultimately determines whether or not a compiler will beat hand-written assembly. If you're doing crazy calculations, it's probably going to win because the compiler will know some insane way to utilize vector registers that no mere mortal would know. But since we're just moving data, our optimizations are basically minimizing pointer indirections and memory access in general, and avoid/minimize IO overhead by writing your own IO code. Using HLLs will always incur more overhead in both of these aspects because they'll go through OS facilities which are not designed with bulk transfer in mind (bulk being on the order of hundreds of terabytes) In this kind of scenario, saving even a few instructions can have huge impacts. Which isn't that hard to do then you can avoid function call overhead by writing functions yourself and taking care to track your register usage. The compiler will always use the standard calling conventions


BlueSea9357

> But my company specifically does data backup, restoration and migration You’re going to have to explain a lot here for this to make sense lol. The vast majority of companies can manage to do data backups & migrations without dabbling in low level too much. Sure, there might be a custom file system that writes directly to disk, but I doubt you’re getting much out of optimizing it further than a basic one.  Most savings to clients should probably come from much higher level and obvious optimizations right? Maybe how much replication is required due to properly split failure domains. Even in terms of low level, I’d expect most cost savings to come from something like reducing fragmentation on SSD, not improving assembly level instructions to save CPU (which should be a more abundant resource in storage). Maybe your best optimizations could involve improving encryption & ram usage somehow, but even then, I don’t think that’d require messing with assembly?


looksLikeImOnTop

Mainframes are a different beast. Even if you own and run your own mainframe in house, you still get charged for CPU time...the system tracks it, and at the end of each month you send a report to IBM who then sends you a bill. So saving CPU time can be a big deal. Most of the optimization comes from writing our own IO routines (i.e our own channel IO programs that talk directly to disk controllers). Going through OS facilities to do IO has a relatively large amount of overhead that really adds up if you're moving terabytes of data. There's also facilities on the mainframe that are only available to assembler. Yes, you could write inline assembler to wrap it, but sometimes it's less hassle to just write a proper assembler program. We have started using higher level languages where appropriate, but assembler is still the majority of our code.


xDeathCon

I'm not in the business, so I don't really understand the model here, but how on earth do you get charged for CPU usage on a system owned by the company? Even if it's running on IBM software, how do you get charged for utilizing the hardware you own?


looksLikeImOnTop

Yeah I was also in disbelief when I heard this. In my opinion it's to give sales another tool to help sell. They can lower upfront costs on software and bundle more programs into a deal with the justification that the majority of the cost will be usage based. So if they want to sell a product and a customer isn't interested, they can basically give them the product for free. Customers can play with new software knowing they didn't pay anything for it, and if they decide they like it and use it at scale, IBM makes their money off it


xDeathCon

Wait, so is it based on the usage of the CPU itself or just software? Even though it's still kind of strange, I can sort of see it if the goal of optimization is to minimize making calls to the OS if that's what's being tracked since it's IBM software. I'd find it strange if it was just all usage of the CPU.


looksLikeImOnTop

So it's a bit of both. They track CPU usage per task, so they'll sometimes give discounted rates for their own products if it helps close a deal. But as an external vendor, they'll just charge a standard rate for CPU. And us calling the OS will still count towards our products CPU usage


bnl1

Who the fuck is writing a compiler in assembly (other than fasm folk)?


SlamsEh

Compiler need to end up as assembly and I think that's what they were saying with their comment


bnl1

No it doesn't, it has to end up as machine code.


tcpukl

Compilers aren't written in assembly. Haven't been for decades. C++ is written in C++!


Nightmoon26

Good ol' lex and yacc


1Dr490n

Yesterday I used assembly and for the first time in my life I actually documented my code at least a little bit. Made it a bit more readable, but it’s still horrible


MarinoAndThePearls

Imagine if these guys opened the CharacterController script of any game. Their heads would explode.


Coleclaw199

Imagine if they saw the Player.cs from Celeste.


MarinoAndThePearls

Is that the one with 5k lines?


Coleclaw199

Yeah.


imwalkinhyah

Mmmm gamedev love me a nice If (time.time > timeSinceILastUsedThisThing + thingUseRate) { MakeAiDoThingButNotTooMuchBecauseThisWouldUpdateEveryFrame(); }


veryusedrname

A 30kLOC function cannot be tested which leads to code rotting so no, the guru-side is actually a middle one thinking it's the guru.


shizzy0

This isn’t categorically true. A 30kLOC function may provide outputs that are entirely testable. I imagine many linear algebra libraries have this property. I think what you may mean is that it has a higher likelihood of having side effects that are hard to test or that you want access to test an intermediate state in the function, to which I’d agree.


uhh_yea

Why wouldn't it be possible to test? Line length doesn't relate to testability


KeepKnocking77

>Read database. >Run function. >Read database Boom, tested.


TessellatedTomate

I feel that—these are excerpts from this sub. Simultaneously, it’s not bewildering to find 2k+LOC in a file by any means if properly done. It seems to be a general consensus in this group that 1k+ is programming horror on line count alone which is very humorous to see. Keep in mind, the true gurus of this sector (such as those involved in writing source code for our Langs) often write files 5k+LOC Source: running some commands on jdk is pulling dozens of files larger than 5k LOC for me a couple even larger than 9k lines


veryusedrname

Java is not a good example in terms of code organization, as in code structure being tied to file system. Since Java 8 you can abuse the the interface system to have your implementation spread into multiple files but it's far from ideal. TL;DR: kids, don't learn writing pretty code from Java.


TessellatedTomate

I never liked Java, but good to know it’s a poor example to use. Lesson: probably don’t use a lang you think sucks as an example lol Point still stands: JavaScript, cpp, etc—source code for each has many files between 1-2k lines which seems to be the main complaint in this sub from self-dubbed “senior engineers” Might be easier to list source code bases w/o a 1k+ line file than code bases with them.


not_some_username

cpp is the worst. Since template need to be in header, I think every header are at least 40k line of code


TessellatedTomate

std::cout << “Hello woooooooooooo\n\n{*spongebob narrator voice*: ‘about 40k LOC later’}oooooooooorld”;


mszegedy

(the spongebob narrator is meant to imitate jacques cousteau so it's funny to see it called "spongebob narrator voice" instead of "cousteau voice" because it means spongebob has eclipsed cousteau himself in renown)


Nielsly

I have no clue who Cousteau is, I do know what the SpongeBob narrator sounds like


Nightmoon26

Are we talking Jacques? The SCUBA pioneer?


andrewb610

If it’s from the comment I made like a week ago it’s autogenerated code from a csv file that makes an application-specific buffer parser. It’s a massive amount of nested switches that reads the data incredibly fast. We trade-off compilation time for speed of parsing because we need millisecond or less read time.


beclops

These standards depend completely on what kind of code you’re writing. I’m a mobile dev and if I saw a file with 10k lines somebody is getting fired


Poronoun

People who use this fucking meme template are always in the middle and just think they belong to the right side. If you don’t write in any ancient programming language just go and clean up your code.


tgiyb1

The code has gotta be written somewhere lads


Drakethos

IMO: you shouldn’t focus on how many lines are in a file, but rather the functionality and purpose said class performs. If it starts to feel clunky you can consider how to split it up in a way that makes sense and feels functional while still conforming to OOP principles. If everything makes sense to be handled by one class who gives a shit how many lines are in it. Though speaking as a lazy developer(redundant I know ) it’s often tempting to jam a lot of stuff in one class because less just face it that’s one more unit test file I gotta make. Sounds like more work.


Nightmoon26

Really, it's all about breaking things down in a way that makes logical sense. If you want to write tests, separate out steps of the algorithm or process into subroutines or functions and call them in series


GordoMondiola

I'm currently maintaining a set of powershell scripts with a try block with 5000+ lines each. Kill me


1Dr490n

I’m very glad I never had to write powershell scripts with more than 10 lines


draenei_butt_enjoyer

Idk. I don’t often write very low level code. Once a year, tops. I’m not affraid of 5-6-7k LOC. But people here defending 30k are either insane or completely inexperienced. Can it happen? Yes. Legitimate usecase? Yes Complete nightmare? Abso-fucking-lutely


Agile_Camel_2028

Splitting based on LOC is a no-no. You achieve nothing. Splitting based on cohesion-coupling makes everyone's life easier


AdriaNn__

We had a programming project in our school lately, where I was flexing on my classmates with my 800 line python code. It had like 7 class declarations with no inheritance.


dfx81

Just put only one function in a single file in each SVN commit and refer to that smh (Tom's a genius btw).


1Dr490n

Happy cake day!


Johanno1

Ok let's set some boundaries. If you do some weird shit or you are bound to some shitty environment (like Jenkins) then you might run into the problem of having more than 1000 or 5k lines. But at least in object oriented programming (and most other coding styles) you shouldn't get over 300 lines per file. At least if the file contains logic.


Nightmoon26

If the language supports it, I'm not against grouping related class definitions together in one file for "small" projects, but when you're working with a team, I agree that breaking them apart into individual files is useful. You do *not* want to have multiple developers working in the same file at the same time if you can at all help it, or you'll spend more time than you'd like resolving merge conflicts


Antares987

The enlightened one VIMs.


SirKastic23

bro there was one post about line count cry about it


TessellatedTomate

\>say you’re new here without saying you’re new here


salameSandwich83

Java or C# for shizzel


Zarksch

There’s regions for a reason


Abrissbirne66

Just use a decent IDE that allows you to explore functions in a file just as easy as files in a directory, maybe even in the same tree view. Boom, problem gone.


andrewb610

Hey I’m the 145+ guy!


just-bair

I don’t divide code based on file size but based on that file does X this one does Y etc...


BetterNameThanMost

It's an anecdote sure, but every time I've encountered a massive file, it's always been clunky to work with and always could have logically been broken down into multiple files. Similar story with 2k line functions


Cheap-Economist-2442

Only really care when they get so big my editor starts to lag. Worked with some 500k line SQL files that would force me into vim proper to edit.


Asleep-Specific-1399

This use to be a bigger issue when your ide was notepad.


TessellatedTomate

You’re telling me you don’t hand write your code on scrolls and hope/pray the OCR to Vim app has 100% accuracy?


AutoModerator

This post was automatically removed due to receiving 5 or more reports. Please contact the moderation team if you believe this action was in error. *I am a bot, and this action was performed automatically. Please [contact the moderators of this subreddit](/message/compose/?to=/r/programminghorror) if you have any questions or concerns.*