[ home / rules / faq / search ] [ overboard / sfw / alt ] [ leftypol / edu / labor / siberia / lgbt / latam / hobby / tech / games / anime / music / draw / AKM ] [ meta ] [ wiki / shop / tv / tiktok / twitter / patreon ] [ GET / ref / marx / booru ]

/tech/ - Technology

"Technology reveals the active relation of man to nature" - Karl Marx
Name
Options
Subject
Comment
Flag
File
Embed
Password(For file deletion.)

Not reporting is bourgeois


File: 1756309952917-0.png (323.89 KB, 677x453, ClipboardImage.png)

File: 1756309952918-1.png (405.14 KB, 1100x688, ClipboardImage.png)

 

How do you go about choosing variable names?
To me this seems to be the atom of abstraction.
So there should be a lot to talk about if we can overcome the scale.

Was recently reading some about DDD [^1] the basic idea being as follows:
- Use ubiquitous language: the language used in the business domain, in coordination with experts
- Divide the language into contexts which assign a unique meaning to each word.
Typically this means division into microservices or separate programs, but it might work okay with modular monoliths.

At a more basic level there is the division of variable names grammatically. [^2]
- transitive verb with direct object: function
- linking verb with predicative adjective: predicate
- transitive verb: method

- singular noun-phrase: non-boolean datum
- predicative adjective: boolean datum

- plural: when multiple entities
- abbreviation: when scope is small

The idea with both of these is to reuse existing language processing for programming.

Anyone have any refinements?

:[^1] https://www.thoughtworks.com/insights/blog/domain-driven-design-neednt-be-hard-heres-how-start
;[^2] https://dev.to/somedood/a-grammar-based-naming-convention-13jf

Hungarian notation is underated

>>30995
There's an interesting occurrence when renaming variables in scope.
We tend to name variables teleologically based on use rather than identity.
So we might have multiplier and multiplicand rather than a integer representing something.
This is actually somewhat poetic, as if using a stick as a sword made it so.

>>30997
Is this mostly for dynamic languages?

>>31000
Mostly C++ in practice
Now that you mention it though

Acceptable variable names:
i, j, k, n, m for integers
s, p for strings/pointers
foo, bar, foobar if management tracks performance by characters

>>30999
This isn't exactly teleological because the "purpose" keeps changing.

>>30995
Anyone else think that the DDD example in the second picture doesn't actually use ubiquitous language? The .*Repository parameters in particular, (but also perhaps the notion of a shoppingCartId as opposed to an articleId which presumably exists, if it's an article of clothing). Wonder if this would be a good reason to choose ActiveRecord over the DataMapper pattern.

>>30995
>Typically this means division into microservices or separate programs, but it might work okay with modular monoliths.
If this would work with microservices or separate programs it would work with modular monoliths just by utilizing nested namespaces, which are equivalent for these purposes.

Guess determining namespaces isn't too far off topic of naming, it's often just a dot.
One approach to DDD is this idea that there should be "a thin service layer and a rich domain model". [^1][^2]
It typically involves more layering, but really find this rather confusing.
Sort of made progress in this direction in a few applications where all the validation etc. was inside of the domain model.
It's also immediately apparent that a traditional ORM with this technique are rather incompatible with functional core.
Overall don't know where to start when it comes conceptualizing the separating out of modules.

:[^1] https://medium.com/@inzuael/anemic-domain-model-vs-rich-domain-model-78752b46098f
:[^2] https://martinfowler.com/bliki/AnemicDomainModel.html

>>31015
Wonder if a rich functional service layer and a thin mutable domain model would be better? The downside seems to be bad locality of behavior, but better testability. Seems the one is far more important than the other, right? Assuming that the domain model must be mutable…

>>31018
Hmmm
This is a bit oldschool but it'll save you a fair bit of work later, get some paper and a pen and sketch out your architecture for your project?

>>30995
> How do you go about choosing variable names?
Honestly, I go 99% just by vibes.
The only rules I follow are:
- Try too append "s" to the collections
- Prefix "has" or "is" to bools
And that's kinda it.
My only real strategy to improve undesirability is to break down the program a lot, in functions and namespaces. Also I try to keep the functions pure, if I can.
>>30997
Maybe in C++ and Java where it can not be exactly clear if you're referencing a class-wide or local variable. Tbh I just use this or the equivalent most of the time.

I haven't made anything big enough to need a convention yet. Could make a game so I'll have to.

>>31022
With vibes we all enter the world, and as vibes we will all return.

>>31020
>>31026
Think my applications are generally to small to benefit from functional shell imperative core. If all you're doing is serializing input with validation at the domain model and rendering there's really no place for it.

>>31022
Agree with pluralising lists and prefixing has/is on bools.

I will add that "count" can be a useful word for integers, e.g. where a list of objects representing different houses might be named "houses" if I wanted to have a function that took the length of that list as a parameter I might name that parameter "house_count" or "houseCount" depending on convention.

>>31003
>i, j, k, n, m for integers
only 5d unit vectors? (scoffs) amateur

>>30996
Think found an example of functional core, imperative shell in my ~/Software directory, metainfo.py in my torrent client.
Basically this object which maintains the overall state of the torrent (excluding the message handlers) is initialized by a set of twelve static method calls.
However, theoretically the object could be recreated every time its state would otherwise need to change and it could be frozen.

It still reads from disk and the network which means that it's not easily tested.
Wonder how the network and filesystem aware parts of the protocol could be implemented in such a way as to separate out the actual IO?
For example how do you test that a piece is properly received without performing the IO to write and receive a piece when there's state to be maintained for the connection etc.?
Should think some about a sans-io [^1] torrent client guess.

:[^1] https://sans-io.readthedocs.io/how-to-sans-io.html#how-to-write-i-o-free-protocol-implementations

>>31045
For reals, when you deconstruct a list, are the names
- h t (head, tail)
- a d (car, cdr)
- v vs (or a as, x xs, etc.)
or something else?

>>31053
Think prefer "x : xs" of these even though it's less descriptive than the other two (what is "x"?), it generalizes to arbitrary collections, and iterators better, and when used generically allows for more description, for example "for file in files".

>>31053
Looking at my imageboard arsvia's code it seems like there's room to pull validation logic into the data model and to factor out this functionality into static methods for testability. This could be done using SQLAlchemy validators [^1] or custom initializers.

:[^1] https://docs.sqlalchemy.org/en/20/orm/mapped_attributes.html#simple-validators

>>31056
>pull validation logic into the data model and to factor out this functionality into static methods for testability
This is actually really cool to me! Arsvia could have the service layer virtually deleted. The entire program can be written as rendering and object initialization/mutation with static IO free methods handling the bulk of init. There's probably some errors in the attached image, but the idea was applied to a scratch buffer with the image object. The init method acts as a imperative shell for the functional core of the static private methods.

>>31057
Obviously the stupid calls to the validation logic in __get_mime should be validators for the size and mime.

File: 1756414049875.png (42.13 KB, 520x520, ClipboardImage.png)

I try to use descriptive names for functions, global, records and long-lived state variables in an ugly main function. Otherwise i try to avoid long variable names, because they clutter the code and relying on them means your code isn't clear enough on its own. The same for comments, if you need to explain the minutiae of your code, you better have good reason to!
>>31003
This, except i use p-w for pointers and occasionally c-e, h, l-m and ll as pseudo hungarian warts.


Unique IPs: 9

[Return][Go to top] [Catalog] | [Home][Post a Reply]
Delete Post [ ]
[ home / rules / faq / search ] [ overboard / sfw / alt ] [ leftypol / edu / labor / siberia / lgbt / latam / hobby / tech / games / anime / music / draw / AKM ] [ meta ] [ wiki / shop / tv / tiktok / twitter / patreon ] [ GET / ref / marx / booru ]