Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace magic numbers with macros and add explaining comments #249

Merged
merged 5 commits into from
Apr 6, 2023

Conversation

AniruddhaKanhere
Copy link
Member

Description

This PR removes the magic numbers from the code and replaces them with macros.
The value to which the macro is set is explained with comments in Doxygen formatting.

Test Steps

Checklist:

  • I have tested my changes. No regression in existing tests.
  • I have modified and/or added unit-tests to cover the code changes in this Pull Request.

Related Issue

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

kstribrnAmzn
kstribrnAmzn previously approved these changes Apr 5, 2023
@kstribrnAmzn
Copy link
Member

I've approved. I'll take it you're using the build checks for your testing?

@AniruddhaKanhere
Copy link
Member Author

I've approved. I'll take it you're using the build checks for your testing?

Yes, I relied on the PR unit-tests and coverage to validate that the code is correct.
Also did not run any integration tests as all I am doing is changing the variables/magic numbers to macros and NOT changing any core logic (or any logic for that matter). And bugs originating from these changes should be caught by the unit-tests.

@@ -2862,7 +2946,7 @@ MQTTStatus_t MQTT_Ping( MQTTContext_t * pContext )
MQTTFixedBuffer_t localBuffer;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
MQTTFixedBuffer_t localBuffer;
uint8_t pingreqPacket[ MQTT_PACKET_PINGREQ_SIZE ];

@@ -2877,6 +2961,7 @@ MQTTStatus_t MQTT_Ping( MQTTContext_t * pContext )

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line above isnt necessary.

@@ -2877,6 +2961,7 @@ MQTTStatus_t MQTT_Ping( MQTTContext_t * pContext )

if( status == MQTTSuccess )
{
assert( packetSize == localBuffer.size );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert( packetSize == localBuffer.size );

@@ -2904,7 +2989,7 @@ MQTTStatus_t MQTT_Ping( MQTTContext_t * pContext )
* from the user provided buffers. Thus it can be sent directly. */
sendResult = sendBuffer( pContext,
localBuffer.pBuffer,
2U );
packetSize );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
packetSize );
MQTT_PACKET_PINGREQ_SIZE );

source/core_mqtt.c Outdated Show resolved Hide resolved
uint8_t * pIndex;
TransportOutVector_t pIoVector[ MQTT_SUB_UNSUB_MAX_VECTORS ];
TransportOutVector_t * pIterator;
uint8_t serializedTopicFieldLength[ MQTT_SUB_UNSUB_MAX_VECTORS ][ 2 ];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like a 16-bit length type would be a simpler way to handle this?

@@ -161,7 +256,7 @@ static int32_t sendMessageVector( MQTTContext_t * pContext,
*
* @return The number of vectors added.
*/
static size_t addEncodedStringToVector( uint8_t serailizedLength[ 2 ],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A typedef seems like a better choice here.

size_t vectorsAdded = 0U;

/* When length is non-zero, the string must be non-NULL. */
assert( ( length != 0U ) == ( string != NULL ) );

serailizedLength[ 0 ] = ( ( uint8_t ) ( ( length ) >> 8 ) );
serailizedLength[ 1 ] = ( ( uint8_t ) ( ( length ) & 0x00ffU ) );
serializedLength[ 0 ] = ( ( uint8_t ) ( ( length ) >> 8 ) );
Copy link
Contributor

@paulbartell paulbartell Apr 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A macro or static inline helper function could improve clarity here

source/core_mqtt.c Outdated Show resolved Hide resolved
* Username + 2 = 9
* Password + 2 = 11
*/
#define CORE_MQTT_CONNECT_PACKET_MAX_VECTORS ( 11U )
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar situation here. This is only used once.

source/core_mqtt.c Outdated Show resolved Hide resolved
Copy link
Contributor

@paulbartell paulbartell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The additional comments are certainly useful. I don't think they warrant macros though, especially when used in a single place.

@AniruddhaKanhere AniruddhaKanhere merged commit 19412aa into FreeRTOS:main Apr 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants