Your opinion on written cpp libraries

I'll start of with and apology for my improper English.

This isn't a problem solving post, more so inquiry on my Arduino library writing skills.
Please provide insight on the code attached below. I would like your thoughts, opinions and concerns on how to refine & fortify the written library (should I include private variables and stuff like that).

If you know a good tutorial/blog/post on how to write better libraries, please do not hesitate to post it here.

MAIN FOO.INO FILE

#include <kywrd.h>
#include <Arduino.h>
#include <cfg.h>

cfg cfg ;

void setup ()
{
  uint8_t i_p [] = { 1 , 2 } ; // i_p = INPUT PIN
  cfg.ent ( i_p ) ;
  
  uint8_t o_p [] = { 3 , 4 } ; // o_p = OUTPUT PIN
  cfg.outp ( o_p ) ;
}

void loop ()
{
}

COMPILE-TIME CONSTANTS HEADER FILE

#ifndef KYWRD_H
#define KYWRD_H

#define PM pinMode

#define I INPUT
#define I_P INPUT_PULLUP
#define O OUTPUT

#define AR analogRead
#define DR digitalRead

#define AW analogWrite
#define DW digitalWrite

#define H HIGH
#define L LOW

#define Z 0

#endif

CONFIGURATION HEADER FILE

#ifndef CFG_H
#define CFG_H

#include <kywrd.h>
#include <Arduino.h>

class cfg 
{	
	public :
		cfg () ;
		void ent ( uint8_t i_p [] ) ;
		void outp ( uint8_t o_p [] ) ;
} ;

#endif

CONFIGURATION CPP FILE - purpose of this code is to assign given pins as either INPUT, OUTPUT or INPUT_PULLUP

#include <kywrd.h>
#include <Arduino.h>
#include <cfg.h>

cfg :: cfg () {}

void cfg :: ent ( uint8_t i_p [] ) // function on assigning INPUT to selected pins
{
	for ( int i = Z ; i < sizeof ( i_p ) ; i++ )
		PM ( i_p [ i ] , I ) ;
}

void cfg :: outp ( uint8_t o_p [] ) // function to assign OUTPUT so selected pins
{
	for ( int i = Z ; i < sizeof ( o_p ) ; i ++ )
		PM ( o_p [ i ] , O ) ;
}

why the cryptic short identifiers?

why the cryptic short identifiers?

First of all i had to google what that even means, but now that i know (or at least think so), in what way are the lacking?

I am not writing this library with the intent of public use. I am merely trying to make things easier on myself. I personally believe writing DW instead of digitalWrite is much more time efficient.

Are there perhaps any drawback of using them? As far as i understand the compiler just replaces those words with their listed counterparts while compiling.

People can't read your code, but then, if the code is for your consumption only, who cares?

Why all the extra blanks, if you are concerning about effort expended in typing?

Thank you for the replay!

makes it look more organised & cleaner which I like.

Perhaps any further thoughts on the core implementation you would like to share (i.e. should I create a private array _i_p / _o_p and use this instead of the actual i_p / o_p array; or stuff like this)?

It is, as long as you're actively working on the project. But if you come back to an earlier project, say, 2 years from now because you want to update some part of it, you have to wade through all kinds of nondescript letter combinations that used to make perfect sense to you one day, but that could mean any number of things today. So you have to go back and forth through your code to track down your own logic from 2 years ago. Trust me, it ain't fun. It really pays off in the end to write code as if you're writing it for other people to understand. If only because 'other people' is going to be you one day down the line!

Thank you for the replay!

Guess I'll either completely remove the cryptic identifiers or properly comment on them and their usage.

Consider how you will be if you picked up your code 1 year from now. Will the short names mean as much then as they do now?

I like sparkfuns How to write a Great Arduino Library

Arduinos general Tutorial: https://www.arduino.cc/en/Hacking/libraryTutorial and the API Style Guide

regarding your code
well ... so much already said... naming things ... just one of your first lines what does .ent() stand for?
Why precompiler defines instead of constexpr?
Why precompiler defines to replace keywords?
Why a class without member variables, why not hand over pins to the class?
Why these blanks?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.