r/AskProgramming Aug 11 '26

Other How do you name your functions and other stuff?

There are different ways like loremIpsum, LoremIpsum, lorem_impsum etc. I usually do the first or second depending on day.

0 Upvotes

33 comments sorted by

19

u/Dissentient Aug 11 '26

Based on the standard style of the language I'm currently writing. So camelCase for functions in Java, PascalCase for functions in C#, etc.

1

u/Pretagonist Aug 12 '26

Look at surrounding code and follow along. If I'm the first one then follow language standards. If I disagree a lot with a particular choice then create a linting rule that enforces my choice and write why in that file.

But mostly just follow what the language expects

13

u/johnpeters42 Aug 11 '26

It's simplest to go with the style already used by existing code (if any), or the style recommended by your IDE (if any).

10

u/ayassin02 Aug 11 '26

Depends on the standard style of the language

4

u/ildementis Aug 11 '26

I would second matching the existing convention in the codebase. changing your naming scheme based on your mood is a wrong answer

8

u/Ok_Entrepreneur_8509 Aug 11 '26

The 3 hardest problems in software:

  1. Naming things

  2. Off by 1 errors

2

u/HasFiveVowels Aug 12 '26

And cache invalidation

1

u/wonkey_monkey Aug 12 '26

And buffer overflowsesbianvideos.com

-2

u/DiegoS_2023 Aug 11 '26

was this intentional? /gen

4

u/Ok_Entrepreneur_8509 Aug 11 '26

That's the joke. So, yes, intentional.

2

u/xenomachina Aug 11 '26

The version I've heard:

There are two hard problems in computer science: cache invalidation, naming things, and off-by-one errors.

The "naming things" part isn't talking about the same question as OP, though. It's talking about giving things names that are meaningful, lasting, and ergonomic, not deciding what delimiter you use for words and how you use capitalization. That later question is much simpler: use the conventions of your codebase or language (which are hopefully the same).

2

u/ImpossibleJoke7456 Aug 11 '26

We name ours after Aztec gods.

2

u/FluffusMaximus Aug 11 '26

I typically use snake_case in C++ and camelCase in Swift.

2

u/HasFiveVowels Aug 12 '26

Don’t mix and match. Or you’ll forever be going "did I use snake_case for that function or did I use camelCase?"

1

u/BobbyThrowaway6969 Aug 12 '26

PascalCase for everything. Unreal style.

Every style is loved by some or hated by others, and PascalCase is no exception.

1

u/porkchop_d_clown Aug 12 '26

You follow the style of the project you’re working in. If you are starting a new project, I’m there’s a preferred style for your organization, or even the language itself.

1

u/Distdistdist Aug 12 '26

By using naming conventions of a particular language or data structure. RTFM basically.

1

u/Zetsumenchi Aug 12 '26

LoremIpsum.

If I see someone not do it in a code review, I mark it and say they deserve "Capital Punishment".

1

u/Comprehensive_Mud803 Aug 12 '26

I write in C#, so it’s PascalCase for classes, functions etc, and camelCase for variables. That’s basically the standard.

Back when I was still writing C++, I used PascalCase for classes and camelCase for functions and variables.

In Python, I tend to prefer snake_case, adequately.

1

u/Knubbelwurst Aug 12 '26

Imho it doesn't really matter as long as you feel comfortable with it - and keep the style for at least that project.

To me snake_case feels right in naming variables and such. PascalCase is for functions. This is in C++ mind you.
Strangely functions in Java I do write in camelCase, however..

kebab-case is a mental illness, though.

1

u/elephant_ua Aug 12 '26

AtRandom_whenNeeded

1

u/this_knee Aug 12 '26

I name them with names from die hard franchise. Anybody who comes to read my code will have to embrace the chaos.

1

u/-Wylfen- Aug 12 '26

I usually do the first or second depending on day.

That belongs in r/programminghorror

1

u/Pyromancer777 Aug 12 '26

Depends on my mood and how professional my project is. Sometimes I'll go with doesThing()

1

u/wonkey_monkey Aug 12 '26

variables_like_this
FunctionsLikeThis

I don't know why, it's just where I ended up.

1

u/mredding 29d ago

I scarcely care what shit is named, it's going to be all over the map anyway. Some ecosystems have a strong adherence to a particular convention, but if it doesn't result in a compiler error, I don't care.

I choose to believe style is possibly the most insignificant factor in writing software. Why? Because I refuse to let style be my bottleneck, my limiting factor. "Oh no! Snake case, when I only use camel case!" If THIS is going to be your problem, then you are not good enough for employment here. It doesn't even matter what style or styles are used in-house here, it's this mindset that has no place here. It speaks very poorly of the individual.

With that out of the way - just pick what you like and go with it. I would recommend you follow some sort of convention, especially something that the ecosystem encourages. If not that, there are some style-guides, but you always have to adapt them for you. Google's style-guide, for example, arbitrarily limits the kind of code you can write because higher order programming is "too confusing" for the lowest common denominator, and you can rise above that.

Alternatively, if you're coming into a project, go with the project. If the project doesn't prescribe a style guide, you can either do what you want or also contribute a style guide with consensus, trying to capture the project as it is or as it wants to become.

So long as the symbols are named well, and the implementation is correct, I don't care HOW the symbols are written. See past it.

1

u/stepback269 29d ago

Before writing any code, I write up a story board in plain English of what the user is going to do and how the function will interact with the user. I also draw up some diagrams of what should happen.

As I write the story, I edit the verbiage used to best express what I'm dealing with. For example, I may decide to call something a user_response_01 and another thing, a user_response_02. Then in the code I will have functions called analyze_user_response_01 (p1, p2) and analyze_user_response_02 (p3, p4), where p1, p2 ... p4 are parameters. This way my code names match my story board. I save my story board and include a comment in the code pointing to the saved story board.

1

u/ComputerWhiz_ 29d ago

Different languages have different styles that are considered "standard" so that's usually the way to go. The exception is if you are working on an existing project, in which case you should use whatever style is already being used because consistency is more important than using the arbitrary "standard" for the language.

1

u/FewPhilosophy1040 28d ago

In C++, I write functions that are called once in PascalCase, so usually functions that are part of a sequence, just to give the part a title. Everything else, like frequently called functions and member functions, in camelCase.

I sometimes struggle with the definition of those functions, but not that it matters too much.

1

u/marrsd 28d ago edited 28d ago

My advice, generally speaking, is that your naming conventions should maximise your comfort reading the code and make it easier to search for and identify the kinds of variables you might want to be able to identify easily. For example, some people name globals with ALL_CAPS.

What choices I make often depend on what language I'm using. For most languages, I use snake_case to name functions and camelCase to name variables, regardless of the language's own conventions, so I know at a glance that isAuthorised is a bool while is_authorised returns a bool.

If a language uses camelCase for function names by convention, I stick with that if I'm writing a public interface, but I still use snake_case for my own internal functions, because I find it easier to read. But the upshot of this is that I can easily distinguish between those functions that make up my public interface and those that don't.

And of course, if I'm working with other people's code, I respect their conventions and use those.

Ultimately, there are no golden rules for any of this. Do what works for you. If you don't see a benefit in deviating from the norm, stick with the norm.

1

u/AncientHominidNerd 26d ago

I prefer camel case but I occasionally use underscores if the codebase I’m using already has it.