6 ms·
Index, Count, Offset, Size
- card_zero 8mo agoI can't read the starts of any lines, the entire page is offset about 100 pixels to the left. :) Best viewed in Lynx?
- Flow 8mo agoLooks perfect here. iOS Safari
- dataflow 8mo agoIs there any other example of "length" meaning "byte length", or is it just Rust just being confusing? I've never seen this elsewhere. Offset is ordinarily just a difference of two indices. In a container I don't recall seeing it implicitly refer to byte offset.
- SabrinaJewson 8mo agoIn general in Rust, “length” refers to “count”. If you view strings as being sequences of Unicode scalar values, then it might seem odd that `str::len` counts bytes, but if you view strings as being a subset of byte slices it makes perfect sense that it gives the number of UTF-8 code units (and it is analoguous to, say, how Javascript uses `.length` to return the number of UTF-16 code units). So I think it depends on perspective.
- dataflow 8mo agoThat makes sense, I agree -- seems Rust is on board here too.
- AlotOfReading 8mo agoIt's the usual convention for systems programming languages and has been for decades, e.g. strlen() and std::string.length(). Byte length is also just more useful in many cases.
- dataflow 8mo agoNo, those are counts by definition, and byte lengths only by coincidence. Look at wcslen() and std::wstring::length().
- wyldfire 8mo agoA length could refer to lots of different units - elements, pages, sectors, blocks, N-aligned bytes, kbytes, characters, etc. Always good to qualify your identifiers with units IMO (or types that reflect units).
- zahlman 8mo ago> or is it just Rust just being confusing? It doesn't mean "byte length", so much as "byte" happens to be the element type. Unicode is conventionally represented as UTF-8, so the container can't be directly indexed to yield a character.
- wahern 8mo agoRelatedly, a survey of array nomenclature was performed for the ISO C committee when choosing the name of the new countof operator: https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3469.htm https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3469.htm It was originally proposed as lengthof, but the results of the public poll and the ambiguity convinced the committee to choose countof, instead.
- pansa2 8mo agoThe reason many languages prefer `length` to `count`, I think, is that the former is clearly a noun and the latter could be a verb. `length` feels like a simple property of a container whereas `count` could be an algorithm. `countof` removes the verb possibility - but that means that a preference for `countof` over `lengthof` isn't necessarily a preference for `count` over `length`.
- ncruces 8mo agoBut count is more clearly a dimensionless number of elements, and not a size measured in some unit (e.g. bytes).
- deleted 8mo ago[deleted]
- layer8 8mo agoI tend to use numFoos (short for “number of foos”), and only use fooCount when the variable is used for actual counting (like an errorCount variable that is incremented for each error). Countof is strange, because one doesn’t talk about the “count of something” in English, other than uses like “on the count of three” (or the “count of Monte Cristo” ;)).
- zahlman 8mo agoWhen I see "countof" I expect an operation that lets me filter the container and tell me the count of things that meet some condition (probably described with a unary predicate, but perhaps just an element to check for equality).
- zephen 8mo agoThe invariant of index < count, of course, only works when using Djikstra's half-open indexing standard, which seems to have a few very vocal detractors.
- GolDDranks 8mo agoFortunately only a few. Djikstra's is obviously the most reasonable system.
- zephen 8mo agoObviously to you and me, but you can see comments right here where others disagree. And the detractors certainly have momentum in certain segments on their side. Historically, of course, it was languages like Fortran and COBOL and even Smalltalk, but even today we have MATLAB, R, Lua, Mathematica, and julia. Big-endian won in network byte order, but lost the CPUs. One-based indexing won in mathematical computing so far, and lost main-stream languages so far, but the julia folks are trying to change that.
- tromp 8mo agoSee https://www.cs.utexas.edu/~EWD/transcriptions/EWD08xx/EWD831.html https://www.cs.utexas.edu/~EWD/transcriptions/EWD08xx/EWD831... for Dijkstra's thoughts on indexing.
- qouteall 8mo agoWith modern IDE and AI there is no need to save letters in identifier (unless too long). It should be "sizeInBytes" instead of "size". It should be "byteOffset" "elementOffset" instead of "offset".
- throwaway2027 8mo agoIsn't that more tokens though?
- pveierland 8mo agoWhen correctness is important I much prefer having strong types for most primitives, such that the name is focused on describing semantics of the use, and the type on how it is represented: struct FileNode { parent: NodeIndex<FileNode>, content_header_offset: ByteOffset, file_size: ByteCount, } Where `parent` can then only be used to index a container of `FileNode` values via the `std::ops::Index` trait. Strong typing of primitives also help prevent bugs like mixing up parameter ordering etc.
- akdor1154 8mo agoThe 'same length for complementary names' thing is great.
- JSR_FDED 8mo agoUsing the same length of related variable names is definitely a good thing. Just lining things up neatly helps spot bugs. It’s the one thing I don’t like about strict formatters, I can no longer use spaces to line things up.
- craig552uk 8mo agoI've never yet seen a linter option for assignment alignment, but would definitely use it if it were available
- ivanjermakov 8mo agoAlignConsecutiveAssignments in clang-format might be the right fit. https://clang.llvm.org/docs/ClangFormatStyleOptions.html https://clang.llvm.org/docs/ClangFormatStyleOptions.html
- skydhash 8mo agoI know prettier can isolate a code section from changes by adding comments. And I think others can too.
- userbinator 8mo agoOr learn an array language and never worry about indexing or naming ;-) Everything else looks disgustingly verbose once you get used to them.
- Fraterkes 8mo agoIs there any reason to not just switch to 1-based indexing if we could? Seems like 0-based indexing really exacerbates off-by-one errors without much benefit
- tialaramex 8mo agoI would bet that in the opposite circumstance you'd say the same thing: "Is there any reason to not just switch to 0-based indexing if we could? Seems like 1-based indexing really exacerbates off-by-one errors without much benefit" The problem is that humans make off-by-one errors and not that we're using the wrong indexing system.
- Fraterkes 8mo agoNo indexing system is perfect, but one can be better than another. Being able to do array[array.length()] to get the last item is more concise and less error prone than having to add -1 every time. Programming languages are filled with tiny design choices that don’t completely prevent mistakes (that would be impossible) but do make them less likely.
- adrian_b 8mo agoHaving to use something like array[length] to get the last element demonstrates a defect of that programming language. There are better programming languages, where you do not need to do what you say. Some languages, like Ada, have special array attributes for accessing the first and the last elements. Other languages, like Icon, allow the use of both non-negative indices and of negative indices, where non-negative indices access the array from its first element towards its last element, while negative indices access the array from its last element towards its first element. I consider that your solution, i.e. using array[length] instead of array[length-1], is much worse. While it scores a point for simplifying this particular expression, it loses points by making other expressions more complex. There are a lot of better programming languages than the few that due to historical accidents happen to be popular today. It is sad that the designers of most of the languages that attempt today to replace C and C++ have not done due diligence by studying the history of programming languages before designing a new programming language. Had they done that, they could have avoided repeating the same mistakes of the languages with which they want to compete.
- cb321 8mo agoAs @SkiFire correctly observes[^1], off-by-1 problems are more fundamental than 0-based or 1-based indices, but the latter still vary enough that some kind of discrimination is needed. For many years (decades?) now, I've been using "index" for 0-based and "number" for 1-based as in "column index" for a C/Python style [ix] vs. "column number" for a shell/awk/etc. style $1 $2. Not sure this is the best terminology, but it is nice to have something consistent. E.g., "offset" for 0-based indices means "off" and even the letter "o" in some case becomes "the zero of some range". So, "offset" might be better than "index" for 0-based. [^1]: https://news.ycombinator.com/item?id=47100056 https://news.ycombinator.com/item?id=47100056
- throwaway27448 8mo agoOrdinal is nice because it explicitly starts at 1.
- adrian_b 8mo agoNit pick: only in few human languages the ordinal numbers start at 1. In most modern languages, the ordinal numbers start at 2. In most old languages, and also in English, the ordinal numbers start at 3. The reason for this is the fact that ordinal numbers have been created only recently, a few thousand years ago. Before that time, there were special words only for certain positions of a sequence, i.e. for the first and for the last element and sometimes also for a few elements adjacent to those. In English, "first", "second" and "last", are not ordinal numbers, but they are used for the same purpose as ordinal numbers, though more accurately is to say that the ordinal numbers are used for the same purpose with these words, as the ordinal numbers were added later. The ancient Indo-European languages had a special word for the other element of a pair, i.e. the one that is not the first element of a pair. This word was used for what is now named "second". In late Latin, the original word that meant "the other of a pair" has been replaced with a word meaning "the following", which has been eventually also taken by English through French in the form of "second".
- MarkusQ 8mo agoMeta nit pick: You are conflating linguist's jargon with mathematician's jargon. In much the same way as physicists co-opted common words (e.g. "work" and "energy") to mean very specific things in technical contexts, both linguists and mathematicians gave "ordinal" a specific meaning in their respective domains. These meanings are similar but different, and your nit pick is mistakenly asserting that one of these has priority over the other. "Ordinal" in linguistics is a word for a class of words. The words being classified may be old, but the use of "ordinal" to denote them is a comparatively modern coinage, roughly contemporary with the mathematicians usage. Both come from non-technical language describing putting things in an "orderly" row (c.f. cognates such as "public order", "court order", etc.) which did not carry the load you are trying to place on them.
- navane 8mo agoI hoped to learn some more excel lookup tactics, alas
- donkeybeer 8mo agoI was thinking sql.
- jp1016 8mo agoBanning "length" from the codebase and splitting the concept into count vs size is one of those things that sounds pedantic until you've spent an hour debugging an off-by-one in serialization code where someone mixed up "number of elements" and "number of bytes." After that you become a true believer. The big-endian naming convention (source_index, target_index instead of index_source, index_target) is also interesting. It means related variables sort together lexicographically, which helps with grep and IDE autocomplete. Small thing but it adds up when you're reading unfamiliar code. One thing I'd add: this convention is especially valuable during code review. When every variable that represents a byte quantity ends in _size and every item count ends in _count, a reviewer can spot dimensional mismatches almost mechanically without having to load the full algorithm into their head.
- maleldil 8mo agoBig-endian naming is great. I've adopted it since I first read it about it in matklad's blog.
- layer8 8mo ago> big-endian naming I would call it “English naming” [0], it’s just more readable to start with, in an anglophone environment. [0] as opposed to “naming, English”, I suppose ;)
- Shish2k 8mo ago> When every variable that represents a byte quantity ends in _size and every item count ends in _count, a reviewer can spot dimensional mismatches almost mechanically At that point I'd rather make them separate data types, and have the compiler spot mismatches actually-mechanically o.o
- stephc_int13 8mo agoCould not approve more a I use near identical naming convention in my C codebase. Not using the standard C library to avoid inconsistencies and the awful naming habits of that era.
- kgwxd 8mo agoSo many arguments could have been avoided if the convention was to use o instead of i in c-like for loops.
- matheusmoreira 8mo agoReally like this. I'll follow this practice from now on.