On 01/06/18 16:38,
bart...@gmail.com wrote:
> On Friday, 1 June 2018 12:13:33 UTC+1, David Brown wrote:
>> On 01/06/18 12:20,
bart...@gmail.com wrote:
>
>>> Here's an example of related identifiers (also an example of
>>> single-letter identfiers):
>>>
>>> int64 evaliandexpr(void) { int64 x, y;
>>>
>>> x = evaleqexpr(); while (nextlx.symbol == iandsym) { lexm(); x &=
>>> evaleqexpr(); } return x; }
>>>
>>> Tell me what possible improvement in readability there can be by
>>> putting x and y into a managed struct
>
>> int64 evaliandexpr(void) { int64_t x = evaleqexpr(); while
>> (nextlx.symbol == iandsym) { lexm(); x &= evaleqexpr(); } return
>> x; }
>>
>> I'm happy with short identifier names in small contexts. I am not
>> impressed with short identifier names in a wider context (like
>> "lexm"),
>
> 'lexm' is a local function in this module, and it is clearly a
> function because it is followed by (). I don't see the problem.
It is clearly a function, yes. I'd be suspicious of such a short-named
function - even when it is local to a module (I hope you've remembered
"static" or an anonymous namespace). If it is local, commonly used, and
the name is obvious within the context of the program, then "lexm" might
be fine.
>
>> nor by jammedupidentifier styles - use camelCase or under_scores
>> to split up words.
>
> I mix and match styles just to break things up. Some names within a
> project will have underscores, some sets will have a common prefix,
> others a common suffix, and some both (see below). But all names in
> the same related set will use the same scheme
>
Using different styles for different purposes is fine, and can help
improve legibility. Some people prefer underscores, some prefer
camel-case - I'm not going to argue for one or the other. But jammed-up
identifiers without a break between the words, are always hard to read.
That makes them bad style in almost all circumstances.
>> And I can't see any advantage of putting the definition of "x"
>> before its initialisation.
>
> I have a problem with your version because the important
> initialisation of x is separated from the rest of the code.
No, it is not. The initialisation is right there with the rest of the code.
In your version, there is no initialisation of x - but its definition is
separated.
>
> I like to separate code that just provides information, like a
> declaration, from code that actualy does something, like the
> assigment.
Then you picked the wrong language. In C++, definitions (rather than
just declarations) /do/ something (even if, for simple types and a good
compiler, that "logical something" may involve no object code). And
"int64_t x" inside a function is a definition, not just a declaration.
> The algorithm here is to assign the first lhs term to x
> then loop for any rhs terms. Your version breaks that up.
No, it does not. It has the same actions in the same place. The only
difference is that "x" is not declared earlier.
>
> It also involves the type of x (and y; see below) more than I would
> like. If one day I decide to change their type, then I would need to
> change the block of code that expresses the algorithm.
No, you would not. You would only need to change it in one place at the
start of the block. And it would make no sense to change the type of
"x" unless you were also changing the type of evaleqexpr() - you need to
make multiple changes anyway.
And if you had several variables to deal with here, you would use a
"using" (modern C++) or "typedef" (older C++ and C) to name the type,
thus you would only have to change it at one point. (Your evaleqexpr()
function would use that same typedef'ed type.)
You can also use modern C++ style and use "auto" instead of the type for x.
> As I have it,
> I could copy and paste that block unchanged into a place where x and
> y are different types. Or even where they have no types at all (eg.
> to in a dynamic language where x and y don't even need declaring).
>
>
>> Certainly I see no advantage in putting the definition of "y" on
>> the same line as that of "x", when it is not used at all. Defining
>> your objects when you need them, and initialising them at the same
>> time, helps avoid the confusion and possible inefficiency of extra
>> objects like "y" in this example.
>
> This function is one of set of a dozen, all with the same interface,
> all named 'evalOPexpr', all declaring 'int64 x,y'. Most will use y,
> in one or two it was dispensed with (such as the one I chose to
> post), but I yet make use of it in the future.
If you define your variables when you need them, you don't need to
pre-define extra variables just to make copy-and-paste easier.
>
> These functions evaluate the operands of a binary operator and it is
> natural to call those operands x and y.
>
I am quite happy with the operands being called "x" and "y" - that seems
a good choice of names here.