|
Urban Cricket wrote: John Carmack uses early returns. Looking at my bookshelf: The Graphics Programming Black Book, by Michael Abrash and with a foreword by John Carmack. Fearsomely thick tome, full of how to write fast graphics code.
That kind of processor cycle counting on a 386 or 486 is outdated, while the algorithmic optimizations remain as current as ever, especially if you are able to delegate them to the graphics processor. Early returns as a way to waste no processor cycle too much in a function is only rarely important anymore.
I have lived with several Zen masters - all of them were cats.
His last invention was an evil Lasagna. It didn't kill anyone, and it actually tasted pretty good.
|
|
|
|
|
That is true, but it has other benefits too. Generally as a principle I prefer within a piece of code, if the task is done within a branch it should exit immediately. Later these blocks can be moved out to separate methods or classes. I work in a team where a couple of the members like to add thousands of lines of code in a single method and management tends to let us do whatever we want. So on one hand I really like being left to do what I want in terms of programming style, on the other hand they don't let me lecture others on how to code
Usually I start a project, write the skeleton and after the first version they assign others to add features, which lead to those 1000 lines functions.Early returns help me with that. I just take that additional code and put it in a separate method or class
modified 20-Oct-19 21:02pm.
|
|
|
|
|
Such long functions can be a pain, no matter what. Early returns definitely are better to follow than deeply nested code with some code blocks being dozens or even hundreds of lines long.
I have lived with several Zen masters - all of them were cats.
His last invention was an evil Lasagna. It didn't kill anyone, and it actually tasted pretty good.
|
|
|
|
|
Unless you're an embedded developer trying to squeak out the quickest code on an underpowered processor. I was always getting projects with weenie processors and grandiose firmware requirements. It seems that the hardware engineer had a rather limited grasp on firmware development and how much space/speed would be required for the project. They eventually figured out that I should be involved in the processor and memory requirements selection.
|
|
|
|
|
I sure know that. My first computer had a hex keyboard and actually was very much like today's microcontrollers. And I still have it and use it, just to stay sharp.
Edit: Picture[^]
I have lived with several Zen masters - all of them were cats.
His last invention was an evil Lasagna. It didn't kill anyone, and it actually tasted pretty good.
|
|
|
|
|
I kind of wish I'd kept my first computer. It was a z80 S100 bus system where I wire wrapped all the boards except for the display card (16 lines by 64 character with a rf modulator to display on a TV). I had a 2kb monitor program (Zapple) that gave you the standard peek/poke memory, dump memory, input/output to I/O, run from an address and set break points. I use to knows most of the z80 instruction set by the numbers. Good times.
|
|
|
|
|
Great! The good old times never ended for me, partially because I would see it as a great loss if that old computer ever died, partially because such old tech is the is the only option to tinker around with as a hobby without getting poor. I'm certain you know what kind of equipment and knowledge is needed when you want to play around with the most modern stuff.
It's more interesting to overcome the limitations of the old stuff anyway. Modern chipsets do that for you, so where is the point in puzzling them together as intended? Why don't you grab a Z80 on ebay and build a new old computer to bring back the old times?
I have lived with several Zen masters - all of them were cats.
His last invention was an evil Lasagna. It didn't kill anyone, and it actually tasted pretty good.
|
|
|
|
|
There's actually some modern stuff that's inexpensive. Look at some of TI's Launchpads. They're on the order of $25 and the IDE they provide (Code Composer Studio) is free. It's based on Eclipse so it's pretty powerful. The last project I was working on before I retired (woohoo!!) was based off of one of their Launchpads. I was able to get a lot of the code working before we got our own hardware up and running.
|
|
|
|
|
sasadler wrote: Eclipse So we meet again, my old enemy!
I have lived with several Zen masters - all of them were cats.
His last invention was an evil Lasagna. It didn't kill anyone, and it actually tasted pretty good.
|
|
|
|
|
Yeah, there is that. So far, I've only found 1 version of Code Composer Studio (CCS) that worked reliably for me (version 7.4), all the later versions I tried would work for a bit and then would stop letting me reliably debug my code through the jtag device. I'd have to close CCS and restart it to be able to reconnect to our hardware. Sometimes had to unplug the (USB based) jtag device and plug it back in to get it to start working again.
I only used CCS for compiling and debugging, the rest of the time I used my own favorite editor to write the code.
|
|
|
|
|
That is the best advice I have seen from re-sharper and I've seen it show some bad alternatives
one was and only know it was re-sharper as the Dev stated it was what re-sharper advised him to use
public bool someMethod(int valueA, bool Valueb, bool ValueC)
{
var output = DoSomethingElse(ValueA, ValueB) ? RunAnotherMethod(ValueA, output.SOmething) :
DoAnotherMethod(ValueB, ValueC) ? RunAthoerMethod(ValueB, output.SomethingElse) :
DoYetAnotherMethod(ValueA, ValueC) RunAnotherMethod(DoYetAnotherMethod(ValueA, ValueC), output.AgainSomethingElse);
}
This horrid IF ELSE statement contains 16 if's
Footnote: I don't like using Resharper
Every day, thousands of innocent plants are killed by vegetarians.
Help end the violence EAT BACON
|
|
|
|
|
Layout can help a lot:
public bool someMethod(int valueA, bool Valueb, bool ValueC)
{
var output = DoSomethingElse(ValueA, ValueB)
? RunAnotherMethod(ValueA, output.SOmething)
: DoAnotherMethod(ValueB, ValueC)
? RunAthoerMethod(ValueB, output.SomethingElse)
: DoYetAnotherMethod(ValueA, ValueC)
...etc.
}
- I would love to change the world, but they won’t give me the source code.
|
|
|
|
|
Still: Yuck!
I have lived with several Zen masters - all of them were cats.
His last invention was an evil Lasagna. It didn't kill anyone, and it actually tasted pretty good.
|
|
|
|
|
agreed. that is just a nasty example of embedded logic.
Charlie Gilley
<italic>Stuck in a dysfunctional matrix from which I must escape...
"Where liberty dwells, there is my country." B. Franklin, 1783
“They who can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.” BF, 1759
|
|
|
|
|
|
That would depend on whether I return a value or not.
On a related note, K&R or Allman?
|
|
|
|
|
Whitesmiths.
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I did expect that answer.
|
|
|
|
|
You're a monster
Allman ftw.
|
|
|
|
|
Under most circumstances, a single return is best. But ... I much prefer this:
void MyFunc ()
{
if (!ValidateUserInput(ATextBox.Text))
{
TellUserAboutTheProblem();
return;
}
if (!ValidateUserInput(AnotherTextBox.Text))
{
TellUserAboutTheProblem();
return;
}
...
} To this:
void MyFunc ()
{
if (!ValidateUserInput(ATextBox.Text))
{
TellUserAboutTheProblem();
}
else if (!ValidateUserInput(AnotherTextBox.Text))
{
TellUserAboutTheProblem();
}
else
{
...
}
} And sometimes the best thing to do is just return, particularly from a nested loop:
for (int i = 0; i < 100; i++)
{
for (int j = 0; j < 100; j++)
{
if (myArray[i, j] == value) return true;
}
}
return false; Any other mechanism is just making it more complicated, not less.
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
That's the longer and much better version of what i couldn't bother to write down.
|
|
|
|
|
I thought you were joking when you said Whitesmiths!
for()
{
for()
Really hurts my eyes
for()
{
for()
I like.
"If we don't change direction, we'll end up where we're going"
|
|
|
|
|
You are welcome to your opinion*, but I like the way it's consistent: the indentation is the whole of the relevant block of code:
if (a)
b;
if (a)
{
b;
c;
} Instead of the inconsistent Allman:
if (a)
b;
if (a)
{
b;
c;
}
* As long as your opinion doesn't include using 1TB, of course.
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I have been brainwashed to NEVER do:
if (a)
b;
I think the preference is a matter of taste. I don't find either alternative more "consistent". "Consistent" depends on how you define that word (and the words "relevant code"). When skimming over large blocks of code the braces are - for my eyes - easier to find Allman style.
And the b; is very slightly easier on my eye in your Allman example. My guess is that we prefer what we were exposed to when we started walking... coding...
"If we don't change direction, we'll end up where we're going"
|
|
|
|
|