|
I'm fairly dyslexic, gets much worse when I'm tired or too stressed. especially verbal to writing or reading out loud, it all gets jumbled up. sequences of more than 4 numbers I can't remember. As a result, I don't trust my memory.
So like you I don't like to spread my code all over the place, but I will still breakdown issues into smaller chucks, usually in the same file, so it's just a scrolling issue. if it's across multiple files then splitting the screen is an absolute must.
Since I don't have confidence in what was written, I test early and often whenever I can. but that also means I have way less issues in production than a lot of my peers. I also document my code, and when the language supports it add signature docs to each function, so when typing out code to connect to other code, the IDE will help me remember what params to pass or other relevant information.
My biggest issues are usually switching between languages and remembering how to do something when two different languages do things similar, but not quite. so C, C#, Java, JavaScript, Rust, D, kind of blur together event though not the same. Had the same issues with ADA, Basic, and SQL oddly enough.
|
|
|
|
|
Quote: you'd think I'd be better at making it accessible to other developers. Maybe hand out copies of this with your source: How to Quickly Understand the Code of a Function - Fluent C++
Cheers,
Mike Fidler
"I intend to live forever - so far, so good." Steven Wright
"I almost had a psychic girlfriend but she left me before we met." Also Steven Wright
"I'm addicted to placebos. I could quit, but it wouldn't matter." Steven Wright yet again.
|
|
|
|
|
i also like longer functions. like i say "when you find the responsible function you have already won".
high cognitive load is not the problem, but you should aim to improve the "difficult to decipher code" part.
|
|
|
|
|
Gem took pride losing thumbs up maybe (7)
"Life should not be a journey to the grave with the intention of arriving safely in a pretty and well-preserved body, but rather to skid in broadside in a cloud of smoke, thoroughly used up, totally worn out, and loudly proclaiming “Wow! What a Ride!" - Hunter S Thompson - RIP
|
|
|
|
|
PERIDOT - a gem
TO OK PRIDE
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
|
|
|
|
|
YAUT - well done
"Life should not be a journey to the grave with the intention of arriving safely in a pretty and well-preserved body, but rather to skid in broadside in a cloud of smoke, thoroughly used up, totally worn out, and loudly proclaiming “Wow! What a Ride!" - Hunter S Thompson - RIP
|
|
|
|
|
The first method below is probably 3e-9 seconds faster per call than the second method...
And a code reviewer asked that I used that syntax
private IMultipleComponentHandler SelectionHandler
{
get
{
if (m_selectionHandler == null)
{
var objects = SelectedObject;
<pre>
if (objects is IMultipleComponentHandler handler)
return m_selectionHandler = handler;
object[] collection;
if (objects is IEnumerable e
&& !objects.GetAttributes<IgnoreIEnumerableAttribute>().Any())
{
collection = e as object[] ?? e.Cast<object>().ToArray();
}
else if (objects != null)
{
collection = new[] { objects };
}
else
{
collection = Array.Empty<object>();
}
return m_selectionHandler = new InspectorMultipleComponentHandler(collection);
}
return m_selectionHandler;
}
}
private IMultipleComponentHandler m_selectionHandler;</pre>
but... that extra variable annoys me (var objects = SelectedObject; ), I see it as increasing code complexity for little benefit. I prefer that simpler version
private IMultipleComponentHandler SelectionHandler
{
get
{
if (m_selectionHandler == null)
{
if (SelectedObject is IMultipleComponentHandler handler)
return m_selectionHandler = handler;
object[] collection;
if (SelectedObject is IEnumerable e
&& !SelectedObject.GetAttributes<IgnoreIEnumerableAttribute>().Any())
{
collection = e as object[] ?? e.Cast<object>().ToArray();
}
else if (SelectedObject != null)
{
collection = new[] { SelectedObject };
}
else
{
collection = Array.Empty<object>();
}
return m_selectionHandler = new InspectorMultipleComponentHandler(collection);
}
return m_selectionHandler;
}
}
private IMultipleComponentHandler m_selectionHandler;
What says you?
For the record this is in a view model, this code is absolutely NOT performance critical.
|
|
|
|
|
Super Lloyd wrote: a code reviewer asked that I used that syntax
Why ?
|
|
|
|
|
because it's faster! (by perhaps as much as 1% I reckon!)
but yea, that's exactly what I am thinking as well....
it's one of those day I want to say, f*** those code comments....
|
|
|
|
|
Ok, you mentioned speed but it did not occur to me that this was the very reason for the change.
When not performance critical, readability takes over anything else.
Maintaining code is a nightmare, maintaining spaghetti code is a PITA inside the nightmare.
|
|
|
|
|
yea, yea, I totally agree.. this comes from C++ developer doing C#....
they pretend be very concerned by speed but their own code is an indecipherable big bowl of spaghetti...
|
|
|
|
|
If we are talking about sexiness it annoys me way more that you have
if (m_selectionHandler == null) instead of if (m_selectionHandler != null) return m_selectionHandler;
Rules for the FOSW ![ ^]
MessageBox.Show(!string.IsNullOrWhiteSpace(_signature)
? "This is my signature: " + Environment.NewLine + _signature
: "404-Signature not found");
|
|
|
|
|
ha, err... yea, I don't care either way.. but now that you mention it, I usually tend to it more that way too.. mm...
I think what happens is some previously annoying code comment and refactoring, so it ended up that way...
|
|
|
|
|
Well, you know i didn't use it much at first but then i found it very useful, since i prefer reading a method not like a chapter of a book but a "flowchart".
So if i get into that method i don't want to scroll down to see what happens if something is something
but i want to know what happens if this is true, okay return. Next "branch" if this happens, do that and return. And so on.
Makes readability in my opinion way better and if you are looking for a specific branch of actions you may find it faster.
But that's just my approach
Rules for the FOSW ![ ^]
MessageBox.Show(!string.IsNullOrWhiteSpace(_signature)
? "This is my signature: " + Environment.NewLine + _signature
: "404-Signature not found");
|
|
|
|
|
exactly my thoughts process too!
but, those code reviews are getting to me, too many stupid comments, and style guideline I don't like, so those days I just give up and do whatever the hell they say, but that also puts me in a bad mood and makes me think.. less clearly shall we say...
|
|
|
|
|
If you can't change the rules, break the rules XD
Nah srsly, i tend to go on change it, love it or leave it mentality. This solves many frustration related issues for me so far.
Rules for the FOSW ![ ^]
MessageBox.Show(!string.IsNullOrWhiteSpace(_signature)
? "This is my signature: " + Environment.NewLine + _signature
: "404-Signature not found");
|
|
|
|
|
The "extra" variable doesn't bother me. I don't think there's much difference in the cognitive load required to understand each function.
Given that the first one passed code review, it's "sexier", IMO.
Edit: I have to add, is the extra time you're spending trying to decide which version is more readable adding the requisite amount of value? *hides*
Real programmers use butterflies
|
|
|
|
|
I am just fed up with all those rubbing me wrong micro management useless comments...
I try to just shrug it off...
But it annoys me every time some (of those particular) guys reviews... but on the other hand getting any review at all is also hard work, so bloody annoying...
|
|
|
|
|
Understandable. I can't stand code reviews myself, because I have some different philosophies about how code needs to be written than a lot of people I have worked with.
However, when I am in a position where I am in charge of code reviews, I tend to go easy and stick to enforcing in-shop style guidelines more than anything.
I don't care about fast for bizdev unless something is slow enough you want to get out and push. I would have accepted either version of your code.
I think both are readable *enough* - and this is one of the areas where I differ with a lot of people. I don't spend as much time chasing readability as other coders. I like to look at cognitive load more than readability, because I feel like readability can be had by reading the complicated parts of a function more than once. The trick is in *understanding* what you've read. That's the part where I care, but also the part I'm not great at. One of the reasons I write here is to try to improve my skillset in terms of making my code understandable. My functions are too long, but that's due to some cognitive issues I have myself, and it's part of how I've adapted to them.
Real programmers use butterflies
|
|
|
|
|
Storing the value of the property (SelectedObject ) into a local variable (objects ) is like a snapshot.
It ensures that any subsequent instruction refers to the same object.
This is important when you need consistency throughout the method, the method can be relatively time consuming and some other thread can concurrently change the property value.
You can always rely on compiler optimizations and hope that the compiled machine code will take care of such thing, or you can do it by yourself with the local variable.
Another reason could be that you know in advance that someone will add code inside the method to purposely change the object value; such change will require to put the object into a variable, like the collegue told you to do.
Doing it later will require to replace any reference to the property with references to the variable: those changes would be spread along the method polluting versioning differences.
It can even be of help if copy-paste is performed to some other method where the logic must be kept but the property to process has a different name.
These are indeed pre-optimizations, and we can argue about their usefullness and their development cost.
Additionally, the name of the property is not totally meaningful, due to the fact it is singular but it can store multiple objects (like the name of the variable unfolds): this could be an important hint for the future-you maintaining the code.
|
|
|
|
|
Just for giggles, how about:
private IMultipleComponentHandler SelectionHandler => m_selectionHandler ??= SelectedObject switch
{
null => new InspectorMultipleComponentHandler(Array.Empty<object>()),
IMultipleComponentHandler handler => handler,
object[] e when !e.GetAttributes<IgnoreIEnumerableAttribute>.Any() => e,
IEnumerable e when !e.GetAttributes<IgnoreIEnumerableAttribute>.Any() => e.Cast<object>().ToArray(),
var e => new InspectorMultipleComponentHandler(new[] { e }),
}; As with your first example, this only accesses SelectedObject once.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
we're still stuck on .NET 4.7.2 at the moment....
not for long I heard .NET 6 is coming, like the winter!
|
|
|
|
|
If you're using VS2019 or 2022, you can still use that construct in .NET 4.7.2; you just need to manually edit your project file to enable C# 9.
If you already have a <LangVersion> element in the file, change it to <LangVersion>9.0</LangVersion> . Otherwise, add that element next to the <TargetFramework> element.
Quite a few C# 8/9/10 features will work in .NET Framework projects:
Using C# 9 outside .NET 5 · Discussion #47701 · dotnet/roslyn · GitHub[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Interesting...
Though in our case there is some sort of build system, which is still mysterious to me, that generate the .csproj files.. so I would need to get familiar with that first!
In fact... I really ought to become more familiar with this particular system....
|
|
|
|
|
Whiskey. Tango. Foxtrot.
Freedom is the freedom to say that two plus two make four. If that is granted, all else follows.
-- 6079 Smith W.
|
|
|
|