Comments (14)
@gilbertw, @antkmsft, @vhvb1989 which one do you prefer for simple functions (without output parameters)?
from azure-sdk-for-c.
I vote for option 2.
from azure-sdk-for-c.
option 2 looks simpler to maintain.
I would probably get rid of AZ_CONTRACT_ARG_NOT_NULL
for now.
from azure-sdk-for-c.
@vhvb1989 yeah, we don't have AZ_CONTRACT_ARG_NOT_NULL
in Azure.Core yet. I will have a PR for that. It's a macro that may return az_result
.
from azure-sdk-for-c.
@JeffreyRichter, @barcharcraz ?
from azure-sdk-for-c.
I still think you need to write it with the out parameters, if we want a fancy functional interface we should deliver that as a wrapper in a functional language.
Also, this lacks a third option which is "az_result can have non-error values but is never used as a channel for the return value"
from azure-sdk-for-c.
@barcharcraz I think, we've already discussed that we may have functions that never ever return errors and in this case we would like to return a value instead of az_result
. Really, they are very usefull and simplify logic and remove useless checks (like non-null output parameters). Without them, our C development life will be even harder :-)
About the third option, we may say it's the second option but with slightly adjusted meaning of what do we call error or non-error. We can still reserve the space for "non-error" but I personally would like to avoid API that uses such "non-error" things.
from azure-sdk-for-c.
This is the best version:
az_result az_span_get(az_span span, size_t i, uint8_t * out) {
AZ_CONTRACT_ARG_NOT_NULL(out);
if (span.size <= i) {
return AZ_ERROR_OUT_OF_RANGE;
}
*out = span.begin[i];
return AZ_OK;
}
If we do contract checking, then we should check that i
is >=0 && i < span.size
and return an az_result for failing this contract. i being out of range is also a contract violation - not an error.
These code samples are a good step but really, when we compare approaches we need to see what the calling/customer code would look like; not what our code would look like.
from azure-sdk-for-c.
@barcharcraz I've added an option 3. One of the problems with this option, it is more error prone because it's not obvious from a function signature that there is a special non-error case AZ_NO_MORE_ITEMS
that requires special attention.
from azure-sdk-for-c.
@JeffreyRichter I agree, usage examples would be good.
az_result az_span_get(az_span span, size_t i, uint8_t * out) {
AZ_CONTRACT_ARG_NOT_NULL(out);
// we can't use `span.size - 1` because it may overflow in case of `span.size == 0` because `size_t` is unsigned integer.
AZ_CONTRACT_ARG_IN_RANGE(i, 0, span.size);
// or AZ_CONTRACT_ARG_ARRAY_INDEX(i, span.size);
*out = span.begin[i];
return AZ_OK;
}
from azure-sdk-for-c.
@antkmsft, @vhvb1989, @gilbertw I've added an option # 3. When az_result may have non-error/non-value status.
I still vote for # 2.
from azure-sdk-for-c.
By the way: AZ_CONTRACT_ARG_...
requires that the function returns az_result
. Example of AZ_CONTRACT_ARG_NOT_NULL
:
#define AZ_CONTRACT_ARG_NOT_NULL(arg) \
do { \
if ((arg) == NULL) { \
return AZ_ERROR_ARG; \
} \
} while (0)
from azure-sdk-for-c.
from azure-sdk-for-c.
We've settled on the az_result return with out parameters.
az_result az_span_get(az_span span, size_t i, uint8_t * out) {
AZ_CONTRACT_ARG_NOT_NULL(out);
// we can't use `span.size - 1` because it may overflow in case of `span.size == 0` because `size_t` is unsigned integer.
AZ_CONTRACT_ARG_IN_RANGE(i, 0, span.size);
// or AZ_CONTRACT_ARG_ARRAY_INDEX(i, span.size);
*out = span.begin[i];
return AZ_OK;
}
from azure-sdk-for-c.
Related Issues (20)
- Direct Method Example to be added on arduino HOT 10
- Azure Central: TELEMETRY_FREQUENCY_IN_SECONDS not working HOT 3
- azure-sdk-for-c-arduino, can't see telemetry data from devices in AZURE,ESP32 HOT 1
- Add optional logging to _az_RETURN_IF_*
- Telemetry Payload question, azure-sdk-for-c-arduino, ESP32 HOT 8
- ublox SARA R500S and ESP32 HOT 2
- message properties HOT 7
- Azure IoT Central telemetry data duplication issue HOT 3
- Create linter baseline file for azure-sdk-for-c. HOT 1
- Failure in Windows VS 2022 CI tests HOT 2
- Re-enable address sanitizer on VS2022
- DEVICE STUCK AS CONNECTING TO SNTP SERVER HOT 1
- Unable to build az_iot_hub_client samples on windows HOT 3
- In Azure central IoT API Query GROUP BY with local time zone feature HOT 4
- Azure SDK-for-C, IoT Central Device Connecting OK, not sending telemetry HOT 6
- Arduino port version for ESP32 Compile Time setting vs. Runtime settings for IOT hub client and MQTT client
- Azure_SDK_for_C ESP32 on Arduino IDE Compile Errors {Compilation error: 'esp_mqtt_client_config_t' {aka 'struct esp_mqtt_client_config_t'} has no member named 'uri'} HOT 6
- [azure-sdk-for-c-arduino]: Azure_Iot_Central_ESP32.ino have minor missing esp_mqtt_event_handler when compile on arduino core 3.0.1 using arduino-cli HOT 2
- Device update HOT 1
- ADU Deployment failed Despite Successful Device Update HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from azure-sdk-for-c.