How do YOU handle bad parameters in functions? [closed] - syntax

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Let's say you have some function that has one int parameter that can be good or bad. Let's say that it's bad when it's less than 5. And if it's bad you should get out of function. I think you already made up that function in your mind. Now tell me which of these functions is what you would've written.
1.
void abc(int a)
{
if (a < 5) return;
//...
}
2.
void abc(int a)
{
if (a >= 5)
{
//...
}
}
This may sound like a really stupid question. But I often have hard time deciding between these two lol.

I prefer the first way:
if a < 5
// return error or throw exception
To me it looks like some kind of "guard". Also you should handle this "bad" variable somehow (return error, throw exception) and it will be harder to follow if this is in some else block somewhere in the function.

Related

How do I print the result of a for loop? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 months ago.
Improve this question
[Trying to count from 1-10 using a for loop][1]
For our purposes here, pay attention to the second for loop in this picture.
[1]: https://i.stack.imgur.com/iUnGD.png
Use this code for print result in console:
public static void LoopSolution()
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine($"Count: {i + 1}");
}
}
In console result:

Elegant/Best solution to get the last element from an iteration [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
I'm receiving multiple messages over a channel, and after iterating over them, I would like to keep the last element for further usage. My first (probably bad!) approach was to declare some variable, and then assign it every loop.
let last = 0;
for some in rx_from_channel.iter() {
let last = some;
}
let a = last + 5;
I really don't like this solution - is there a to avoid assigning last in each loop?
Further, I would have expected that after using let last inside the for {} loop for the first time, the variable declared above the loop goes out of scope - and ļast shouldn't be available after the for {} loop at all. The compiler suggests otherwise - why?
You can just do:
let last = rx_from_channel.iter().last().unwrap_or_else(|| &0);
let a = last + 5;
See last()
fn last(self) -> Option<Self::Item>
Consumes the iterator, returning the last element.
Doesn't method last() solve your problem?

Map string (key) to number (or any data) [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
In JavaScript I can do something like this:
const periods = {
'mercury': 0.2408467,
'venus': 0.61519726,
'earth': 1.0,
'mars': 1.8808158,
'jupiter': 11.862615,
'saturn': 29.447498,
'uranus': 84.016846,
'neptune': 164.79132,
};
const planetName = prompt();
const period = periods[planetName];
How can I do similar thing in go-lang?
You came very close to answering your own question. :)
Just put those tags on google.
package main
import (
"fmt"
)
func main() {
periods := map[string]float32{
"mercury": 0.2408467,
"venus": 0.61519726,
}
fmt.Println(periods["mercury"])
}

Calculate difference between two numbers and get the absolute value [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I want to find the difference between two numbers in Go and the result should not be in "-".
Please find my code below:
dollarValue := 240000 - 480000
The result is "-240000". But my expected output is just "240000". Can anybody help on how to calculate the difference between these two numbers.
Your title is misleading. It should be states without negative instead of - operator.
Basically what you want to get is the absolute different between two numbers
You have two options:
Use if/else condition to return the positive result if the result is negative
Use math.Abs (need to convert from/to float)
Just implement your own method
func diff(a, b int) int {
if a < b {
return b - a
}
return a - b
}
and use it like this:
dollarValue := diff(240000, 480000)

Is it humanly possible to fix indent size issues w/o access to a syntax tree? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Asked another way, if I showed you this masked code file, using only your human brain, is it possible to fix the indentation issues, even if you know it should be 2-space indentation?
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
x
x
xxxxxxxxxxxxxxxxxxxxxxxxxxx
I have my own ideas, but I don't want to bias the answer. The actual source code and language will be revealed after I get a good batch of answers. Feel free to post your fix as a code block below.
This test assumes the following:
You have no idea the language in which this code is written.
All you know is how many spaces or tabs lead up to the first character of each line. In this case, there are no tabs (just spaces).
You know what the indent size should be. In this case, 2 spaces.
Note: If it's possible with your human brain, it should also be possible with code, right?
Bonus Points (optional): How would you break-down the logic to tackle this problem?
EDIT: Here's the source code, from which these exes were created:
function greet(firstName, lastName) {
var firstName = prompt('What is your first name?');
var lastName = prompt('Last name?');
var fullName = firstName + ' ' + lastName;
for (var i = 0; i < 10; i++) {
console.log('Hello,', fullName + '!');
}
}
greet(firstName, lastName);
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
x
x
xxxxxxxxxxxxxxxxxxxxxxxxxxx

Resources